Flexbox by drawing it
You give flex items a width, and they come out a different size. Here is the algorithm that decides, run on a single row: start at the basis, then grow or shrink to fill the line.
01 Three boxes in one row
You write display: flex and three boxes jump into a row. Then you give them widths, and the browser ignores them: they come out wider, or narrower, than you asked. Flexbox is not ignoring you. It follows a short algorithm, and once you see it run on one row you can predict every width.
The three numbers in flex are grow, shrink and basis. The rest of this page works out what they produce.
02 Two axes: main and cross
A flex container lays its children out along one line, the main axis. flex-direction picks it: row (the default) runs left to right in English, column runs top to bottom. The cross axis is the other one, at right angles.
| Property | Axis | What it does |
|---|---|---|
| flex-direction | Main | Chooses the main axis: row or column |
| flex-grow, flex-shrink, flex-basis | Main | Size each item along the main axis |
| justify-content | Main | Places items when space is left over after growing |
| align-items | Cross | Places or stretches each item across the line |
| flex-wrap | Both | Lets items start a new line instead of shrinking |
Sizing happens on the main axis only. Across, the default align-items: stretch makes every item as tall as the tallest one, which is why flex columns come out the same height.
- item
- empty
The line is as tall as its tallest item, B. stretch grows the others to match; the other values keep each item’s own height and only move it.
03 Every item starts at its flex-basis
Flexbox first gives each item a starting size along the main axis: its flex-basis. With flex-basis: auto (the default) that is the item’s width in a row, or, if it has none, the size of its content.
Then it subtracts. Free space is the container’s size minus all the bases. If it is positive, flex-grow decides who gets it. If it is negative, flex-shrink decides who gives some back. Nothing else is shared out.
04 flex-grow shares out the space left
Each item’s flex-grow is its number of shares in the free space. Here is the row from the first section, 600px wide, drawn in 50px cells.
- just added
- item
- free space
- not laid out yet
The container is 600px wide and holds A, B and C. Nothing is placed yet.
Grow adds to the basis; it does not set the final size. A and B both grew by 50px, and B is still bigger because it started bigger.
05 flex-shrink takes back what does not fit
When the bases add up to more than the container, the free space is negative and items shrink. The rule is slightly different: each item gives back in proportion to flex-shrink times its basis, so big items give more.
- just placed
- just shrank
- item, inside
- item, overflowing
- free space
- outside the container
Now the container is only 400px wide, and A, B and C each have flex-basis: 200px. Cells after the mark are outside the container.
Weighting by basis keeps small items from vanishing. If shrinking were shared equally, a 40px icon next to two 400px panels could be squeezed to nothing.
06 What flex: 1 really means
Most code sets all three values with the flex shorthand, and the common values hide different bases. That is where most surprises come from.
| Shorthand | grow | shrink | basis |
|---|---|---|---|
| (nothing set) | 0 | 1 | auto |
| flex: 1 | 1 | 1 | 0% |
| flex: auto | 1 | 1 | auto |
| flex: none | 0 | 0 | auto |
0, so the whole row is free space, shared equally. Three items get a third each, whatever their content.To check a result, open DevTools: Chrome and Firefox show a flex badge next to the container in the Elements panel that draws the items, the free space and the gaps on the page. Or read the sizes from the console:
07 When flexbox surprises you
.title { flex: 1; } /* holds a long URL */An item that will not shrinkFlex items have min-width: auto, so they do not shrink below their content’s smallest size. A long word or URL pushes the row wider. Add min-width: 0 (or overflow: hidden) to the item..a { width: 300px; flex: 1; }width that seems ignoredflex: 1 sets flex-basis: 0%, and a basis other than auto wins over width. Use flex-basis to size flex items, or flex: 0 0 300px to fix one..row img { } /* in a flex row */Images stretched tallThe default align-items: stretch stretches an item to the line’s height. An image whose height is not set can be stretched out of shape. Set align-items: flex-start or align-self on the image..cards { display: flex; } /* 12 cards */Items squeezed instead of wrappingThe default is flex-wrap: nowrap, so items shrink to fit one line. Add flex-wrap: wrap and a basis, such as flex: 1 1 200px, to let them flow onto new lines.justify-content: center; /* items have flex: 1 */justify-content does nothingjustify-content only places leftover space. If any item can grow, it takes all the free space first and there is nothing left to place.