Sub topic 15
Nested grids and subgrid
This chapter assumes you can create a grid container, define tracks with grid-template-columns and grid-template-rows, and place items with line numbers and spans.
Nesting a grid inside a grid item
A grid item can also be a grid container. Set display: grid on it and its children are laid out in a new grid. This is a nested grid.
A nested grid is independent. It has its own tracks, its own gap, and its own sizing. Its children do not line up with the outer grid's tracks.
That independence causes a common layout problem. Suppose three cards sit side by side, and each card has a title, a body, and a link. If each card is its own nested grid, the title in one card can be taller than the title in another. The bodies then start at different heights, because each card sized its rows without knowing about the other cards.
Sharing tracks with subgrid
The value subgrid fixes this. A nested grid that uses subgrid has no tracks of its own for that axis. It uses the tracks of its parent grid.
<div class="cards">
<article class="card">
<h3>Short title</h3>
<p>Body text.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h3>A much longer title that wraps onto two lines</h3>
<p>Body text with more words than the first card has.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h3>Medium title</h3>
<p>Body text.</p>
<a href="#">Read more</a>
</article>
</div>.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}Each card spans three rows of the parent. With grid-template-rows: subgrid, the card's three children occupy those three parent rows. The parent sizes each row to fit the tallest content in it. All three titles share one row, all three bodies share the next, and all three links share the last.
What the item inherits
- Gaps. The subgrid uses the parent's gap by default. Set
row-gaporcolumn-gapon the subgrid to change it. The example sets a smaller row gap inside each card. - Line names. Named lines from the parent are available in the subgrid. You can also add names that apply only inside it.
- Contribution to sizing. The subgrid's content and its padding, border, and margin affect the size of the parent tracks it uses.
You can use subgrid on columns, rows, or both. Any axis that does not use subgrid is an ordinary independent grid.
Common errors
- The element is not a grid item.
subgridonly works when the element is a direct child of a grid container. If the parent is not a grid, the value is invalid. - The element does not span tracks. A subgrid gets only the parent tracks it spans. Without
grid-row: span 3, the card covers one row and its children are squeezed into it. - The span does not match the child count. If the card has four children but spans three rows, the extra child creates an implicit track inside the subgrid.
- The element is not also a grid container. The item needs
display: grid, so thesubgridvalue has something to apply to.
Exercise
A card has display: grid and grid-template-rows: subgrid, but its children still do not align with the cards beside it. The card does not set grid-row. What is the likely cause, and what is the fix?
Show answer
Without grid-row: span N, the card occupies only one parent row, so the subgrid has a single track to share among its children. Add grid-row: span N, where N is the number of children that should each get a row, for example grid-row: span 3 for a title, body, and link.