Skip to content

Menu

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 1fr

Fixed sidebar plus a main column that takes the rest

Try it: CSS Grid GeneratorFrom the docs, not run by us
grid-template-columns: repeat(3, 1fr)

Three equal columns; shorter than writing 1fr 1fr 1fr

From the docs, not run by us
grid-template-columns: repeat(3, minmax(0, 1fr))

Three equal columns that are allowed to shrink below their content width

From the docs, not run by us
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

From the docs, not run by us
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr))

Same, but empty tracks collapse so the items present stretch across the row

Try it: grid-template-generatorFrom the docs, not run by us
gap: 1rem 2rem

Row gap then column gap between tracks; never at the outer edges

From the docs, not run by us

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

Try it: grid-template-generatorFrom the docs, not run by us
.

A null cell token: an unnamed, empty cell (any run of dots works)

From the docs, not run by us
header { grid-area: head; }

Put an item into the area named head

From the docs, not run by us
grid-row: head-start / foot-end

Use the implicit -start and -end line names that every named area creates

From the docs, not run by us

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 / 3

Start at line 1 and end at line 3, so the item spans two columns

From the docs, not run by us
grid-column: 1 / -1

Stretch across every explicit column, the usual full-width row

From the docs, not run by us
grid-column: span 2

Span two tracks from wherever auto-placement puts the item

From the docs, not run by us
grid-column: span 2 / 7

End at line 7 and reach back two tracks

From the docs, not run by us
grid-area: 1 / 1 / 3 / 3

Shorthand for row-start / column-start / row-end / column-end

From the docs, not run by us

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

From the docs, not run by us
grid-auto-flow: column

Fill each column before starting the next, adding columns as needed

From the docs, not run by us
grid-auto-flow: row dense

Backfill earlier holes with later small items; visual order can then differ from DOM order

From the docs, not run by us
place-items: center

Set align-items and justify-items to centre every item in its cell

From the docs, not run by us

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

From the docs, not run by us
grid-template-columns: subgrid

Reuse the parent's column tracks for the area this item spans

From the docs, not run by us
grid-template-columns: subgrid [a] [b]

Add local line names on top of the ones inherited from the parent

From the docs, not run by us
row-gap: 0

Override the gap the subgrid inherits from its parent

From the docs, not run by us

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 1fr column wider than its siblings.

    1fr has an automatic minimum. Write minmax(0, 1fr) so the column can shrink and let the content scroll or wrap.

  • Using auto-fill and wondering why three cards don't stretch across a wide screen.

    auto-fill keeps empty tracks. Switch to auto-fit so empty tracks collapse.

  • Writing an L-shaped area in grid-template-areas and 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 / -1 to span implicit columns too.

    -1 is the last line of the explicit grid. Define the columns in grid-template-columns if the item must reach them.

  • Turning on grid-auto-flow: dense for 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.