Skip to content
Velloreth
Web Development 7 min read

CSS Flexbox: A Complete Practical Guide

Flexbox lays out items along one axis. Learn the container and item properties, the common patterns, and the gotchas that trip people up.

By

The model

Flexbox lays out items along a single axis. You set a direction, and the items flow along it.

.container {
  display: flex;
  flex-direction: row; /* main axis: horizontal */
}

Two axes matter:

  • Main axis — the direction items flow, set by flex-direction.
  • Cross axis — perpendicular to the main axis.

flex-direction: row makes the main axis horizontal and the cross axis vertical. flex-direction: column swaps them. This swap is the source of most flexbox confusion, because justify-content and align-items change meaning with it.

Container properties

display

.container {
  display: flex;    /* block-level flex container */
  display: inline-flex; /* inline-level */
}

flex-direction

flex-direction: row;            /* left to right (default) */
flex-direction: row-reverse;    /* right to left */
flex-direction: column;         /* top to bottom */
flex-direction: column-reverse; /* bottom to top */

flex-wrap

flex-wrap: nowrap;       /* default: items shrink to fit one line */
flex-wrap: wrap;         /* items wrap onto new lines */
flex-wrap: wrap-reverse; /* wrap upward */

Without wrap, items shrink rather than wrapping. This is why a row of cards can squash instead of flowing onto a second line.

justify-content

Aligns along the main axis.

justify-content: flex-start;    /* default */
justify-content: flex-end;
justify-content: center;
justify-content: space-between; /* first and last at the edges */
justify-content: space-around;  /* equal space around each */
justify-content: space-evenly;  /* equal space between all */

align-items

Aligns along the cross axis.

align-items: stretch;    /* default: fill the container height */
align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: baseline;   /* align text baselines */

align-content

Aligns the lines when items wrap. Has no effect without flex-wrap: wrap.

align-content: flex-start;
align-content: center;
align-content: space-between;

gap

.container {
  display: flex;
  gap: 1rem;          /* row and column gap */
  gap: 1rem 2rem;     /* row gap, column gap */
}

gap replaced the old negative-margin trick and is the right way to space flex items.

Item properties

flex

The shorthand for grow, shrink, and basis.

.item {
  flex: 1;        /* grow: 1, shrink: 1, basis: 0% */
  flex: 0 0 200px; /* fixed 200px, no grow, no shrink */
  flex: 1 1 auto;  /* grow and shrink from content size */
}

flex: 1 is the most useful declaration in flexbox. It makes items share the available space equally.

/* Three equal columns */
.container { display: flex; }
.column { flex: 1; }

flex-grow

How much of the free space the item takes.

.a { flex-grow: 1; } /* takes 1 share */
.b { flex-grow: 2; } /* takes 2 shares */

With 300px free, .a gets 100px and .b gets 200px.

flex-shrink

How much the item shrinks when space is short.

.item { flex-shrink: 0; } /* never shrink */

Useful for icons and avatars that should keep their size.

flex-basis

The item’s starting size before growing or shrinking.

.item { flex-basis: 200px; }
.item { flex-basis: 30%; }

align-self

Overrides align-items for one item.

.item { align-self: flex-end; }

order

Changes visual order without changing DOM order.

.item { order: -1; } /* moves to the front */

Use sparingly — it creates a mismatch between visual and DOM order, which confuses keyboard navigation and screen readers.

Common patterns

Centering

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

The classic. Centers both axes.

.nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.nav .logo { margin-right: auto; } /* pushes everything else right */

margin-right: auto on the logo pushes the remaining items to the far end. This is the idiomatic way to split a flex row.

Card row with wrapping

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.card {
  flex: 1 1 300px; /* grow, shrink, start at 300px */
}

Each card starts at 300px, grows to fill the row, and wraps when there is no room. This is a responsive card grid with three lines of CSS.

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main { flex: 1; }

main grows to fill the space, pushing the footer to the bottom even on short pages.

Media object

