Sub topic 12
Nested grids and subgrid
This chapter assumes you can build a 12-column grid with display: grid, repeat(), and gap, and that you can place items with grid-column. It covers what goes wrong when you put a grid inside a grid, and how subgrid fixes it.
The problem with nesting
A grid item can itself be a grid container. That is a nested grid. By default, the nested grid knows nothing about its parent. It creates its own columns, and its own gap, from scratch.
Suppose an article spans the whole 12-column page grid. You want a figure on the left four columns and the text on the right eight. If you give the article grid-template-columns: repeat(12, 1fr), you get twelve columns, but they are sized from the article's width. They only line up with the page grid when the article has the same width as the parent and the same gutter. Change the gutter, add padding, or let the article span fewer columns, and the two grids drift apart.
Using subgrid
With grid-template-columns: subgrid, the nested element does not create new column tracks. It borrows the tracks of its parent that it spans, including the gutters. Its children then snap to the same lines as the rest of the page.
Here is the markup. It reuses the page grid from earlier chapters and adds an article inside it.
<div class="grid">
<article class="article">
<figure class="figure">Figure</figure>
<div class="text">Text</div>
</article>
</div>And the CSS:
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.article {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid;
}
.article .figure {
grid-column: 1 / span 4;
}
.article .text {
grid-column: 5 / span 8;
}Three things are happening:
.articlespans all twelve columns withgrid-column: 1 / -1.display: gridmakes it a grid container, andsubgridtells it to use the parent's twelve tracks instead of defining its own.- The children use line numbers 1 to 13 of the parent's grid. The figure covers columns 1 to 4, and the text covers columns 5 to 12.
What a subgrid inherits
A subgrid takes the track sizes and the line positions from its parent along the axis where you write subgrid. By default it also inherits the parent's gap on that axis. You can override the gap on the subgrid if the nested content needs different spacing.
Line numbers inside a subgrid start at 1 for the first line the subgrid spans, not for the parent's first line. If the article spans columns 3 to 10, then grid-column: 1 / span 4 inside it means parent columns 3 to 6.
Common errors
- Forgetting
display: grid.grid-template-columns: subgriddoes nothing on an element that is not a grid container. - Using
subgridon an element that is not a grid item. Its parent must be a grid container, otherwisesubgridhas no tracks to borrow. - Expecting padding to disappear. Padding on the subgrid container shifts its content inside the borrowed tracks. Margins and padding can make children no longer line up with the page grid, so keep them at zero where alignment matters.
- Browser support. Subgrid is supported in current versions of the major browsers, but older ones ignore the declaration. Check the support table for the browsers you target. A fallback is to declare a plain
repeat(12, 1fr)first, then override it in@supports (grid-template-columns: subgrid).
.article {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
@supports (grid-template-columns: subgrid) {
.article {
grid-template-columns: subgrid;
}
}Exercise
An article spans parent columns 3 to 10 of a 12-column grid and uses grid-template-columns: subgrid. Which parent columns does a child with grid-column: 1 / span 4 cover?
Show answer
Parent columns 3 to 6. Line numbers inside a subgrid count from the first line the subgrid spans, so its line 1 is the parent's line 3, and spanning four tracks reaches parent column 6.
Exercise
A subgrid is declared on .article, but its children ignore the parent's columns. The CSS sets only grid-template-columns: subgrid on .article. What is missing?
Show answer
display: grid. Without it, .article is not a grid container, so the subgrid declaration has no effect.