Sub topic 12
Nested Grids and Subgrid
This chapter assumes you can write a 12-column grid with repeat(), fr units, and gap, and that you can place items with grid-column. Here you will see why a grid inside a grid usually drifts out of alignment, and how subgrid fixes that.
Why nest a grid at all
A page grid gives the whole layout one set of column lines. Sooner or later an item placed in that grid needs its own internal layout. An article might hold a figure and a block of text. A card might hold an image, a heading, and a button.
The obvious move is to make that item a grid too. This works, but the new grid is independent of the page grid. It does not know where the parent's column lines are.
The problem with an independent nested grid
Suppose the page grid has twelve columns and a 1.5rem gutter, and an article spans all of them. You make the article a grid and give it twelve columns of its own.
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.article {
grid-column: 1 / -1;
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}With no padding on .article, its columns happen to match the parent's, because the widths and gaps are identical. That match is fragile. It breaks when any of these change:
- The nested element spans fewer than twelve columns, so its twelve columns now fit into a narrower space.
- The nested element gets padding or a border, which shrinks its content box.
- You change the gutter in one rule and forget the other.
- The parent uses a different track definition, such as
auto-fitcolumns.
The tracks inside the nested grid are sized from the nested element's own width. The parent's tracks play no part in it.
How subgrid works
subgrid is a value for grid-template-columns (and grid-template-rows). It tells a grid item that is itself a grid: "do not create your own tracks on this axis. Use the parent's tracks that I span."
Here is the markup and CSS from this tutorial's running example.
<div class="page">
<div class="grid">
<article class="article">
<figure class="figure">Figure</figure>
<div class="text">Text</div>
</article>
</div>
</div>.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 happen here:
.articlespans all twelve parent columns withgrid-column: 1 / -1.grid-template-columns: subgridmakes the article's column tracks the same twelve tracks.- The children use ordinary line numbers. Line 1 of the article is line 1 of the page grid, so the figure covers columns 1 to 4 and the text covers columns 5 to 12.
The gutters come along too. By default a subgrid inherits the parent's column-gap, so you do not repeat it.
What the subgrid inherits and what it can override
The subgrid takes the parent's track sizes and the line positions of the tracks it spans. It also takes the parent's gap by default. You can still set a different gap on the subgrid. The tracks stay where they are, and the extra or reduced space is absorbed by the subgrid's edges.
Padding on the subgrid is also allowed. The padding is applied between the subgrid's edge and its content, and the subgrid's tracks are adjusted to account for it. This is a useful difference from the independent nested grid, where padding would shift all your columns.
Subgrid over a partial span
A subgrid does not need to span the whole parent. If it spans columns 3 to 10, it receives those eight tracks, numbered 1 to 8 inside the subgrid.
.article {
grid-column: 3 / span 8;
display: grid;
grid-template-columns: subgrid;
}
.article .figure {
grid-column: 1 / span 3;
}
.article .text {
grid-column: 4 / -1;
}Here the figure sits on parent columns 3 to 5, and the text on columns 6 to 10. Line numbers inside the subgrid are relative to the subgrid, not the page. Counting from the wrong origin is the most common mistake with partial spans.
Subgrid on rows
Subgrid works on the row axis as well. This solves a classic card problem: cards in one row each contain a heading, a paragraph, and a button, and you want the headings, paragraphs, and buttons to line up across cards, even when text lengths differ.
<div class="cards">
<article class="card">
<h3>Short title</h3>
<p>A brief description.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h3>A much longer title that wraps onto two lines</h3>
<p>A longer description that takes several lines of text in the card.</p>
<a href="#">Read more</a>
</article>
</div>.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
grid-template-rows: auto;
gap: 1.5rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
gap: 0.5rem;
}Each card spans three row tracks of the parent and takes them as its own. The parent creates implicit rows sized to the tallest content in each row, so all headings share one row height, all paragraphs share another, and all buttons share a third.
Subgrid and the cross-axis
You can use subgrid on one axis and a normal definition on the other.
.article {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid;
grid-template-rows: auto auto;
}The columns follow the page grid. The rows are the article's own. This is the common case for article layouts, because columns need to match the page, while rows belong to the article's content.
Browser support and fallbacks
Subgrid is supported in current versions of Firefox, Chrome, Edge, and Safari. Older browsers ignore grid-template-columns: subgrid as an invalid value. The element then has a single implicit column, so its children stack.
Write a fallback first and wrap the subgrid version in @supports.
.article {
grid-column: 1 / -1;
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
@supports (grid-template-columns: subgrid) {
.article {
grid-template-columns: subgrid;
}
}The fallback is an independent twelve-column grid. It is correct as long as the article spans the full width and has no padding, which is the case here.
When to use subgrid and when not to
| Situation | Better choice |
|---|---|
| Children must sit on the page's column lines | `subgrid` |
| Aligning parts of sibling cards across a row | `subgrid` on rows |
| A component with its own small layout, such as a button group | Independent nested grid or flexbox |
| A component reused on pages with different grids | Independent nested grid |
| Nested content spans only part of the width and needs its own proportions | Independent nested grid |
Subgrid couples a component to its parent. That coupling is the point when alignment matters, and a cost when you want a component to work anywhere.
Common errors
- Using subgrid on an element that is not a grid item. Subgrid only works when the element is a direct child of a grid container and is itself
display: grid. If the parent is a flex container or a plain block, the value is invalid. - Forgetting
display: gridon the nested element.grid-template-columns: subgridhas no effect without it. - Counting lines from the page origin. Inside a subgrid, line 1 is the first line the subgrid spans.
- Expecting subgrid to change the parent's tracks. Subgrid items can contribute to the size of shared rows in the parent, but they cannot define new columns.
- Putting a grandchild directly on the page grid. Only direct children of the subgrid take part. A deeper element needs to be a subgrid as well.
Exercise
A 12-column page grid has an .article that uses grid-column: 3 / span 8 and grid-template-columns: subgrid. Inside it, you want a .figure to cover the first three columns of the article. Which grid-column value do you give .figure, and which page columns does it end up on?
Show answer
Use grid-column: 1 / span 3. Line numbers inside a subgrid start at 1 at the subgrid's first spanned line, so the figure sits on page columns 3, 4, and 5.
Exercise
You nest a twelve-column grid inside a card using repeat(12, 1fr) and the card's content is not aligned with the page columns. Name two causes, and the one-line change that fixes both.
Show answer
The card is narrower than the page grid, or it has padding, so its twelve tracks are sized from its own content box. Change the card's grid-template-columns to subgrid (and make it span the right parent columns with grid-column) so it uses the parent's tracks.