Sub topic 7
Subgrid in One Axis or Both
This chapter assumes you know CSS Grid basics: grid-template-columns, grid-template-rows, fr units, gap, and placing items with grid-column and grid-row. It also assumes you have seen the subgrid keyword and how a subgrid inherits tracks from its parent. Here you will choose which axis, or axes, a nested grid inherits.
Each axis is independent
The subgrid keyword is a value for grid-template-columns and grid-template-rows. You set each property on its own, so a nested grid can do any of these:
- Inherit columns only, and define its own rows.
- Inherit rows only, and define its own columns.
- Inherit both.
An axis without subgrid works like any normal grid. It uses the tracks you declare, or implicit tracks if you declare none.
| Columns | Rows | Result |
|---|---|---|
| `subgrid` | normal value | Aligns with parent columns. Rows are local. |
| normal value | `subgrid` | Aligns with parent rows. Columns are local. |
| `subgrid` | `subgrid` | Aligns with parent columns and rows. |
Rows only: the card pattern
In the card layout, each card only needs to share rows with its neighbors. The card keeps its own single column, so you don't need to inherit columns.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}grid-row: span 3 makes each card occupy three parent rows. grid-template-rows: subgrid turns those three rows into the card's own rows. The title, text and link then line up across cards in the same parent row. No grid-template-columns is set on .card, so its columns stay local, and its children stack in one implicit column.
Columns only: the form pattern
A form needs the opposite. Each field is one row, but every field must share the same label column.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}.field spans both parent columns and inherits them. The label sits in the first column, and the input sits in the second. The max-content column is sized by the widest label in any field, so all inputs start at the same position. Each field's rows stay local, which is correct because every field is a single row.
Both axes
Set both properties when a nested grid must line up in two directions, such as a section inside a page layout that shares the parent's columns and rows.
.page {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: auto auto auto;
gap: 1rem;
}
.panel {
grid-column: 2 / 4;
grid-row: 1 / 3;
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid;
}The .panel spans two columns and two rows, so it inherits exactly two of each. Its children are placed with local line numbers: 1 is the first line of the panel's area, and 3 is the last line in each axis.
Common mistakes
- Setting
subgridon an item that is not a grid container. Adddisplay: gridfirst. - Expecting rows to align when only
grid-template-columns: subgridis set. Each axis needs its own declaration. - Using
subgridon the axis where the parent has no explicit tracks. The subgrid then inherits implicit tracks, which may not be the sizes you expect. - Forgetting that a
grid-row: span 3card needs exactly three children in the row axis. A fourth child creates an extra implicit row that is outside the parent grid.
Exercise
A .field element spans both columns of a form grid. You want its label and input to share the form's two columns, but each field has only one row. Which properties should be subgrid on .field, and why?
Show answer
Only grid-template-columns should be subgrid. The columns must match the parent so labels and inputs align across fields. The rows should stay local, because each field is a single row and does not need to share row sizes with other fields.