Aligning nested grids with subgrid
By default a nested CSS grid defines its own tracks, so its rows and columns do
not line up with the outer grid. The subgrid value makes a nested grid adopt
the parent’s grid lines on a chosen axis, so deeply nested content aligns to one
shared track structure. This reference covers the syntax and inheritance rules.
How it works
Set display: grid on the child, then set grid-template-columns: subgrid
and/or grid-template-rows: subgrid. The child must itself be a grid item; the
parent lines it inherits are exactly the ones its placement spans:
.parent {
display: grid;
grid-template-columns: [a] 1fr [b] 2fr [c] 1fr [d];
gap: 1rem;
}
.child {
grid-column: a / d; /* spans all three parent columns */
display: grid;
grid-template-columns: subgrid; /* adopts a / b / c / d */
}
The child’s columns now align pixel-for-pixel with the parent’s, inheriting the
named lines a–d and the 1rem gap. You can append your own names —
subgrid [start] [mid] — and override column-gap/row-gap if you accept the
resulting misalignment.
Tips and notes
- Subgrid only mirrors the tracks the item spans — placement controls extent.
- Inherited line names let you write
grid-column: a / binside the subgrid. - Both axes can be subgrid; mix subgrid on one axis with normal tracks on the other.
- Provide a non-subgrid fallback for legacy browsers, as it degrades silently.