Sub topic 4
How a subgrid borrows its parent's tracks
This chapter assumes you know display: grid, grid-template-columns, fr units, gap, and placing items with grid-column and grid-row. It also assumes you have seen the subgrid keyword. Here you will learn what the browser does with it.
A subgrid has no tracks of its own
A normal nested grid creates new tracks from its own grid-template-* value. A subgrid does not. When you write subgrid for an axis, the element gives up its own track list in that axis and uses the parent's tracks instead. Its items are laid out on the parent's lines.
Three rules follow from this:
- The element must be a grid item of a grid container, and it must itself have
display: grid. - The number of tracks it gets equals the number of parent tracks the element spans in that axis.
- The size of each track is decided by the parent. The subgrid cannot change it.
A worked example
The parent defines four columns. The .panel element spans the two middle ones and declares them as subgrid.
<div class="page">
<aside>Left</aside>
<section class="panel">
<div>Child A</div>
<div>Child B</div>
</section>
<aside>Right</aside>
</div>.page {
display: grid;
grid-template-columns: 6rem 1fr 1fr 6rem;
gap: 1rem;
}
.panel {
grid-column: 2 / 4;
display: grid;
grid-template-columns: subgrid;
}.panel spans columns 2 and 3, so its column axis has two tracks. Both are 1fr, copied from the parent. "Child A" fills the first inherited track and "Child B" fills the second, because auto-placement works the same way inside a subgrid as in any other grid. If you change the parent to 6rem 2fr 1fr 6rem, the panel's children follow the change. You edit the track list in one place.
What is inherited and what is not
The subgrid takes the parent's track sizes, and normally the parent's gaps too. Gap handling has its own chapter, so only the basic point matters here: the child columns above are separated by the parent's 1rem gap.
The subgrid element is still a box. Its own padding, border and margin are not removed. They sit between the parent's lines and the subgrid's items, so the items inside shift inward. The tracks are the parent's, but the content area is smaller.
Implicit tracks
The subgrid only inherits tracks that exist in the parent. If you place a child outside the spanned range, the subgrid creates extra implicit tracks. Those extra tracks are local to the subgrid and are not shared with the parent. Keep children within the inherited tracks and the layout stays aligned.
Common mistakes
- Forgetting
display: grid.grid-template-columns: subgriddoes nothing on an element that is not a grid container. - Using it on a nested child. The element must be a direct child of the parent grid. A grandchild cannot subgrid the grandparent.
- Expecting a size to apply. Writing
grid-template-columns: subgridand then expecting1fr 2fris meaningless. Change the parent's tracks instead. - Not spanning. An element in a single parent column inherits one track. A subgrid that needs three columns must span three.
Quick reference
| Question | Answer |
|---|---|
| Who decides track sizes? | The parent grid |
| How many tracks does the subgrid get? | The number of parent tracks it spans in that axis |
| Does the subgrid need `display: grid`? | Yes |
| Can it be a grandchild of the grid? | No, it must be a direct grid item |
Exercise
A parent has grid-template-columns: 4rem 1fr 3fr 2rem. A child has grid-column: 2 / 4; display: grid; grid-template-columns: subgrid; and contains three elements. How many columns does the subgrid have, what sizes are they, and where does the third element go?
Show answer
The child spans two parent columns (lines 2 to 4), so it has two tracks: 1fr and 3fr. The first two elements fill those tracks. The third element has no inherited track left, so it goes into a new implicit row, in the first inherited column.