Flexbox
Flexbox lines boxes up along one axis and shares out the space between them. Watch three items in a 600px row start at a size, grow, shrink and align.
In 30 seconds
Flexbox is a CSS layout mode for arranging boxes in a single line. Set display: flex on a parent and its children become flex items placed along a main axis, with a cross axis at right angles.
Each item starts at its flex-basis. Spare room is shared out according to flex-grow, and a shortage is absorbed according to flex-shrink.
The container then decides where items sit: justify-content along the main axis, align-items across it. For layouts with both rows and columns, CSS grid is usually the better tool.
Flexbox, step by step
Simplified: shrinking is really weighted by flex-shrink × flex-basis, items will not shrink below their smallest content size by default, and the axes follow the text direction of the page.
- Step 1: display: flex. By default, block elements stack down the page, each on its own row. Put display: flex on their parent and the parent becomes a flex container: its direct children become flex items, line up in one row, and each takes only as much width as its content needs.
- Step 2: Main axis, cross axis. A flex container has two axes. Items are placed one after another along the main axis; the cross axis runs at right angles to it. With flex-direction: row, the default, the main axis is horizontal. Switch to column and the two axes swap. Every other flexbox property is defined in terms of these two axes.
- Step 3: Basis: the starting size. flex-basis is the size an item starts from along the main axis, before any space is shared out. Give all three items a basis of 100px in a 600px container and they use 300px, leaving 300px of free space. Unlike display and flex-direction, this is set on the items, not the container.
- Step 4: Grow: share spare room. flex-grow says how much of the free space an item may take. The numbers are shares, not pixels. With grow values 1, 2 and 0, the 300px of free space is cut into three shares of 100px: A gets one, B gets two, and C, at 0, stays at its basis. The row is now exactly full.
- Step 5: Shrink: give room back. When the container is too small for the bases, flex-shrink decides who gives way. Squeeze the container to 240px: the items want 300px, which is 60px too much. Each has flex-shrink: 1, the default, and the same basis, so each gives up 20px and ends at 80px. An item with flex-shrink: 0 would refuse to shrink.
- Step 6: Alignment. If free space is left over, two container properties say where the items sit in it. justify-content distributes them along the main axis: packed at the start, centred, or spread apart. align-items positions them across the cross axis: stretched to fill it by default, or lined up at one edge or the centre.
- Step 7: Flexbox or grid. Flexbox lays out one line at a time. When items wrap, each line is sized on its own from its content, so edges in different lines need not match. CSS grid plans rows and columns together, so everything lines up in both directions. Choose flexbox when content sets the sizes along one axis, and grid for a two-dimensional layout.
Try it yourself
Learn this first
Spotted a mistake on this page?