Flexbox explained: axes, alignment and the grow and shrink maths
Flexbox lays items out in one direction at a time and shares spare space between them. Once you know which axis you are on and how the free space is split, most flexbox puzzles solve themselves.
Think in two axes
Put display: flex on a parent and its direct children become flex items. The main axis runs in the direction set by flex-direction: left to right for row, top to bottom for column. The cross axis is at a right angle to it.
The two alignment properties map to those axes, and this is the part people mix up most:
justify-contentplaces items along the main axis.align-itemsplaces items along the cross axis.
Switch from row to column and the two properties swap their visual effect, because the axes rotate with the direction.
How flex-grow shares spare space
The browser first gives each item its flex-basis, then splits whatever is left over in proportion to each item’s flex-grow value.
item extra = free space × (item grow ÷ total grow)
Example. A 900px row holds three items, each with flex-basis: 200px and no gap. Their grow values are 1, 2 and 1.
- Free space: 900 − 600 = 300px
- Total grow: 1 + 2 + 1 = 4, so one share is 300 ÷ 4 = 75px
- Final widths: 200 + 75 = 275px, 200 + 150 = 350px, 200 + 75 = 275px
Notice that the middle item is not twice as wide as the others. It gets twice as much of the extra space. If you want exact ratios of the whole width, set flex-basis: 0 so all the space counts as free.
How flex-shrink takes space back
When items overflow, flex-shrink decides who gives up width. The browser weights each item by its shrink value multiplied by its basis, so large items shrink more.
Example. A 500px row holds two items, each with a 300px basis. The overflow is 600 − 500 = 100px.
- Shrink 1 and 1: the weights are equal, so each loses 50px and ends at 250px.
- Shrink 1 and 3: the weights are 300 and 900. The first loses 100 × 300 ÷ 1200 = 25px (275px left); the second loses 75px (225px left).
An item never shrinks below its minimum content width by default, because min-width: auto applies. A long word or URL can therefore push a flex row wider than its parent. Setting min-width: 0 on the item fixes that.
Recipes for everyday layouts
Centre one thing in both directions
.box { display: flex; justify-content: center; align-items: center; min-height: 200px; }
Logo on the left, links on the right
.header { display: flex; align-items: center; gap: 16px; }
.header .logo { margin-right: auto; }
An auto margin in flexbox absorbs all free space on that side, which pushes everything after it to the far end.
Cards that wrap
.cards { display: flex; flex-wrap: wrap; gap: 16px; }
.cards > * { flex: 1 1 240px; }
Footer stuck to the bottom
body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex: 1; }
The flex shorthand
| You write | Grow | Shrink | Basis |
|---|---|---|---|
flex: 1 | 1 | 1 | 0% |
flex: auto | 1 | 1 | auto |
flex: none | 0 | 0 | auto |
flex: 0 0 120px | 0 | 0 | 120px |
Common mistakes
- Putting
display: flexon the item instead of the parent. - Expecting
align-itemsto do anything in a single-line row with no extra height. - Using flexbox for a strict two-dimensional table of rows and columns, which is the job CSS grid does better.
Questions
Does gap work with flexbox?
Yes. Current browsers support gap on flex containers, so you no longer need margins on every item to space them.
Why does order not change what a screen reader reads?
The order property only changes visual placement. Assistive technology and keyboard focus still follow the source order, so keep the HTML in a sensible order.