Sub topic 4
How a Subgrid Borrows Its Parent's Tracks
This chapter assumes you know display: grid, grid-template-columns and grid-template-rows, fr units, gap, and how to place items with grid-column and grid-row using line numbers and span. It covers what happens to the tracks of a grid item once you set a track list to subgrid. The syntax itself was introduced in the previous chapter, so here the focus is on the mechanism: which tracks the subgrid receives, how many, and what that means for the items inside it.
What "inheriting" means
A normal nested grid defines its own tracks. It sizes them from its own content and its own width or height, and it knows nothing about the grid that contains it.
A subgrid does not define tracks in the axis where you write subgrid. Instead, it uses the tracks of its parent grid that lie under the subgrid's own grid area. The parent keeps ownership. The subgrid's children are laid out on the parent's tracks, as if they were placed on the parent directly, but they still belong to the subgrid for everything else: they inherit from it, and the subgrid's own padding, border, and margin affect where they sit.
Three facts follow from this and drive the rest of the chapter:
- The subgrid must itself be a grid item of the parent grid. A grid nested deeper in the DOM, inside a plain
div, has no parent grid to borrow from. - The number of tracks the subgrid receives is decided by how many parent tracks its grid area spans.
- Sizing flows upward as well as downward. The subgrid's content takes part in sizing the parent's tracks.
The tracks you receive
Consider a parent with four columns and a subgrid item placed across the middle two.
.parent {
display: grid;
grid-template-columns: 100px 1fr 2fr 100px;
}
.child {
grid-column: 2 / 4;
display: grid;
grid-template-columns: subgrid;
}The .child element covers parent columns 2 and 3. Its column axis therefore contains exactly two tracks: the 1fr track and the 2fr track. Its first column is the parent's second column, and its second column is the parent's third column. The 100px columns lie outside it and are not part of it.
You can confirm the count by placing children in the subgrid:
<div class="parent">
<div class="child">
<span>A</span>
<span>B</span>
<span>C</span>
</div>
</div>Item A goes in the first subgrid column, which is parent column 2. Item B goes in the second, which is parent column 3. Item C has no third column to go into, so auto-placement creates an implicit column track. That implicit track is not borrowed from the parent. It is added to the subgrid and sized from its content, and it sits beyond the area the subgrid spans, which usually causes overflow or odd layout. When this happens, the usual fix is to span more parent tracks or reduce the number of children per row.
Track count comes from the span, not from the content
The subgrid's track count is fixed by its position and span in the parent. If you change the placement, you change the tracks it inherits.
| Placement of the subgrid | Parent tracks inherited |
|---|---|
| `grid-column: 2 / 4` | Columns 2 and 3 (two tracks) |
| `grid-column: 1 / -1` | All explicit columns |
| `grid-column: span 3` | Three tracks, starting where auto-placement puts it |
| `grid-row: span 3` | Three rows starting at its row |
The card example from the topic relies on the last row of this table:
.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;
}Each .card covers three parent rows, so it receives three row tracks. The card has three direct children (a title, a text paragraph and a link), and each lands in one of those inherited rows. When two cards sit side by side, they share the same three parent rows. The title row has the height of the tallest title in that line of cards, so every body starts at the same height. The footer links line up for the same reason.
The parent has no grid-template-rows here. Its rows are implicit, and grid-row: span 3 makes the card create three of them. The subgrid works with implicit tracks as well as explicit ones.
Spanning more than the children need
A subgrid can span more tracks than it has children. The extra tracks are empty in the subgrid, but they still exist in the parent and are still shared. If the card above spanned four rows but had three children, the fourth row would hold nothing from this card, though other cards in the same band might use it.
Spanning fewer tracks than children is the problem case. Children auto-place into the subgrid's tracks in order. Once those run out, extra children create implicit tracks inside the subgrid, as described earlier.
What stays under the subgrid's control
The subgrid is still a box with its own properties. Several things remain local to it:
- Its own
padding,border, andmarginstay. The subgrid's edges are inset for its children, so a child in the first inherited column starts after the subgrid's padding, not at the parent track's edge. - The properties of its children, such as
justify-self,align-self, andgrid-columnplacement, are interpreted against the subgrid's tracks. - Any axis where you did not write
subgridis an ordinary track list that the subgrid defines itself.
The padding point matters in practice. The track widths are still the parent's, but the subgrid effectively adds its margin, border, and padding to the tracks at its start and end edges. That extra space is counted when the parent sizes those edge tracks, which is covered later in this tutorial under sizing contribution.
Local line numbers
Inside the subgrid, line numbers restart at 1. The subgrid's first inherited line is line 1, no matter where it falls in the parent. For the earlier .child that spans parent columns 2 to 3, the subgrid has three column lines: 1, 2 and 3 (and -1 is the last). Those correspond to parent lines 2, 3 and 4.
.child > .wide {
grid-column: 1 / -1; /* both inherited columns */
}
.child > .second {
grid-column: 2; /* parent column 3 */
}A child with grid-column: 1 / -1 covers both inherited columns, which are parent columns 2 and 3, not the whole parent. Using parent line numbers inside a subgrid is a common mistake, and it produces implicit tracks beyond the area.
Inherited tracks and `repeat()`
Because the tracks come from the parent, the subgrid cannot use repeat() with a count to define them, and it cannot use minmax() or fr values for those axes. The only value that makes it inherit is subgrid, optionally followed by a list of line names. Mixing sizes and subgrid in the same declaration is invalid, and the browser discards the entire declaration.
/* Invalid: sizes cannot follow the subgrid keyword */
.child {
grid-template-columns: subgrid 1fr 1fr;
}When a declaration is discarded, the property falls back to its initial value, none, and the element becomes a plain grid with no explicit columns. This makes invalid subgrid code look like a layout that has simply lost its alignment, which is an easy bug to miss. Check the DevTools grid overlay if the inheritance seems not to happen.
Checking what you received
Most browsers' DevTools can display the grid overlay for both the parent and the subgrid. When you turn on both, the subgrid's lines match the parent's lines exactly. A quick checklist when the result looks wrong:
- Is the element a direct child of the grid container? If not, there is nothing to inherit from.
- Is
display: gridset on the element in addition to thesubgridvalue? Without it, the track list has no effect. - Does the span cover the number of tracks you expect? Check
grid-roworgrid-columnon the subgrid. - Are there more children than inherited tracks? If so, implicit tracks are being added.
- Are the track lists valid? Remove anything that follows the
subgridkeyword other than line names.
Exercise
A parent grid has grid-template-columns: repeat(6, 1fr). A child sets grid-column: 3 / 6, display: grid, and grid-template-columns: subgrid. It contains four items with no explicit placement. How many tracks does the subgrid receive in the column axis, which parent columns are they, and what happens to the fourth item?
Show answer
The child spans lines 3 to 6, so it covers three parent columns: columns 3, 4 and 5. The subgrid receives exactly those three tracks. The first three items fill them in order. The fourth item has no inherited track, so auto-placement wraps it onto a new row in the subgrid's first column, because the default flow is row. If the subgrid also needed a fourth column in the same row, an implicit column would be created, sized from its content and not borrowed from the parent.
Exercise
A card uses grid-row: span 2 with grid-template-rows: subgrid, but it has three direct children. What goes wrong, and how do you fix it?
Show answer
The card inherits only two parent rows, so the third child creates an implicit row inside the card. That row is sized by the card's own content and does not line up with other cards. Fix it by changing the span to span 3, so the card inherits one row for each child.