CSS Grid Cheat Sheet: Templates, fr, minmax, Areas and Subgrid
Checked against CSS Grid CSS Grid Level 2 (W3C CR Draft, 26 Mar 2025) and MDN reference on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who can write display: grid but keep second-guessing auto-fill, line numbers and what happens to items that don't fit the template. It follows the W3C CSS Grid Layout Level 2 draft dated 26 March 2025, which adds subgrid to Level 1, plus the MDN reference pages read on 28 November 2026. Rows were checked against the docs; no browser rendering was run.
When to use what
Use grid when you care about both directions at once: a page shell, a dashboard, a gallery where columns must line up across rows. For a single strip of things that size themselves, flexbox is less code (see the flexbox sheet and our grid or flexbox write-up).
Define columns with fr for flexible space and fixed lengths for things that must not move, like a 16rem sidebar. Be aware that a bare 1fr is really minmax(auto, 1fr), so a column will not go narrower than its content. When a wide table or long word blows a column out, write minmax(0, 1fr) instead.
For responsive cards without media queries, repeat(auto-fit, minmax(15rem, 1fr)) is the line most people want. Choose auto-fit when a short row should stretch to fill the width, and auto-fill when you want cards to keep their size and leave empty tracks at the end.
Placement has three styles. Named areas read like a picture of the layout and are easiest to change at breakpoints. Line numbers are precise and suit one-off spans such as 1 / -1 for a full-width row. Auto-placement handles the rest. Use subgrid when nested elements (card titles, prices, buttons) must line up with their neighbours; it has been Baseline since September 2023.
The CSS grid generator and the grid template generator let you draw a layout and copy the CSS.
Only one auto-repeat (auto-fill or auto-fit) is allowed per track list, and its track size must be fixed, though minmax(10rem, 1fr) qualifies because the minimum is a length.
grid-template-columns: 16rem 1frFixed sidebar plus a main column that takes the rest
grid-template-columns: repeat(3, 1fr)Three equal columns; shorter than writing 1fr 1fr 1fr
grid-template-columns: repeat(3, minmax(0, 1fr))Three equal columns that are allowed to shrink below their content width
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr))As many 12rem-plus columns as fit; empty tracks are kept at the end of a short row
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr))Same, but empty tracks collapse so the items present stretch across the row
gap: 1rem 2remRow gap then column gap between tracks; never at the outer edges
Every string must have the same number of cells, and each named area must be a rectangle. An L-shaped area makes the whole declaration invalid, so it is silently ignored.
grid-template-areas: "head head" "nav main" ". foot"Draw the layout as strings: one string per row, one word per cell
.A null cell token: an unnamed, empty cell (any run of dots works)
header { grid-area: head; }Put an item into the area named head
grid-row: head-start / foot-endUse the implicit -start and -end line names that every named area creates
Lines count from 1 at the start edge; negative numbers count back from the end of the explicit grid, with -1 the last line. Line 0 is invalid.
grid-column: 1 / 3Start at line 1 and end at line 3, so the item spans two columns
grid-column: 1 / -1Stretch across every explicit column, the usual full-width row
grid-column: span 2Span two tracks from wherever auto-placement puts the item
grid-column: span 2 / 7End at line 7 and reach back two tracks
grid-area: 1 / 1 / 3 / 3Shorthand for row-start / column-start / row-end / column-end
grid-auto-rows and grid-auto-columns default to auto; grid-auto-flow defaults to row.
grid-auto-rows: minmax(100px, auto)Size rows the template didn't define: at least 100px, taller if content needs it
grid-auto-flow: columnFill each column before starting the next, adding columns as needed
grid-auto-flow: row denseBackfill earlier holes with later small items; visual order can then differ from DOM order
place-items: centerSet align-items and justify-items to centre every item in its cell
A subgridded axis cannot create implicit tracks: extra items pile into the last track. Line numbers restart at 1 inside the subgrid.
.card { display: grid; grid-row: span 3; grid-template-rows: subgrid; }Let a card's three children share the parent's row tracks so titles and buttons line up across cards
grid-template-columns: subgridReuse the parent's column tracks for the area this item spans
grid-template-columns: subgrid [a] [b]Add local line names on top of the ones inherited from the parent
row-gap: 0Override the gap the subgrid inherits from its parent
Worked example: Cards whose titles, text and buttons line up across a row
Card heights differ because titles wrap differently. You want each card's button on the same line as its neighbours.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
gap: 1.5rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}
/* each .card holds an h3, a p and a footer */
The outer grid builds as many 15rem-or-wider columns as fit and, because of auto-fit, stretches the cards to fill a short last row. Each card asks for three rows of the outer grid with span 3 and then declares subgrid on its rows, so its h3, p and footer sit in tracks shared with every other card in the same outer row. The tallest title in that row sets the height of the title track for all of them, and the buttons therefore line up. The card overrides the inherited gap with row-gap: 0.5rem so spacing inside a card is tighter than between cards. If a card had a fourth child, it would land in the last track rather than adding a row, because a subgridded axis has no implicit tracks.
Common mistakes
A long URL or wide table makes a
1frcolumn wider than its siblings.1frhas an automatic minimum. Writeminmax(0, 1fr)so the column can shrink and let the content scroll or wrap.Using
auto-filland wondering why three cards don't stretch across a wide screen.auto-fillkeeps empty tracks. Switch toauto-fitso empty tracks collapse.Writing an L-shaped area in
grid-template-areasand getting no layout at all.Areas must be rectangles; the whole property is dropped otherwise. Split the shape into two areas.
Expecting
grid-column: 1 / -1to span implicit columns too.-1 is the last line of the explicit grid. Define the columns in
grid-template-columnsif the item must reach them.Turning on
grid-auto-flow: densefor a list of links or form fields.Dense packing reorders things visually but not for keyboard users. Keep it for decorative galleries only.
Adding more children to a subgrid than it has tracks.
A subgridded axis can't grow. Increase the span on the parent, or drop subgrid on that axis and use
grid-auto-rows.
Related cheat sheets
Sources
- https://www.w3.org/TR/css-grid-2/
- https://developer.mozilla.org/en-US/docs/Web/CSS/repeat
- https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas
- https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column
- https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow
- https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-rows
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_grid_layout/Subgrid
- https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.