CSS Reference ยท Grid
The subgrid value
Reference for the CSS subgrid keyword: let a nested grid reuse its parent's rows or columns so items line up across cards, with syntax, examples and notes.
subgrid is a value for grid-template-columns and grid-template-rows. It tells a nested grid not to create its own tracks but to use the tracks of its parent grid in that direction. The children of the subgrid then line up with the outer grid, even though they sit one element deeper in the HTML.
The classic use is a row of cards where the title, body and button of every card must align across all cards, without fixed heights.
.card { display: grid; grid-row: span 3; grid-template-rows: subgrid; }
Try it
All three buttons sit on the same line, because the three cards share the same three row tracks.
Syntax
grid-template-columns: subgrid; grid-template-rows: subgrid; grid-template-columns: subgrid [line-name] [other-name];
- Initial value: none (the keyword is a value of the template properties, whose initial value is
none) - Applies to: grid containers that are themselves grid items of another grid
- Inherited: no
- Animatable: no (switching to or from
subgridis discrete) - Computed value:
subgridwith the list of line names
Values
| Syntax and arguments | Description |
|---|---|
subgrid |
Use the parent's tracks in this axis. The item must span exactly the tracks it wants to reuse. |
subgrid [name] ... |
Give names to the shared lines, applying only inside the subgrid. Names listed are matched to lines in order. |
gap on the subgrid |
Optional: a gap set on the subgrid adds to the shared tracks' spacing; by default it inherits the parent's gap. |
Usage notes
- Both ends must be a grid. The subgrid element needs
display: gridand must itself be a direct grid item of the parent. - Spanning is required.
grid-row: span 3makes the card occupy three parent rows, andsubgridmaps its three children onto them. - Tracks come from the parent. The subgrid cannot size them; it only contributes its content's size to the parent's track sizing, which is why the middle row grows to fit the tallest card.
- One axis or both. You can use
subgridfor rows only, columns only, or both. - Margins and padding on the subgrid are added to the shared tracks' edges, so tracks still line up.
- Implicit tracks can't be created by a subgrid: its children must fit into the lines it spans.
Browser support
Supported in all current major browsers (Firefox 71, Safari 16, Chrome and Edge 117), and part of Baseline since late 2023.
Related
grid-template-columns- the property that acceptssubgridgap- spacing between tracksgrid-row-start- spanning rowsmasonry layout- a different approach to uneven grids- Lesson: Subgrid