Lesson 16 of 52Layout
Flexbox
Lay out items in a row or column, space and align them, let them wrap and grow, and build nav bars, card rows and sticky footers.
You can now style a single box: its selectors, size, colours and type. The next problem is arranging many boxes on a page. For years, centring something vertically on a web page was a running joke among developers. Flexbox ended the joke. It is a layout mode for arranging items along one line, a row or a column, and deciding how they share space, how they line up and what happens when they run out of room. Navigation bars, button groups, card rows, media objects and page footers are all one or two flexbox declarations away. This lesson is hands-on: every section has a small live editor, so change the values and watch what happens.
Container and items
Flexbox always involves two levels. You set display: flex on a parent, which becomes the flex container. Its direct children automatically become flex items. Grandchildren are not affected; they lay out normally inside their own item.
The moment you add display: flex, three things happen. The items line up in a row instead of stacking. They shrink to fit their content instead of stretching to full width. And they all stretch to the same height as the tallest one.
Preview
Try this: delete display: flex; and watch the items stack as ordinary blocks. Put it back, then change gap: 12px to gap: 40px. The gap property adds space between items only, never on the outside edges, which is why it is cleaner than margins.
The main axis and the cross axis
Everything in flexbox is described in terms of two directions. The main axis runs the way the items flow. The cross axis runs at a right angle to it. With the default flex-direction: row, the main axis is horizontal (left to right in English) and the cross axis is vertical. Switch to column, and the two axes swap.
That swap is the key to remembering the two most-used properties:
justify-contentpositions items along the main axis.align-itemspositions items along the cross axis.
flex-direction | Main axis | justify-content moves items | align-items moves items |
|---|---|---|---|
row (default) | horizontal | left and right | up and down |
column | vertical | up and down | left and right |
row-reverse | horizontal, starting from the right | right and left | up and down |
column-reverse | vertical, starting from the bottom | down and up | left and right |
Preview
Try this: change row to column and watch the steps stack and stretch to the full width. Then try row-reverse and column-reverse. The reversed values only change what you see: screen readers and the Tab key still follow the HTML order, so do not use them to fix the meaning of your content.
justify-content: spacing along the main axis
When the items do not fill the container, justify-content decides where the spare space goes.
| Value | Spare space goes |
|---|---|
flex-start (default) | All at the end, items packed at the start |
flex-end | All at the start, items packed at the end |
center | Equally on both outer sides |
space-between | Between items, none on the outer edges |
space-around | Around each item, so edges get half a gap |
space-evenly | Exactly equal gaps everywhere, edges included |
Preview
Try this: go through center, flex-end, space-between, space-around and space-evenly one at a time. space-between is the one you will use most, for a logo on the left and a menu on the right.
display: flex; flex-direction: column;. Which property moves its items to the vertical middle of the container?align-items: lining up on the cross axis
align-items sets how items sit across the line. The default, stretch, makes them all as tall as the container (in a row), which is why flex cards in a row get equal heights for free.
| Value | Effect in a row |
|---|---|
stretch (default) | Items fill the full height |
flex-start | Items sit at the top, at their natural height |
flex-end | Items sit at the bottom |
center | Items are vertically centred |
baseline | The first lines of text line up |
To move just one item, put align-self on that item. It takes the same values.
Preview
Try this: change stretch to center, then flex-start, then baseline and look at how the words "Small", "Big" and "Two" line up. Then add align-self: flex-end; to the .big rule only.
And here is the end of the old joke. Perfect centring in both directions takes three lines:
.center-me {
display: flex;
justify-content: center;
align-items: center;
}flex-wrap: when items run out of room
By default, flex items stay on one line (flex-wrap: nowrap) and shrink to fit, even if that squashes them. With flex-wrap: wrap, items that do not fit move onto a new line instead.
Preview
Try this: with nowrap the tags burst out of the dashed box. Change it to wrap and they flow onto a second line, with gap spacing the rows as well. Try width: 180px too. When items wrap, align-content controls how the lines themselves are spaced inside a taller container.
The container is 300px wide and holds four items that are each 100px wide and cannot shrink. With flex-wrap: wrap and no gap, how many items are on the first line?
.list { display: flex; flex-wrap: wrap; width: 300px; }
.list > * { width: 100px; flex-shrink: 0; }flex-grow, flex-shrink and flex-basis
So far items have kept their natural size. Three properties on the items decide how they share the space:
flex-basisis the starting size before any growing or shrinking. The default,auto, uses the item'swidthor its content size.flex-growsays how much of the spare space an item takes.0(the default) means none. If one item has1and another has2, the second gets twice as much of the leftover space.flex-shrinksays how much an item gives up when there is not enough space. The default is1;0means "never shrink below my basis".
You usually set all three at once with the flex shorthand. The default is flex: 0 1 auto. The most common value is flex: 1, which means flex: 1 1 0%: start from zero and share all the space equally.
Every item starts at its flex-basis. With auto, that is its width or its content size.
Preview
Try this: the search box takes all the spare space because only it can grow. Give .a flex: 1 too and they split it equally. Then set .b { flex: 2; } so the search box gets twice the share. Finally set all three to flex: 1 and notice they become equal widths, because flex: 1 starts every item from zero.
Match each declaration to what it does.
One gotcha worth knowing: by default a flex item will not shrink below the width of its longest word or widest child. A long URL or a wide code block can then push the whole row wider than the screen. Adding min-width: 0 to the item lets it shrink, and then you can handle the overflow inside it.
Common recipes
Nav bar. Logo on the left, links on the right, all vertically centred. A margin-left: auto on an item soaks up all the free space on that side, which pushes it (and everything after it) to the far end.
Preview
Try this: move the push class from "Recipes" to "Sign in" so only that link sits on the far right. Then remove align-items: center and notice the logo and links no longer line up neatly.
Card row. display: flex; flex-wrap: wrap; gap: 1rem; on the container and flex: 1 1 240px; on each card. Each card starts at 240px, grows to fill the row, and wraps when the row gets too narrow. For strict columns that line up in both directions, CSS Grid (next lesson) is the better tool.
Sticky footer. On a short page, you want the footer at the bottom of the window, not halfway up. Make the body a column that is at least the full screen height, and let the main content grow:
body { display: flex; flex-direction: column; min-height: 100dvh; margin: 0; }
main { flex: 1; }Put the steps flexbox follows to size items in a row in order.
- Tap the steps below in the right order.
- flex container
- An element with display: flex; its direct children become flex items.
- main axis
- The direction flex items flow in, set by flex-direction; justify-content works along it.
- cross axis
- The direction at a right angle to the main axis; align-items works along it.
- gap
- Space between flex or grid items, never on the outer edges.
- flex-wrap
- Whether items stay on one line (nowrap) or move to new lines when they run out of room (wrap).
- flex-grow
- How much of the spare space an item takes, relative to its siblings.
- flex-basis
- An item's starting size before growing or shrinking.