CSS Flexbox & Grid Cheat Sheet
Two layout systems, one page. Flexbox lays things out in one direction; Grid lays them out in two. Pick the right one and half the CSS writes itself.
Which one do I need?
| Situation | Use | Why |
|---|---|---|
| A row or column of items (nav bar, button group, form row) | Flexbox | One-dimensional: items flow along a single axis |
| A page layout with rows and columns (sidebar + content, card grid) | Grid | Two-dimensional: you control rows and columns together |
| Centering one thing, horizontally and vertically | Either | Grid: place-items: center. Flexbox: two properties — see below |
| Items that should wrap onto new lines as the screen shrinks | Flexbox (flex-wrap: wrap) or Grid (auto-fill) | Both work; Grid gives you aligned columns, Flexbox gives you flowing rows |
| An unknown number of equal columns | Grid (repeat(auto-fill, minmax(…))) | Flexbox can't keep columns aligned across rows |
Flexbox — the container
Set on the parent. The main axis runs in the flex-direction; the cross axis is perpendicular to it.
.row {
display: flex; /* or inline-flex */
flex-direction: row; /* row | row-reverse | column | column-reverse */
justify-content: center; /* main axis */
align-items: center; /* cross axis, one line */
flex-wrap: wrap; /* nowrap | wrap | wrap-reverse */
align-content: center; /* cross axis, multiple lines only */
gap: 12px; /* row-gap column-gap */
flex-flow: row wrap; /* shorthand: direction + wrap */
}
| Property | Values | Does |
|---|---|---|
display: flex | flex / inline-flex | Turns the element into a flex container; its children become flex items |
flex-direction | row (default) · row-reverse · column · column-reverse | Sets the main axis — and flips what "justify" and "align" mean |
justify-content | flex-start (default) · flex-end · center · space-between · space-around · space-evenly | Distributes items along the main axis |
align-items | stretch (default) · flex-start · flex-end · center · baseline | Aligns items along the cross axis, within one line |
flex-wrap | nowrap (default) · wrap · wrap-reverse | Whether items spill onto extra lines instead of shrinking |
align-content | stretch · flex-start · flex-end · center · space-between · space-around · space-evenly | Distributes the lines along the cross axis — does nothing without wrapping |
gap | e.g. 12px or 8px 16px (row column) | Space between items only — never at the container's edges |
Spacing, decoded: space-between puts the first item flush left and the last flush right, with equal gaps between. space-around gives every item equal space on both sides, so the edge gaps are half the middle gaps. space-evenly makes all gaps — edges included — exactly equal.
Flexbox — the items
Set on the children. Three properties decide how an item grows and shrinks; the flex shorthand bundles them.
.item {
flex-grow: 1; /* take a share of free space (0 = don't grow) */
flex-shrink: 1; /* give up space when cramped (0 = never shrink) */
flex-basis: 0; /* starting size before growing/shrinking */
flex: 1 1 0; /* shorthand: grow shrink basis */
align-self: center; /* override align-items for this one item */
order: 2; /* default 0; negative numbers go first */
margin-left: auto; /* absorbs free space — pushes the item right */
}
| Shorthand | Expands to | Means |
|---|---|---|
flex: 1 | 1 1 0% | Share the space equally, ignoring content size — the classic equal-columns trick |
flex: auto | 1 1 auto | Grow and shrink, but start from the content's natural size |
flex: none | 0 0 auto | Fixed size — never grows, never shrinks |
flex: 0 0 200px | — | A fixed 200px column that refuses to flex at all |
flex: 2 | 2 1 0% | Takes twice the free space of a flex: 1 sibling |
The one gotcha: a flex item will not shrink below its content's minimum size — a long unbreakable word or a wide image can blow the layout out. Fix it on the item with min-width: 0 (or overflow: hidden).
Grid — the container
Set on the parent. fr is a fraction of the free space left after fixed tracks are placed.
.grid {
display: grid; /* or inline-grid */
grid-template-columns: 200px 1fr 2fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
gap: 16px; /* row-gap column-gap */
justify-items: stretch; /* default alignment inside each cell */
align-items: stretch;
place-items: center; /* shorthand: align-items justify-items */
justify-content: start; /* distribute the tracks when the grid is smaller than the container */
align-content: start;
grid-auto-flow: row; /* row | column | dense */
grid-auto-rows: minmax(100px, auto); /* size of rows you didn't declare */
}
| Pattern | What it builds |
|---|---|
grid-template-columns: repeat(3, 1fr) | Three equal columns |
repeat(12, 1fr) | A 12-column layout grid — items span columns as needed |
repeat(auto-fill, minmax(240px, 1fr)) | A responsive card grid: as many 240px+ columns as fit; empty tracks stay empty |
repeat(auto-fit, minmax(240px, 1fr)) | Same, but empty tracks collapse — the cards stretch to fill the row |
grid-template-columns: 240px 1fr | Fixed sidebar + fluid content |
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr) | Two columns in a 1:2 ratio that are allowed to shrink below content size (see gotchas) |
grid-template-rows: auto 1fr auto | Header / content / footer — the content row takes the leftover height |
Grid — the items
Set on the children. Line numbers start at 1; span counts tracks from the start line.
.item {
grid-column: 1 / 3; /* from line 1 to line 3 (spans 2 columns) */
grid-column: 2 / span 2; /* start at line 2, span 2 columns */
grid-row: 1 / -1; /* -1 = the last line: spans all rows */
grid-area: sidebar; /* a name from grid-template-areas */
grid-area: 1 / 1 / 3 / 4; /* row-start / column-start / row-end / column-end */
justify-self: center; /* start | end | center | stretch */
align-self: center;
place-self: center; /* shorthand: align-self justify-self */
}
| Property | Does |
|---|---|
grid-column / grid-row | Places the item on explicit lines — 1 / 3, span 2, or 1 / -1 for "all of it" |
grid-area | A named area from grid-template-areas, or the four-line shorthand |
justify-self / align-self | Overrides the container's justify-items / align-items for one item |
grid-auto-flow: dense | Backfills holes left by spanning items — great for masonry-like grids, but it reorders items visually |
Centering, and three layouts you'll reuse forever
/* Center one thing, both axes — the one-liner */
.center { display: grid; place-items: center; }
/* Same thing with Flexbox */
.center { display: flex; align-items: center; justify-content: center; }
/* Sidebar + content */
.layout { display: grid; grid-template-columns: 240px 1fr; gap: 24px; }
/* Sticky footer: content pushes the footer to the bottom */
.page { display: grid; grid-template-rows: auto 1fr auto; min-height: 100vh; }
/* Holy grail with named areas */
.holy-grail {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-areas: "nav main aside";
}
.holy-grail nav { grid-area: nav; }
.holy-grail main { grid-area: main; }
.holy-grail aside { grid-area: aside; }
Gotchas that break both
| Symptom | Cause | Fix |
|---|---|---|
| A flex item overflows its container and won't shrink | Items can't shrink below their content's minimum size | min-width: 0 on the item (or overflow: hidden) |
| Equal flex columns aren't equal | flex: 1 vs flex: auto: 1 sets basis 0% (ignore content), auto keeps content size | Use flex: 1 when you want truly equal widths |
A grid column grows wider than its 1fr share | 1fr really means minmax(auto, 1fr) — content can force it wider | Write minmax(0, 1fr) instead of 1fr |
align-content does nothing | It only distributes lines — with one line of flex items, or one row of grid tracks, there's nothing to distribute | Use align-items for a single line; align-content needs wrapping / multiple rows |
space-between leaves no gap at the edges | That's what it does — first item flush left, last flush right | Use space-evenly (equal gaps everywhere) or add gap + justify-content: center |
| Percentage heights inside a grid row do nothing | Percentages resolve against a definite height — auto rows don't have one | Give the container a height (min-height: 100vh) or use 1fr rows |
gap in Flexbox doesn't work in an old browser | gap for Flexbox landed in browsers in 2021 (Safari 14.1+) | Use margins as a fallback if you still support very old Safari |
Keep going
- Color Converter — HEX / RGB / HSL / CMYK for the colors in your CSS, with contrast checks
- Markdown Syntax Cheat Sheet — the other one-pager you'll reach for every week
- Regex Quick Reference — patterns for validating the values your layouts display