Flexbox, Finally Understood: The One-Dimensional Layout Mental Model
Understand CSS Flexbox for good: main and cross axes, flex-grow, flex-shrink and flex-basis, alignment, wrapping, and the min-width: auto bug.

You give three cards flex: 1 and expect three equal columns. Two of them are equal, the one with a long product name is wider, and a URL in the third card pushes the whole row past the edge of the screen. You add width: 33%, which mostly works until someone adds a gap. Flexbox feels like it has opinions you were never told about.
CSS Flexbox, the Flexible Box Layout module, is a one-dimensional layout system. A flex container arranges its children, the flex items, in a single row or column, and distributes free space among them according to rules you control with flex-grow, flex-shrink and flex-basis. “One-dimensional” means each line is laid out independently: items in one row know nothing about items in the next.
My position: Flexbox is not a weaker Grid. It is the right tool when content should decide sizes, and the wrong tool when a design decides them. Once you think “content out” for Flexbox and “layout in” for Grid, choosing becomes obvious.
Two axes, and why they rotate
Every flex container has a main axis, along which items are placed one after another, and a cross axis perpendicular to it. flex-direction picks the main axis:
row(default): the main axis follows the inline direction, the way text runs. In English that is left to right. In Arabic or Hebrew, withdir="rtl", it runs right to left automatically.column: the main axis follows the block direction, the way paragraphs stack, usually top to bottom.row-reverseandcolumn-reverse: the same axes, flipped.
This is why the alignment properties are named for axes rather than for left, right, top and bottom. justify-content always works on the main axis and align-items always works on the cross axis. When you switch from row to column, the two properties swap their visual effect. If centering “stopped working” after changing direction, that is the reason.
How Flexbox sizes items: the algorithm in five steps
The spec’s sizing algorithm is long, but its core fits in five steps. Run them in your head and Flexbox stops surprising you.
- Find each item’s flex base size. This comes from
flex-basis. If the basis isauto, the browser uses the item’swidth(orheightin a column), and if that is alsoauto, it uses the item’s content size. - Measure the free space. Subtract the sum of the base sizes, plus gaps and margins, from the container’s main size. The result can be positive (room left over) or negative (overflow).
- If space is positive, grow. Divide it among items in proportion to their
flex-growvalues. Items withflex-grow: 0get nothing. - If space is negative, shrink. Take it away in proportion to
flex-shrinkmultiplied by each item’s base size. Large items give up more pixels than small ones. - Clamp and repeat. If any item hits its
min-widthormax-width, freeze it at that limit and redistribute the remaining space among the others.
A worked growing example
A 600px container holds three items with bases of 100px, 200px and 100px, and flex-grow values of 1, 1 and 2. There is no gap.
| Item | Base | Grow | Share of 200px free space | Final width |
|---|---|---|---|---|
| A | 100px | 1 | 200 × 1/4 = 50px | 150px |
| B | 200px | 1 | 200 × 1/4 = 50px | 250px |
| C | 100px | 2 | 200 × 2/4 = 100px | 200px |
Notice that flex-grow: 2 did not make C twice as wide as A. It made C receive twice as much of the leftover space. Grow values divide free space, not total width. That one sentence explains most “Flexbox ratios are wrong” complaints.
A worked shrinking example
A 300px container holds two items with bases of 300px and 100px, both with the default flex-shrink: 1. They overflow by 100px. Shrinking is weighted by base size, so the weights are 300 and 100:
- The first item gives up 100 × 300/400 = 75px and ends at 225px.
- The second gives up 100 × 100/400 = 25px and ends at 75px.
The weighting is deliberate. Without it, a small icon and a large text block would lose the same number of pixels, and the icon would vanish first. With it, everything shrinks roughly in proportion to its size.
The flex shorthand, decoded
The flex shorthand sets grow, shrink and basis together. Its keyword and single-number forms are where the surprises live, because the single-number form changes the basis to zero.
| Shorthand | Expands to | Behavior |
|---|---|---|
| (default) | 0 1 auto |
Content-sized, can shrink, never grows |
flex: auto |
1 1 auto |
Starts at content size, shares leftover space |
flex: 1 |
1 1 0% |
Ignores content size, shares all the space |
flex: 2 |
2 1 0% |
Twice the share of a flex: 1 sibling |
flex: none |
0 0 auto |
Rigid, exactly content size |
flex: 1 1 15rem |
as written | Starts at 15rem, then grows and shrinks |
Now the opening bug makes sense. flex: 1 sets the basis to zero, so all space is free space and is divided equally. That should give equal columns, and it does, until one item’s content cannot fit in its equal share. Then a different rule takes over.
The min-width: auto problem
Flex items have a default min-width: auto (or min-height: auto in a column). For flex items, auto resolves to the item’s minimum content size: roughly the width of its longest unbreakable word, URL or image. Step 5 of the algorithm then freezes the item at that size, and the others absorb the difference.
That is why a card with a long product name grows wider than its flex: 1 siblings, and why text-overflow: ellipsis often appears to do nothing inside a flex item: the item never gets narrow enough for the text to overflow.
/* WRONG: the title never truncates; the row overflows instead */
.row { display: flex; gap: 1rem; }
.row__title {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* RIGHT: allow the item to shrink below its content size */
.row__title {
flex: 1;
min-width: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
The fix is min-width: 0 on the flex item. It also applies one level up: if the truncating text sits inside a nested flex container, that container needs min-width: 0 too, because it is itself a flex item of its parent. Setting overflow to anything other than visible on the item has the same effect, because it changes the automatic minimum to zero. Grid has the identical rule for its 1fr tracks, covered in the CSS Grid guide.
Alignment and spacing
Four properties handle alignment. Remember them by axis.
justify-contentdistributes items along the main axis:flex-start,center,flex-end,space-between,space-around,space-evenly. It only does something when there is free space, so it has no effect if any item has a non-zeroflex-growthat consumes it.align-itemspositions items on the cross axis within their line:stretch(default),flex-start,center,flex-end,baseline.align-selfoverridesalign-itemsfor one item.align-contentdistributes whole lines on the cross axis, and only matters when items wrap onto more than one line.
There is no justify-self in Flexbox, because the main axis is shared by all items. To push one item away from the others, use an auto margin. Auto margins absorb free space before justify-content runs:
.navbar {
display: flex;
align-items: center;
gap: 1rem;
}
.navbar__actions {
margin-inline-start: auto; /* takes all free space, pushing itself to the end */
}
Use gap for spacing between items. It works on Flexbox in every current browser, never adds space at the edges, and avoids the margin bookkeeping described in the box model guide.
The default align-items: stretch is also how Flexbox gives you equal-height cards for free: every item in a row stretches to the tallest one. It is also why a sticky sidebar inside a flex row stops sticking, so set align-self: start on anything sticky.
Wrapping, and where Flexbox stops being the right tool
With flex-wrap: wrap, items that do not fit move to a new line. Each line then runs the sizing algorithm on its own. This produces a pattern that is great for some interfaces and wrong for others:
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.tags > * { flex: 0 0 auto; } /* chips stay at content size */
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.cards > * { flex: 1 1 16rem; } /* last row items stretch to fill */
For tags and chips, independent lines are exactly right: each line packs as many as fit. For a card grid, they are not. If the last row has one card, it stretches across the whole width, because that line has nothing else to share space with. You cannot align a card on the second row with a card on the first, because rows do not know about each other. That is a two-dimensional problem, and Grid’s repeat(auto-fill, minmax(16rem, 1fr)) solves it in one line.
Order and accessibility
The order property and the -reverse directions change visual order without changing DOM order. Screen readers and keyboard Tab order still follow the DOM. A layout where the visual sequence differs from the focus sequence fails WCAG 2.4.3 (Focus Order) when the mismatch affects meaning or operation, and it confuses sighted keyboard users who watch focus jump backward.
Use order only for purely visual rearrangements where reading order does not matter, and prefer changing the source order when it does. Chromium has shipped a reading-flow property that makes focus order follow the visual flex or grid order, but it is not yet cross-browser, so do not rely on it alone.
Five patterns worth memorizing
/* 1. Center anything */
.center { display: flex; align-items: center; justify-content: center; }
/* 2. Media object: fixed image, flexible text */
.media { display: flex; gap: 1rem; align-items: flex-start; }
.media__img { flex: none; }
.media__body { flex: 1; min-width: 0; }
/* 3. Sticky footer: footer at the bottom on short pages */
body { display: flex; flex-direction: column; min-height: 100dvh; }
main { flex: 1; }
/* 4. Input with attached button */
.input-group { display: flex; }
.input-group input { flex: 1; min-width: 0; }
.input-group button { flex: none; }
/* 5. Toolbar with actions pushed right */
.toolbar { display: flex; gap: 0.5rem; align-items: center; }
.toolbar .spacer { margin-inline-start: auto; }
Pattern 3 uses 100dvh, the dynamic viewport height unit, which tracks mobile browser toolbars appearing and disappearing. Plain 100vh on mobile is taller than the visible area when the toolbar is shown.
Flexbox or Grid?
| Question | If yes |
|---|---|
| Should content size decide how much space each item gets? | Flexbox |
| Is it a single row or column of things, like a navbar, toolbar or button group? | Flexbox |
| Should items wrap and pack as many per line as fit, like tags? | Flexbox |
| Must items line up in both rows and columns? | Grid |
| Does the design define the tracks before the content arrives? | Grid |
| Do you need items to overlap or span areas? | Grid |
Frequently Asked Questions
What is the difference between flex: 1 and flex: auto?
flex: 1 expands to 1 1 0%, so items ignore their content size and share all available space equally. flex: auto expands to 1 1 auto, so items start at their content size and only share the leftover space, which makes items with more content wider.
Why is text-overflow: ellipsis not working in a flex item?
Flex items default to min-width: auto, which stops them from shrinking below the width of their content. The text never overflows, so no ellipsis appears. Add min-width: 0 to the flex item, and to any nested flex container around it.
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis, which is horizontal in a row. align-items positions items on the cross axis, which is vertical in a row. When flex-direction is column, the axes swap, and so do their visual effects.
When should I use Flexbox instead of CSS Grid?
Use Flexbox when items sit in a single row or column and their content should decide their size, such as navbars, toolbars, button groups and tag lists. Use Grid when items must align in both rows and columns or the design defines the tracks in advance.
Think content out
Flexbox is a negotiation between content and space: items say how big they want to be, the container says how much room there is, and grow and shrink settle the difference. Keep that model in mind, add min-width: 0 when content should yield, and use Grid the moment you need two dimensions. When you need that second dimension, CSS Grid is the tool for it.
Rule of thumb: if you are fighting Flexbox to line up rows with each other, you wanted Grid.
Originally posted on 29 May 2025 and post last modified on 29 May 2025