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?

SituationUseWhy
A row or column of items (nav bar, button group, form row)FlexboxOne-dimensional: items flow along a single axis
A page layout with rows and columns (sidebar + content, card grid)GridTwo-dimensional: you control rows and columns together
Centering one thing, horizontally and verticallyEitherGrid: place-items: center. Flexbox: two properties — see below
Items that should wrap onto new lines as the screen shrinksFlexbox (flex-wrap: wrap) or Grid (auto-fill)Both work; Grid gives you aligned columns, Flexbox gives you flowing rows
An unknown number of equal columnsGrid (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 */
}
PropertyValuesDoes
display: flexflex / inline-flexTurns the element into a flex container; its children become flex items
flex-directionrow (default) · row-reverse · column · column-reverseSets the main axis — and flips what "justify" and "align" mean
justify-contentflex-start (default) · flex-end · center · space-between · space-around · space-evenlyDistributes items along the main axis
align-itemsstretch (default) · flex-start · flex-end · center · baselineAligns items along the cross axis, within one line
flex-wrapnowrap (default) · wrap · wrap-reverseWhether items spill onto extra lines instead of shrinking
align-contentstretch · flex-start · flex-end · center · space-between · space-around · space-evenlyDistributes the lines along the cross axis — does nothing without wrapping
gape.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 */
}
ShorthandExpands toMeans
flex: 11 1 0%Share the space equally, ignoring content size — the classic equal-columns trick
flex: auto1 1 autoGrow and shrink, but start from the content's natural size
flex: none0 0 autoFixed size — never grows, never shrinks
flex: 0 0 200px—A fixed 200px column that refuses to flex at all
flex: 22 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 */
}
PatternWhat 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 1frFixed 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 autoHeader / 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 */
}
PropertyDoes
grid-column / grid-rowPlaces the item on explicit lines — 1 / 3, span 2, or 1 / -1 for "all of it"
grid-areaA named area from grid-template-areas, or the four-line shorthand
justify-self / align-selfOverrides the container's justify-items / align-items for one item
grid-auto-flow: denseBackfills 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

SymptomCauseFix
A flex item overflows its container and won't shrinkItems can't shrink below their content's minimum sizemin-width: 0 on the item (or overflow: hidden)
Equal flex columns aren't equalflex: 1 vs flex: auto: 1 sets basis 0% (ignore content), auto keeps content sizeUse flex: 1 when you want truly equal widths
A grid column grows wider than its 1fr share1fr really means minmax(auto, 1fr) — content can force it widerWrite minmax(0, 1fr) instead of 1fr
align-content does nothingIt only distributes lines — with one line of flex items, or one row of grid tracks, there's nothing to distributeUse align-items for a single line; align-content needs wrapping / multiple rows
space-between leaves no gap at the edgesThat's what it does — first item flush left, last flush rightUse space-evenly (equal gaps everywhere) or add gap + justify-content: center
Percentage heights inside a grid row do nothingPercentages resolve against a definite height — auto rows don't have oneGive the container a height (min-height: 100vh) or use 1fr rows
gap in Flexbox doesn't work in an old browsergap for Flexbox landed in browsers in 2021 (Safari 14.1+)Use margins as a fallback if you still support very old Safari

Keep going