Under The Hood
frontend css CSS Grid Level 2 (subgrid, Chrome 117+/Safari 16+)

CSS Subgrid: Nested Track Alignment & Parent Line Inheritance

Last updated
Prerequisites:
CSS Grid basics (grid-template-columns/rows)
Understanding of grid lines and tracks
  • css
  • subgrid
  • grid
  • layout
  • tracks
  • alignment

Read at your depth

The practical view

Subgrid lets a nested grid inherit the track definitions of its parent instead of defining its own. grid-template-columns: subgrid; makes the child grid use the parent's column tracks, so items inside align exactly with the parent's columns. This is the clean solution to the 'card alignment' problem where inner sections need to line up with outer columns: .grid { display: grid; grid-template-columns: repeat(4, 1fr); } .card { grid-column: span 2; display: grid; grid-template-columns: subgrid; }. The same works for rows with grid-template-rows: subgrid.

The same idea in other frameworks

react equivalent
CSS-in-JS with subgrid: styled-system or inline styles can emit grid-template-columns: subgrid for aligned card internals.

Legacy vs modern

Hard-coded inner tracks vs subgrid inheritance

Repeating fixed track sizes in nested grids drifts from the parent; subgrid ties the inner grid to the parent's actual tracks.

before → after
Nested grid with hard-coded tracks
.card__body { display: grid; grid-template-columns: 1fr 1fr; } /* ❌ OLD: re-invents tracks, drifts from parent */
Subgrid inherits parent tracks
.card__body { display: grid; grid-template-columns: subgrid; } /* ✅ NEW: inherits parent's columns */

Interview gotchas

Context

This separates developers who understand grid layout from those who just know the display: grid syntax — subgrid is the concept that proves real grid knowledge.

The mechanical answer

A nested grid is an independent grid container inside a grid item: it defines its own tracks, so its internal columns have no relationship to the parent's columns. A subgrid (grid-template-columns: subgrid / grid-template-rows: subgrid) is a grid item whose tracks are the parent's tracks — it does not size its own columns/rows; it aliases the parent's track list and participates in the parent's track sizing algorithm. Subgrid matters whenever inner content must align with outer columns/rows that you cannot hard-code: card metadata rows, form label/value columns inside a broader grid, sticky footers, and table-like layouts. Without subgrid you must duplicate track definitions and manually keep them in sync.

Trap

The naive answer is 'subgrid is just a shortcut for nested grids'. It is a fundamentally different mechanism: shared tracks vs independent tracks. Another trap: assuming subgrid works for both axes by default — you must specify grid-template-columns: subgrid and/or grid-template-rows: subgrid explicitly for each axis; a plain display: grid child is not a subgrid. Candidates who explain the track-alias and line-numbering details show real depth.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.