Layouts with Flexbox
Put boxes side by side, space them evenly and center anything with flexbox, the layout tool behind most modern navigation bars and card rows.
Welcome back!
Step 1 of 12Learn
Turning a stack into a row
By default, block elements like <div>, <section> and <p> stack on top of each other, each taking the full width. That's fine for a blog post, but a navigation bar or a row of project cards needs things side by side.
Flexbox solves this. You turn it on for the parent element, the container, and its direct children become flex items that line up in a row:
<div class="cards">
<div class="card">Robot</div>
<div class="card">Volcano</div>
<div class="card">Game</div>
</div>.cards { display: flex; gap: 16px; }That's it: three cards in a row with 16 pixels between them. The gap property spaces items without fiddling with margins.
The key idea: flex properties go on the container, and they control how the children are arranged. Beginners often put display: flex on the cards themselves and wonder why nothing moves. Only the direct children of the flex container are rearranged; grandchildren keep their normal behavior unless their own parent is also a flex container.
Step 2 of 12Practice · Choose one
Rosa wants three .card elements inside a .gallery div to sit in a row. Where does display flex go?
Step 3 of 12Compare
Before and after display: flex
| Aspect | Normal blocks | Flex container |
|---|---|---|
| Direction | Stacked top to bottom | Side by side in a row |
| Width of each item | Full width | As wide as its content |
| Spacing | Margins on each item | One gap property on the container |
| Where you write it | - | On the parent, not the children |
Step 4 of 12Learn
Main axis, cross axis
A flex container has two directions. The main axis is the direction items flow: left to right by default (flex-direction: row), or top to bottom with flex-direction: column. The cross axis runs perpendicular to it.
Two properties control alignment:
justify-contentspaces items along the main axis. Values:flex-start(bunched at the start, the default),center,flex-end,space-between(first and last items at the edges, equal space between) andspace-aroundorspace-evenly.align-itemsaligns items on the cross axis. Values:stretch(the default, items fill the height),flex-start,center,flex-end.
A classic navigation bar, with the logo on the left and links on the right:
header { display: flex; justify-content: space-between; align-items: center; }And the famous trick that used to take hacks: centering something both horizontally and vertically.
.hero { display: flex; justify-content: center; align-items: center; height: 300px; }If you switch flex-direction to column, the axes swap: justify-content now works vertically and align-items horizontally. That swap trips up everyone at least once.
Step 5 of 12Practice · Diagram
Flexbox properties at a glance
Tap the property that vertically centers items in a normal row.
Tap the shape that answers the question, then press Check.
- Flexbox
- On the container
- On the items
- display: flex
Turns flex on.
- flex-direction
row or column: sets the main axis.
- justify-content
Aligns along the main axis.
- align-items
Aligns across, on the cross axis.
- gap
Space between items.
- flex-wrap
Lets items move onto a new line.
- flex: 1
The item grows to share spare space.
Step 6 of 12Compare
justify-content values with three items
| Aspect | Value | What you see in a row |
|---|---|---|
| flex-start | default | All three items bunched on the left, leftover space on the right |
| center | centered | Items grouped in the middle, equal empty space on both ends |
| space-between | spread to edges | First item at the left edge, last at the right edge, equal gaps between |
| space-evenly | even gaps | Equal space before, between and after every item |
Step 7 of 12Practice · Match the pairs
Match each goal to the CSS that achieves it on a flex container.
Tap an answer, then tap the place where it belongs (or drag it there).
- Logo on the left, menu on the right
- Center items vertically in a tall header
- Stack items top to bottom
- Put 20 pixels between every item
Step 8 of 12Learn
Letting items grow and wrap
Two more properties make flex layouts flexible.
flex-wrap. By default, flex items squeeze onto one line no matter how narrow the screen. Six cards on a phone become six thin slivers. Add flex-wrap: wrap; to the container and items that don't fit move to the next line.
flex. On an item, flex: 1; means "grow to share the leftover space equally." Give three cards flex: 1 and they split the row into three equal columns. Give one card flex: 2 and it gets twice as much of the leftover space as a card with flex: 1.
Combine them with a minimum width for a gallery that adapts by itself:
.gallery { display: flex; flex-wrap: wrap; gap: 16px; }
.gallery .card { flex: 1 1 200px; }The flex: 1 1 200px shorthand means: grow if there's room, shrink if needed, and start from 200 pixels. On a wide screen you might get four cards per row; on a phone, one. No extra code required.
Flexbox handles one direction at a time. For full two-dimensional grids with rows and columns lined up, CSS also offers display: grid, a great next topic once flexbox feels natural.
Step 9 of 12Diagram
Six cards on a narrowing screen
Tap a shape with to see more.
- Laptop: 6 in a row
- Screen narrows
- No wrap: cards squeeze thin
Default flex-wrap: nowrap.
- flex-wrap: wrap
Cards that do not fit move to the next line.
- Phone: neat rows of 1-2
Step 10 of 12Practice · True or false
Without flex-wrap set to wrap, flex items will automatically move to a new line when they run out of space.
Step 11 of 12Note
Step 12 of 12Practice · What would you do?
What would you do?
Omar's header has a logo and a menu. With display flex on the header, both sit pressed together on the left, and the logo is a bit higher than the menu text. Which declarations fix both problems?
Lesson complete!