Sub topic 15
Nested grids and subgrid
This chapter assumes you can define explicit tracks with grid-template-columns and grid-template-rows, use fr, repeat() and gap, and place items by line number or span. If those are new, review the earlier chapters first.
What a nested grid is
A grid item is not limited to holding plain content. Any grid item can also be a grid container, by setting display: grid on it. The inner grid is called a nested grid.
The key rule is that a nested grid is independent. Its tracks, gaps and line numbers belong only to itself. The outer grid sees the nested container as a single item that sits in one cell or spans several.
.page {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 24px;
}
.panel {
display: grid; /* .panel is an item of .page and a container itself */
grid-template-columns: repeat(2, 1fr);
gap: 8px;
}Only direct children of a grid container become grid items. Grandchildren are not placed by the outer grid. Nesting is how you give them a grid of their own.
<div class="page">
<section class="panel">
<h2>Stats</h2>
<p>Users</p>
<p>Orders</p>
</section>
<aside>Notes</aside>
</div>Here section and aside are items of .page. The h2 and the two p elements are items of .panel, and .page knows nothing about them.
The alignment problem with nested grids
Independence is a strength, but it causes a common layout problem. Imagine a row of cards. Each card has a title, a body and a footer button. You want the titles, bodies and buttons to line up across cards, even when one card has a long title.
If each card is its own nested grid, each card sizes its rows from its own content. A card with a two-line title gets a taller first row than its neighbor. The result is misaligned bodies and footers.
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
.card {
display: grid;
grid-template-rows: auto 1fr auto; /* sized per card, not shared */
}Before subgrid, the usual workarounds were fixed heights, JavaScript measurement, or flattening the markup so all elements are direct children of one grid. Each has costs. Fixed heights break with variable content, JavaScript adds complexity, and flattening damages semantic structure.
Subgrid: inheriting the parent's tracks
The subgrid value tells a nested grid to use the tracks of its parent grid instead of defining its own. You use it as the value of grid-template-columns, grid-template-rows, or both.
For the card example, each card should span three rows of the parent grid and borrow those rows.
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr auto; /* title, body, footer rows shared by all cards */
gap: 0 16px;
}
.card {
grid-row: span 3; /* occupy all three parent rows */
display: grid;
grid-template-rows: subgrid; /* use those three rows as my own */
}Now the title row is as tall as the tallest title across all cards. The same holds for the body and footer rows, so the elements line up.
<div class="cards">
<article class="card">
<h3>Short title</h3>
<p>Body text.</p>
<a href="#">Read</a>
</article>
<article class="card">
<h3>A much longer title that wraps onto two lines</h3>
<p>More body text that makes this card taller than the others.</p>
<a href="#">Read</a>
</article>
<article class="card">
<h3>Medium title</h3>
<p>Body.</p>
<a href="#">Read</a>
</article>
</div>How subgrid works internally
Several details explain what you see.
- The subgrid spans a fixed area of the parent. The number of tracks it inherits equals the number of parent tracks it spans. A card with
grid-row: span 3inherits three rows. If you span two, you inherit two. - Its children are placed on the inherited lines. Line numbers inside the subgrid start at 1 for the first inherited line. They are not the parent's numbers.
- Its items contribute to parent track sizing. This is the point of subgrid. Content in the subgrid influences how large the parent's tracks become.
- Subgrid on one axis is allowed. You can write
grid-template-columns: subgridand define your own rows, or the reverse.
A common pattern is to inherit columns only, so a nested form lines its labels and inputs up with the page columns.
.page {
display: grid;
grid-template-columns: [full-start] 1fr [content-start] minmax(0, 60ch) [content-end] 1fr [full-end];
}
.form {
grid-column: full; /* span all columns */
display: grid;
grid-template-columns: subgrid; /* reuse page columns */
}
.form > label,
.form > input {
grid-column: content; /* named lines are inherited */
}Named lines defined on the parent are available inside the subgrid, which is why grid-column: content works. The full and content names resolve because [full-start]/[full-end] and [content-start]/[content-end] define implicit named areas.
Gaps and subgrid
A subgrid inherits the parent's gap by default. You can override it by setting gap, row-gap or column-gap on the subgrid. The override changes the spacing between the subgrid's own items. The subgrid's edges still align with the parent's lines, and the extra or reduced space is absorbed at the edges of the subgrid's tracks.
.card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3;
row-gap: 4px; /* tighter spacing inside the card than between cards */
}Padding on a subgrid also works. Padding, border and margin on the subgrid container act like extra space at its edges, and parent tracks are sized to include them. This lets a card have inner padding while its content still aligns with its neighbors.
Common errors
- Using
subgridwithout a grid parent. The element's parent must itself be a grid container. If it is not,subgridbehaves likenone. - Forgetting to span. If a card occupies one parent row,
grid-template-rows: subgridgives it one row, so the card's three children do not get three rows. They fall into extra implicit rows. Setgrid-row: span 3. - Using
subgridinsiderepeat()orminmax().subgridis a keyword that replaces the whole track list. You cannot combine it with other track sizes in the same property, apart from optional line names after it. - Expecting auto-placed parent rows to appear. The parent needs explicit rows, or enough implicit rows, for the span to make sense. Define them with
grid-template-rowsorgrid-auto-rows. - Assuming support everywhere. Subgrid has been in current Firefox, Safari and Chromium-based browsers for several years, but older browsers ignore it. Use
@supportsto provide a fallback.
.card {
display: grid;
grid-template-rows: auto 1fr auto; /* fallback: independent rows */
}
@supports (grid-template-rows: subgrid) {
.card {
grid-row: span 3;
grid-template-rows: subgrid;
}
}Choosing between a plain nested grid and subgrid
| Situation | Better choice |
|---|---|
| The inner layout is self-contained, such as an icon and text inside a button | Plain nested grid |
| Inner elements must align with elements in sibling items | Subgrid |
| Inner elements must align with the page's main columns | Subgrid on columns |
| The inner grid needs different track sizes than the parent | Plain nested grid |
Subgrid couples the inner layout to the outer one. If the parent's tracks change, the nested content moves too. Use it when that coupling is the goal, and use a plain nested grid when you want isolation.
Exercise
Three .card elements are items of a grid with grid-template-rows: auto 1fr auto. Each card contains an h3, a p and an a. You set .card { display: grid; grid-template-rows: subgrid; } and the parts still do not line up across cards. What is missing, and why?
Show answer
Each card needs grid-row: span 3. Without it, a card occupies only one parent row, so subgrid gives it a single inherited row and the three children fall into implicit rows that are not shared. Spanning three rows makes the card inherit all three parent rows, so the titles, bodies and links align across cards.
Exercise
A nested grid defines grid-template-columns: repeat(2, 1fr). Can the outer grid's line numbers be used to place the nested grid's children? What changes if you use grid-template-columns: subgrid instead?
Show answer
No. A plain nested grid has its own lines, so its children are placed with its own line numbers, and the outer grid does not see them. With subgrid, the children are placed on the inherited lines, numbered from 1 within the subgrid's span, and they also affect the sizing of the parent's tracks.