Grandma Marni’s three price boards on Mr. Karim’s rack: set grid-template-rows: subgrid and grid-row on each board, and the rows the browser computes show…
Why it matters
Product galleries, article lists, dashboards: wherever there is a row of cards, lining them up is a real problem. Subgrid answers it declaratively, replacing forced heights, flexbox tricks or JS that measures heights.
The key idea
Subgrid borrows the parent’s lines, it makes no new grid
Common mistakes
Forgot grid-row: span N on the card, the subgrid does not work A subgrid can only use the parent rows its card crosses. Without a span the card takes one row, and title, body and footer cram into it. Write grid-row: span 3 (as many as the card has parts).
Thinking subgrid makes a new grid for the child A subgrid has no track sizes of its own: its tracks and gap belong to the parent. Their sizes are worked out from every card’s content at once. The gap may be overridden with row-gap on the card.
Putting subgrid on the wrong axis (columns vs rows)subgrid works per axis. A card whose rows should line up needs grid-template-rows: subgrid; grid-template-columns: subgrid only borrows the parent’s columns, and a card that takes one column gets one column only.
Try it in this playground
Change grid-template-rows to auto 1fr auto The prices stay at the bottom, but board 2’s words start lower: each board sizes its own rows.
Empty grid-row (remove span 3) Its subgrid has one rack row, and the board’s parts pile up.
With subgrid, give board 1 a long name The rack’s name row grows, and all three boards’ words move down together.
Move .cards row-gap with no row-gap on the boards The space inside the boards changes too: a subgrid carries its rack’s gap.