Sub topic 3
The subgrid keyword
This chapter assumes you know the following:
- Basic CSS selectors and the box model.
- CSS Grid fundamentals:
display: grid,grid-template-columnsandgrid-template-rows,frunits andgap. - Placing grid items with
grid-columnandgrid-row, includingspanand line numbers.
What the keyword does
subgrid is a value for grid-template-columns and grid-template-rows. Normally you give those properties a list of track sizes. With subgrid, you tell the browser to use the parent grid's tracks on that axis instead of creating new ones.
The result is a nested grid whose rows or columns line up with the parent's. This fixes the usual problem with nested grids, where each one sizes its tracks independently.
Three conditions
A subgrid works only when all of these hold:
- The element is a direct child of a grid container, so it is a grid item.
- The element is itself a grid container (
display: gridorinline-grid). - The element spans the parent tracks it should borrow, using
grid-roworgrid-column.
A worked example: card sections
Each card below borrows three rows from the parent grid. The first row holds the title, the second holds the text and the third holds the link.
.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;
}<div class="cards">
<article class="card">
<h2>Short title</h2>
<p>Brief text.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h2>A much longer title that wraps onto two lines</h2>
<p>Longer body text that takes more room than the first card.</p>
<a href="#">Read more</a>
</article>
</div>.card is a grid item of .cards, and grid-row: span 3 makes it occupy three parent rows. grid-template-rows: subgrid then hands those three rows to the card's own grid. The card has three direct children, so the h2, p and a fill the rows in order.
Cards that sit side by side share the same three rows. If one title wraps to two lines, the first row grows for every card in that band, so the paragraphs and links stay aligned.
The row-gap on .card overrides the parent's gap inside the card. Gaps get their own chapter, so treat that line as a detail for now.
Only the row axis is a subgrid here. The card's columns are not defined, so it has one implicit column. Choosing axes is covered in a later chapter.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Children stack as normal blocks | The item is missing `display: grid` | Add `display: grid` to the item |
| Tracks are not shared | A wrapper element sits between the parent grid and the item, so the item is not a grid item | Make the item a direct child, or turn the wrapper into the subgrid |
| Cards stay misaligned | The item spans one track, so the subgrid has only one | Add `grid-row: span 3`, or the correct count |
| Extra children sit outside the aligned rows | The item has more children than spanned tracks | Span as many tracks as there are children |
Check your understanding
Exercise
A .card has display: grid, grid-template-rows: subgrid and four direct children, but its rule says only grid-row: span 3. What happens to the fourth child, and how do you fix it?
Show answer
The subgrid has only three inherited rows, so the fourth child goes into an extra implicit row inside the card. That row is sized by the card alone, so it is not shared with neighboring cards. Fix it by spanning four rows with grid-row: span 4, so every child gets a parent track.