.media {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.media img { flex-shrink: 0; }
.media .body { flex: 1; min-width: 0; }

The image keeps its size, the text takes the rest, and min-width: 0 allows the text to wrap rather than overflow.

The gotchas

min-width: auto

Flex items have min-width: auto by default, which prevents them shrinking below their content. Long text or a wide table will overflow.

.item {
  flex: 1;
  min-width: 0; /* allows shrinking below content size */
}

This is the single most common flexbox bug. If text overflows a flex item, add min-width: 0.

align-items versus align-content

align-items aligns items within a line. align-content aligns the lines themselves. align-content does nothing unless flex-wrap: wrap is set.

Percentage basis with gap

flex-basis: 50% with a gap overflows, because the gap is added to the basis. Use flex: 1 1 0 and let the gap handle spacing, or account for the gap in the basis.

order and accessibility

order changes visual order but not DOM order. Keyboard focus follows DOM order, so a visually reordered layout can be confusing to navigate. Prefer reordering the DOM.

Nested flex and height

A flex item with align-items: stretch fills the container height. If the container has no height, there is nothing to stretch to. Set a height or use min-height.

Flexbox versus grid

FlexboxGrid
DimensionsOne axisTwo axes
Content-drivenYesNo
Best forToolbars, card rows, centeringPage layout, galleries
SizingFrom contentFrom tracks

The rule: flexbox for one dimension, grid for two. A row of cards is flexbox. A page with a header, sidebar, and content is grid.

They compose. A grid cell can contain a flex container, and a flex item can be a grid.

Flexbox and accessibility

Flexbox changes visual order, and that has the same consequences as grid.

order breaks the reading order. Keyboard focus and screen readers follow the DOM, not the visual layout. An element moved with order is announced in its original position, which can be confusing.

/* The visual order is B, A — but the DOM order is A, B */
.a { order: 2; }
.b { order: 1; }

If the order matters for comprehension, reorder the markup instead.

flex-direction: row-reverse has the same problem. It reverses the visual order without changing the DOM, so a list of links appears right-to-left visually but is read left-to-right by assistive technology.

Do not use flexbox to fake a table. A flex row of divs has no row or column semantics. For tabular data, use a real table.

Test with CSS disabled. The DOM order is what assistive technology sees, and reading the page without styles reveals whether the logical order makes sense.

Practical guidance

  • Use flex: 1 for equal-width items.
  • Use gap for spacing, not margins.
  • Add min-width: 0 when text overflows.
  • Use margin: auto to push items apart.
  • Remember the axis swap when changing flex-direction.
  • Use flex-wrap: wrap for responsive rows.
  • Prefer grid for two-dimensional layout.
  • Avoid order where keyboard navigation matters.

For the related guides, see css-grid-complete-guide and responsive-design-breakpoints. For the broader context, css-specificity-explained and mobile-first-css.

Quotable summary

Flexbox lays out items along a single axis, with the main axis set by flex-direction and the cross axis perpendicular to it, which is why justify-content and align-items swap meaning when the direction changes. justify-content aligns along the main axis and align-items along the cross axis, while align-content aligns the lines themselves and only works with flex-wrap. flex: 1 is shorthand for grow, shrink, and basis, and it is the most useful single declaration in flexbox. The most common gotcha is min-width: auto on flex items, which prevents shrinking below content size and causes text overflow until you set min-width: 0. Flexbox is for one-dimensional layout and grid for two, and they compose freely.

Frequently asked questions

What is the difference between flexbox and grid?
Flexbox lays out items along one axis at a time, either a row or a column. Grid lays out items in two dimensions, with rows and columns defined together. Use flexbox for a toolbar or a card row, and grid for a page layout.
What does flex: 1 mean?
It is shorthand for flex-grow: 1, flex-shrink: 1, and flex-basis: 0%. The item grows to fill available space, shrinks if needed, and starts from zero width, so items share space equally.
What is the difference between justify-content and align-items?
justify-content aligns items along the main axis, which is horizontal in a row. align-items aligns them along the cross axis, which is vertical in a row. Changing flex-direction swaps which is which.
Why is my flex item overflowing?
Flex items have min-width: auto by default, which prevents them shrinking below their content size. Set min-width: 0 on the item to allow it to shrink, which is the usual fix for text overflow in a flex row.
How do I center something with flexbox?
Set display: flex, justify-content: center, and align-items: center on the container. That centers the children both horizontally and vertically.
What is the difference between align-items and align-content?
align-items aligns items within a single line. align-content aligns the lines themselves when items wrap onto multiple lines. align-content has no effect unless flex-wrap is set.