Skip to main content

57 entries · 7 sections

CSS Grid Cheat Sheet

CSS Grid from the container down: defining tracks, the sizing functions that make grids responsive without media queries, placing and spanning items, named template areas, alignment, and the differences that trip people up.

Creating a grid

SyntaxWhat it does
display: gridMake the element a block-level grid container.
display: inline-gridA grid container that flows inline with text.
grid-template-columns: 200px 1frA fixed sidebar and a flexible main column.
grid-template-columns: repeat(3, 1fr)Three equal columns.
grid-template-rows: auto 1fr autoHeader, growing body, footer.
gap: 16pxSpace between tracks. Older code calls it grid-gap.
row-gap / column-gapControl the two gaps separately.
grid-auto-flow: columnPlace new items into new columns rather than new rows.
grid-auto-flow: denseBackfill the holes that spanning items leave behind. It can reorder items visually.

Sizing tracks

SyntaxWhat it does
1frOne share of the free space left after fixed tracks and gaps.
2fr 1frTwo columns where the first gets twice the free space of the second.
minmax(200px, 1fr)At least 200px, then flexible up to a share of the free space.
min-contentAs narrow as the content can be without overflowing.
max-contentAs wide as the content wants, with no wrapping.
fit-content(300px)Size to the content, capped at 300px.
autoSize to the content, but stretch to absorb leftover space.
repeat(auto-fill, minmax(200px, 1fr))As many 200px-or-wider columns as fit. Empty tracks are kept.
repeat(auto-fit, minmax(200px, 1fr))The same, but empty tracks collapse so the real items stretch to fill the row.
grid-auto-rows: minmax(100px, auto)The size of rows the browser creates beyond your template.
grid-auto-columns: 1frThe size of implicitly created columns.

Placing items

SyntaxWhat it does
grid-column: 1 / 3Start at line 1 and end at line 3 — a two-column span. Lines, not cells.
grid-column: span 2Span two columns from wherever the item lands.
grid-column: 1 / -1First line to last line: full width, whatever the column count turns out to be.
grid-row: 2 / 4Occupy rows two and three.
grid-area: 1 / 1 / 3 / 3row-start / column-start / row-end / column-end, in that order.
grid-column-start / grid-column-endThe longhand pair, when a shorthand is harder to read.
grid-row: span 2 / autoSpan two rows and let the browser choose where to start.
z-index on a grid itemGrid items are allowed to overlap; z-index decides which is on top.

Named areas and lines

SyntaxWhat it does
grid-template-areas: "head head" "side main"Draw the layout as a picture in the stylesheet.
grid-area: headAssign an item to a named area.
"head head" "side main" "foot foot"The classic three-row application shell.
a . in an area stringA dot leaves that cell deliberately empty.
grid-template: "head head" 60px "side main" 1fr / 200px 1frAreas, row sizes and column sizes in a single declaration.
[main-start] 1fr [main-end]Name the grid lines themselves inside a track list.
grid-column: mainPlace an item between the lines named main-start and main-end.

Alignment

SyntaxWhat it does
justify-items: centerAlign every item inside its own cell along the inline axis.
align-items: centerAlign every item inside its own cell along the block axis.
place-items: centerBoth at once — the shortest genuine centring in CSS.
justify-content: centerPosition the whole grid inside the container when the tracks are narrower than it.
align-content: space-betweenDistribute the rows inside a container that is taller than the grid.
place-content: centerBoth content alignments in one declaration.
justify-self / align-selfOverride the alignment for a single item.
place-self: end startBoth self alignments at once, block axis first.

Recipes

SyntaxWhat it does
display: grid; place-items: center; min-height: 100dvhA perfectly centred, full-height hero.
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr))A responsive card grid with no media queries at all.
grid-template-columns: minmax(0, 1fr)Stops a child with long unbreakable content from blowing the track out.
grid-template-rows: auto 1fr auto; min-height: 100dvhHeader, content and footer filling the viewport, with the footer pinned down.
grid-template-areas: "stack"; children all set grid-area: stackStack children on top of each other without absolute positioning.
grid-column: 1 / -1 on one cardA full-width row inside an otherwise regular card grid.
aspect-ratio: 1 on the itemsSquare cells regardless of what is inside them.

Gotchas

SyntaxWhat it does
1fr is not minmax(0, 1fr)1fr has a min-content floor, which is why a long word can push a track wider than its share.
auto-fill versus auto-fitauto-fill keeps empty tracks so items stay their base width; auto-fit collapses them so items stretch.
grid-gapThe old name for gap. gap is the current one and is supported everywhere.
Ragged grid-template-areasEvery row string must contain the same number of cells or the whole declaration is ignored.
Implicit tracksItems placed past your template create rows sized by grid-auto-rows, not by grid-template-rows.
Margins do not collapseAdjacent margins inside a grid container behave independently, unlike in block layout.
Percentage gapsResolve against the container's own size on that axis, which can surprise you inside a flexible parent.

Worth knowing

  • repeat(auto-fit, minmax(240px, 1fr)) is the single most useful line in CSS Grid: it produces a card gallery that adds and removes columns as the container resizes, with no breakpoints to maintain. Swap auto-fit for auto-fill when you want the leftover columns to stay empty rather than letting the items stretch.
  • Grid lines are numbered from 1, and there is always one more line than there are tracks. grid-column: 1 / 3 therefore spans two columns, not three, and grid-column: 1 / -1 spans the whole row no matter how many columns exist.
  • Firefox DevTools has the best grid inspector of any browser: it overlays line numbers, track sizes and area names directly on the page. When a grid is not doing what you expect, opening that overlay is faster than reasoning about the CSS.

Try a snippet instead of reading about it

Reference is faster when you can run the thing. Paste a snippet into AltF Code, the free in-browser editor — real syntax highlighting for HTML, CSS, JavaScript, Python, SQL, JSON and more, with HTML, CSS and JavaScript running live in the preview. No install and no sign-up.

Open the editor

CSS Grid — common questions

What is the difference between auto-fit and auto-fill?
Both repeat as many tracks as will fit. auto-fill keeps the empty tracks it created, so a single item stays its minimum width with blank space beside it. auto-fit collapses the empty tracks to zero, so the real items stretch to fill the row. For card grids people almost always want auto-fit.
What does the fr unit actually mean?
A fraction of the free space remaining after fixed-size tracks and gaps have been subtracted. In 200px 1fr 2fr, the two flexible tracks split whatever is left one-to-two. Note that 1fr also has an automatic minimum of min-content, so use minmax(0, 1fr) when you need a track that can genuinely shrink.
How do I make a responsive grid without media queries?
Use grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)) with a gap. The browser fits as many 240px-minimum columns as the container allows and stretches them to share the width, so the layout responds to the container rather than to the viewport — which also works inside a sidebar.
Should I use CSS Grid or Flexbox?
Grid when you are describing a two-dimensional structure up front — rows and columns that should line up. Flexbox when you are distributing items along one axis and want the content to drive the sizes. Most real pages use both: grid for the page skeleton, flex inside the components.