css layout cheat sheet: flex direction, justify content, align items, gap and wrap, and grid
Stacks · 11 min read · posted Nov 30, 2026, 9:30 pm
Most CSS layout comes down to the same five things: flex direction, justify content, align items, gap and wrap, and grid. Once you can picture where each one moves the boxes, layout stops being trial and error.
This is the cheat sheet from the reel. There's a "which one do I want" lookup first. Then for each of the five you get the property and its values, a small piece of CSS, where the boxes actually landed, the mistake that usually bites, and a couple of things to practise on with the answers.
Every block of CSS here was saved as a page with the HTML below, opened in headless Google Chrome 158.0.8127.88 on macOS, and every box was measured with getBoundingClientRect(). The numbers are what Chrome returned, in CSS pixels from the top left corner of .box. The sketches are drawn from those numbers, one character for every 10 pixels across.
Unless a section says otherwise, the HTML is a box with numbered items in it, three, five or six of them:
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
which one do i want
| i want to | write | what chrome did |
|---|---|---|
| put things side by side | .box { display: flex; } |
three 80px boxes at x 0, 80, 160 |
| stack them instead | flex-direction: column; |
the same boxes at y 0, 50, 100 |
| centre one thing in a box | display: flex; justify-content: center; align-items: center; |
a 100 x 60 box in a 300 x 200 box lands at x 100, y 70, dead centre |
| the same, with grid | display: grid; place-items: center; |
x 100, y 70 again |
| push one item to the far end | .item:last-child { margin-left: auto; } in a flex row |
the last box moves to x 240 of 300, the others stay at 0 and 60 |
| spread items out, ends touching the edges | justify-content: space-between; |
x 0, 120, 240, two equal 60px gaps |
| space between items, not round the edges | gap: 20px; |
20px between each pair, nothing at either end |
| let items wrap onto new lines | flex-wrap: wrap; |
five 80px boxes in 300px: three on the first line, two on the second |
| make side-by-side items the same height | leave align-items alone (it's stretch) and don't give them a height |
an 80px sidebar grows to the 260px of the content next to it |
| equal columns | display: grid; grid-template-columns: repeat(3, 1fr); |
three 100px columns in 320px, with 10px gaps |
| one item two columns wide | grid-column: span 2; |
210px wide: two columns plus the gap between them |
| a full-width row in a grid | grid-column: 1 / -1; |
320px, the whole grid |
| as many columns as fit | grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); |
3 columns in a 500px box, 4 in a 700px box |
which axis is which
Flexbox has two axes. The main axis is the way the items run. The cross axis is across it. justify-content always works on the main axis and align-items always works on the cross axis, so turning the row into a column swaps what they do.
flex-direction: row (the default) |
flex-direction: column |
|
|---|---|---|
| main axis | left to right | top to bottom |
justify-content moves items |
across | up and down |
align-items moves items |
up and down | across |
1. flex direction
When you reach for it. You want things side by side (a nav bar, a row of buttons, an icon next to its label), or you want to stack them and still have flexbox's alignment tools.
The property. display: flex on the box turns its children into flex items and lines them up along the main axis. flex-direction sets which way that axis runs.
| value | items run |
|---|---|
row (default) |
left to right |
row-reverse |
right to left, packed at the right |
column |
top to bottom |
column-reverse |
bottom to top: item 1 landed at y 100, item 3 at y 0 |
The CSS.
.box {
width: 300px;
display: flex;
}
.item {
width: 80px;
height: 50px;
}
Where the boxes landed. Before display: flex the divs are blocks, one per line. With it, they sit in a row. Add flex-direction: column and they stack again.
| css | box | item 1 | item 2 | item 3 |
|---|---|---|---|---|
no display (block) |
300 x 150 | x 0, y 0 | x 0, y 50 | x 0, y 100 |
display: flex |
300 x 50 | x 0, y 0 | x 80, y 0 | x 160, y 0 |
display: flex; flex-direction: column |
300 x 150 | x 0, y 0 | x 0, y 50 | x 0, y 100 |
display: flex |[ 1 ][ 2 ][ 3 ]......|
Every item stayed 80 x 50 in all three. A column looks just like plain blocks here, but now justify-content, align-items and gap work on it.
What bites. Centring a column that has no height. With flex-direction: column, justify-content: center works top to bottom, but a box with no height is exactly as tall as its items, so there is no spare room to share out and nothing moves.
.box {
width: 300px;
display: flex;
flex-direction: column;
justify-content: center;
}
.box height |
box | item 1 | item 2 | item 3 |
|---|---|---|---|---|
| not set | 300 x 150 | y 0 | y 50 | y 100 |
height: 300px |
300 x 300 | y 75 | y 125 | y 175 |
Give the box a height (or let it fill a parent that has one) and the items centre: 75px of space above and 75px below.
Practise.
- Reverse the order of three boxes without touching the HTML. (
flex-direction: row-reverse. Chrome put item 1 at x 220, item 2 at 140 and item 3 at 60: packed at the right, 1 furthest right.)
row-reverse |......[ 3 ][ 2 ][ 1 ]|
- Put a label above its input instead of beside it. (
display: flex; flex-direction: column;on the element that holds both. Chrome put the label at y 0 and the input straight under it at y 18 (the label is 18px tall in Chrome's default font), both stretched to the full 300px width, becausealign-items: stretchworks across in a column.)
2. justify content
When you reach for it. The items are in a row and you want to decide where the spare room goes: all at the end, split around them, or between them.
The property. justify-content on the flex box shares out the free space on the main axis. Here the box is 300px and the three items are 60px each, so there's 120px to share.
.box {
width: 300px;
display: flex;
justify-content: center;
}
.item {
width: 60px;
height: 50px;
}
Where the boxes landed.
| value | item 1 x | item 2 x | item 3 x | the 120px went |
|---|---|---|---|---|
flex-start (default) |
0 | 60 | 120 | all after the last item |
flex-end |
120 | 180 | 240 | all before the first |
center |
60 | 120 | 180 | 60 each end |
space-between |
0 | 120 | 240 | 60 in each gap, none at the ends |
space-around |
20 | 120 | 220 | 20 at each end, 40 in each gap |
space-evenly |
30 | 120 | 210 | 30 everywhere |
flex-start |[ 1 ][ 2 ][ 3 ]............|
flex-end |............[ 1 ][ 2 ][ 3 ]|
center |......[ 1 ][ 2 ][ 3 ]......|
space-between |[ 1 ]......[ 2 ]......[ 3 ]|
space-around |..[ 1 ]....[ 2 ]....[ 3 ]..|
space-evenly |...[ 1 ]...[ 2 ]...[ 3 ]...|
space-around gives each item the same space on both of its sides, so the gaps in the middle are twice the ends. space-evenly makes every gap the same, ends included.
What bites. Putting justify-content on a box that isn't a flex (or grid) box. It's quietly ignored. With the same CSS minus display: flex, Chrome stacked the items at x 0, y 0, 50 and 100, as plain blocks.
The other one is reaching for justify-content to move a single item. It moves all of them as a group. To push one item away from the rest, give it an auto margin:
.item:last-child {
margin-left: auto;
}
margin-left: auto |[ 1 ][ 2 ]............[ 3 ]|
Items 1 and 2 stayed at x 0 and 60, and item 3 went to x 240. The auto margin soaked up all 120px of free space. That's the usual way to do a nav bar with the logo on the left and the links on the right.
Practise.
- Three buttons with the same gap between them and at both ends. (
space-evenly: x 30, 120, 210, every gap 30px.) - A footer with a 120px copyright on the left and two 50px links pushed right. (
margin-left: autoon the first link: the links landed at x 200 and 250. Or wrap the links in one element and usejustify-content: space-between: the wrapper landed at x 200, 100px wide.)
3. align items
When you reach for it. Things in a row don't line up vertically: an icon sits higher than its label, or cards in a row come out different heights.
The property. align-items on the flex box places each item on the cross axis, which is up and down in a row.
| value | each item |
|---|---|
stretch (default) |
grows to fill the line, if it has no height of its own |
flex-start |
sits at the top |
flex-end |
sits at the bottom |
center |
sits in the middle |
The CSS. The box is 200px tall. The items have a min-height instead of a height, so stretch has room to work.
.box {
width: 300px;
height: 200px;
display: flex;
align-items: center;
}
.item {
width: 60px;
min-height: 40px;
}
Where the boxes landed. All three items moved together, so this is item 1.
| value | item 1 y | item 1 height |
|---|---|---|
flex-start |
0 | 40 |
flex-end |
160 | 40 |
center |
80 | 40 |
stretch |
0 | 200 |
| not set | 0 | 200 |
Leaving align-items out gave the same result as stretch, because that's the default.
Centre a div. Centre on both axes at once: justify-content for across, align-items for down.
.box {
width: 300px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
}
.item { width: 100px; height: 60px; }
With one 100 x 60 item, Chrome put it at x 100, y 70: 100px either side and 70px above and below. display: grid; place-items: center; on the box gave exactly the same x 100, y 70.
What bites. Expecting stretch to work on an item that has a height. Give the items height: 40px instead of min-height: 40px and they stay 40px tall, at y 0, even though align-items: stretch is right there. Stretch only fills in a size the item hasn't set.
The other one is mixing up the two. If the items move left and right when you wanted up and down, you've used justify-content where you wanted align-items, or the box is a column (see "which axis is which" above).
Practise.
- Line up a 40 x 40 icon with a 120 x 20 label next to it, 12px apart. (
display: flex; align-items: center; gap: 12px;. Chrome put the icon at y 0 and the label at x 52, y 10, so their middles both sit at 20.) - A sidebar that's always as tall as the content beside it. (
display: flexon the box, and no height on the sidebar. An 80px wide sidebar next to 260px of content came out 260px tall.)
4. gap and wrap
When you reach for it. The items are touching and you want space between them, or there are too many to fit on one line.
The properties.
| property | values | does |
|---|---|---|
gap |
a length, or two (row column) |
space between items, and between lines. never at the outside edges |
row-gap, column-gap |
a length | one direction only |
flex-wrap |
nowrap (default), wrap, wrap-reverse |
whether items may start a new line. wrap-reverse stacks the lines bottom up |
The CSS. Five 80px items in a 300px box. 5 x 80 is 400, so they don't fit.
.box {
width: 300px;
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.item {
width: 80px;
height: 50px;
}
Where the boxes landed.
| css | box | item widths | item x positions |
|---|---|---|---|
display: flex only |
300 x 50 | 60 | 0, 60, 120, 180, 240 |
+ gap: 20px |
300 x 50 | 44 | 0, 64, 128, 192, 256 |
+ flex-wrap: wrap |
300 x 120 | 80 | 0, 100, 200 on line one (y 0), then 0, 100 on line two (y 70) |
display: flex |[ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]|
+ gap: 20px |[1 ]..[ 2 ]..[3 ]..[ 4 ]..[5 ]|
+ flex-wrap: wrap |[ 1 ]..[ 2 ]..[ 3 ]..|
|[ 4 ]..[ 5 ]............|
Without wrap, flexbox squeezes the items to fit: each lost 20px. The gap squeezed them further, to 44px. With wrap, each item went back to its own 80px, three fit on a line (80 + 20 + 80 + 20 + 80 = 280) and the rest started a second line. The gap went between the lines too, so line two starts at y 70, not 50.
What bites. Three things here.
Not knowing items shrink. If the items must keep their width, add flex-shrink: 0. Then they don't wrap or squeeze, they spill out of the box: item 5 landed at x 320, past the 300px edge.
flex-shrink: 0 |[ 1 ][ 2 ][ 3 ][ 4 | ][ 5 ]
Using margins for spacing. margin: 0 10px on each item puts 10px outside the first and last item too: the items landed at x 10, 110 and 210, with 20px between them but 10px at each end. gap only goes between.
Wrapping inside a box with a fixed height. Give the wrapping box height: 200px and the second line drops to y 110 instead of 70. The lines share out the extra height between them. Add align-content: flex-start to the box and line two goes back to y 70.
Practise.
- A row of tags that wraps, with 8px between tags both ways. (
display: flex; flex-wrap: wrap; gap: 8px;. Five 90px tags in 300px: three on the first line at x 0, 98, 196, and the second line at y 32, 8px under the 24px tags.) - Six 80px cards in 300px, 20px between rows and 10px between columns. (
gap: 20px 10px;with wrap. The row gap comes first. Chrome put three on each line at x 0, 90, 180, and the second line at y 70.row-gap: 20px; column-gap: 10px;gave exactly the same.)
5. grid
When you reach for it. You want rows and columns that line up with each other: a card grid, a gallery, a page with a header, sidebar and main area. Flexbox lays out one line at a time. Grid lays out both directions at once.
The properties.
| property | goes on | example | does |
|---|---|---|---|
display: grid |
the box | turns the children into grid items | |
grid-template-columns |
the box | repeat(3, 1fr) |
the columns: how many and how wide |
gap |
the box | 10px |
space between rows and columns |
grid-column |
an item | span 2, 1 / -1 |
how many columns that item covers |
1fr means one share of the space left over after the gaps and any fixed columns. repeat(3, 1fr) is three equal shares.
The CSS.
.box {
width: 320px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.item {
height: 40px;
}
.item:first-child {
grid-column: span 2;
}
Where the boxes landed. Six items. Without display: grid they're full-width blocks, 320 x 40 each, stacked from y 0 to y 200.
With the grid, but before the span 2 rule. 320px minus two 10px gaps is 300px, so each column is 100px:
| x 0 | x 110 | x 220 | |
|---|---|---|---|
| y 0 | 1 | 2 | 3 |
| y 50 | 4 | 5 | 6 |
With .item:first-child { grid-column: span 2; }. Item 1 came out 210px wide (100 + 10 + 100), and the rest moved along to the next free cells, so item 6 started a third row:
| x 0 | x 110 | x 220 | |
|---|---|---|---|
| y 0 | 1 (210 wide) | 1 | 2 |
| y 50 | 3 | 4 | 5 |
| y 100 | 6 |
The box grew from 90px tall to 140px.
As many columns as fit.
grid-template-columns:
repeat(auto-fill, minmax(150px, 1fr));
Each column is at least 150px, and they share whatever is left. In a 500px box Chrome made three 160px columns. In a 700px box it made four columns of 167.5px. Nothing else changed, which is why this one line shows up in so many responsive layouts.
What bites. Wide content in a 1fr column. A 1fr column won't shrink below the content inside it. Put a 200px wide element in item 1 of the three-column grid above and the columns come out 200, 50 and 50 instead of 100 each. Use minmax(0, 1fr) to say the column may shrink below its content:
grid-template-columns: repeat(3, minmax(0, 1fr));
With that, the columns went back to 100, 100 and 100. The 200px element still ran from x 0 to 200, so it now spills out of its 100px column and over the next one. That's usually the better problem to have: give it overflow: hidden or let it scroll, instead of the whole grid going lopsided.
Fixed pixel columns can overflow too. repeat(3, 120px) with a 10px gap needs 380px. In the 320px box the third column started at x 260 and ran 60px past the edge.
Practise.
- A header that runs across all three columns, with three items below it. (
grid-column: 1 / -1;on the header. It came out 320px wide at y 0, and the items sat in one row at y 40 below its 30px plus the 10px gap.) - A photo gallery whose column count changes with the width of the page. (
repeat(auto-fill, minmax(150px, 1fr))as above.)
flex or grid
| you're laying out | use |
|---|---|
| one row or one column of things (nav, toolbar, a button with an icon) | flex |
| items that should wrap, each line sized by its own content | flex with flex-wrap: wrap |
| rows and columns that line up with each other | grid |
| a page frame: header, sidebar, main, footer | grid |
| one thing centred in a box | either: flex with justify-content and align-items, or grid with place-items |
They mix well. A grid of cards, where each card is a flex column, is a very common pattern.
how to practise with this
Make an HTML file with a box and a few coloured divs, open it in a browser, and change one property at a time. Open the dev tools: Chrome and Firefox both draw an overlay on flex and grid boxes showing the lines, gaps and free space. That's the fastest way to see why something landed where it did.
Before you write any CSS, say which way the items should run and where the spare room should go. That usually tells you whether you need flex-direction, justify-content, align-items or gap.
honest limitations
Five things isn't all of CSS layout. flex-grow, flex-shrink and flex-basis (the flex shorthand), align-self, order, named grid areas, position and the box model all turn up too. These five are what most layouts get built from.
The measurements are from Chrome, with plain boxes and fixed sizes so the numbers come out exact. Real content has text, images and padding, so your numbers will differ, but the boxes move the same way.
If you're also brushing up on JavaScript, the javascript cheat sheet covers the five things that come up most there.