Sub topic 5
Spanning Tracks and Placing Items Inside a Subgrid
This chapter assumes you know display: grid, grid-template-columns, fr units, gap, and placement with grid-column and grid-row, including span and line numbers. It also assumes you have seen the subgrid keyword and know that a subgrid reuses its parent's tracks. Here you learn how many tracks a subgrid gets, how its internal lines are numbered, and how to place its children correctly.
The span decides how many tracks you get
A subgrid does not define tracks. It borrows them from the parent. The number it borrows equals the number of parent tracks the subgrid item occupies in that axis. Two properties on the subgrid item control that number:
grid-column(orgrid-row) on the subgrid item sets where it sits and how far it spans.grid-template-columns: subgrid(orgrid-template-rows: subgrid) tells it to adopt those tracks.
The spanning declaration is what you write on the item, not on its parent. This is the most common source of confusion.
.panel {
grid-column: 2 / 5; /* occupies parent tracks 2, 3 and 4 */
display: grid;
grid-template-columns: subgrid; /* so the panel has exactly 3 column tracks */
}If you leave out the span, the item occupies one track by default. It then gets a one-track subgrid, which is almost never what you want. If your children seem to ignore the parent's tracks, check the span first.
Spans that depend on auto-placement
You can use a keyword span instead of explicit lines:
.card {
grid-row: span 3;
display: grid;
grid-template-rows: subgrid;
}Here the browser's auto-placement algorithm chooses the start line. The subgrid still gets exactly three tracks, starting wherever the card lands. Local numbering (described next) is always relative to that start, so your child placement rules do not change when the card moves.
Local line numbering
Inside a subgrid, line numbers restart at 1. Line 1 is the subgrid's first edge, which is the parent line where the item starts. The last line is the parent line where the item ends. Negative numbers count back from that last line, so -1 is the subgrid's end edge, not the parent's.
For .panel above, which uses grid-column: 2 / 5:
| Local line | Parent line | Meaning |
|---|---|---|
| 1 | 2 | Start edge of the panel |
| 2 | 3 | Between first and second borrowed track |
| 3 | 4 | Between second and third borrowed track |
| 4 (or -1) | 5 | End edge of the panel |
The panel spans three tracks, so it has four lines. A subgrid that spans n tracks always has n + 1 lines, numbered 1 to n + 1.
A complete example
This page places two children inside a column subgrid. Open it in a browser that supports subgrid and inspect it with the grid overlay in your developer tools.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Subgrid placement</title>
<style>
.layout {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 0.5rem;
}
.panel {
grid-column: 2 / 5;
display: grid;
grid-template-columns: subgrid;
}
.wide { grid-column: 1 / 3; } /* local lines 1-3 = parent tracks 2 and 3 */
.last { grid-column: -2 / -1; } /* the final borrowed track = parent track 4 */
.box { padding: 0.5rem; background: #cde; }
</style>
</head>
<body>
<div class="layout">
<section class="panel">
<div class="box wide">Wide</div>
<div class="box last">Last</div>
</section>
</div>
</body>
</html>.wide uses 1 / 3. In local numbering that covers the first two borrowed tracks, which are parent columns 2 and 3. .last uses -2 / -1, which is the final borrowed track, parent column 4. Neither rule mentions parent line numbers, so you could move the panel to grid-column: 3 / 6 and both rules would still work.
Placing children by row
The same rules apply on the row axis. Here is a card that spans three parent rows, with a link that is always placed in the last of them.
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
.card > h2 { grid-row: 1; }
.card > p { grid-row: 2; }
.card > a { grid-row: 3; }Without explicit placement, auto-placement fills rows in source order. If a card has no <p>, the link would move up into row 2, and its footer would no longer line up with the other cards. Explicit grid-row: 3 keeps the link in the footer row regardless. This is the usual reason to place children explicitly inside a subgrid, even when the markup looks simple. The wider card pattern gets its own chapter. Here, note only that local numbering makes this rule independent of where the card sits in the parent.
Explicit and implicit tracks
A subgrid has no implicit tracks in a subgridded axis. Its track count is fixed by the span. Two practical consequences follow:
- Every child must fit within the span. If you have four rows of content but
span 3, the extra child does not create a fourth row. Do not rely on how browsers resolve this. Match the span to the content. 1 / -1means "all explicit tracks". For the parent, that excludes implicit tracks. In a card grid where the rows are implicit (created by the cards themselves),grid-row: 1 / -1would not span them. Usespan 3instead.
Margins, padding and borders on the subgrid
The subgrid item is still a box. Its margin, border and padding sit outside or around its tracks. Tracks are shared with the parent, so these values do not change the track sizes by themselves. Instead, the browser treats them as extra space at the subgrid's edges. Children in the first and last tracks are pushed inward by the padding, and that extra amount counts toward the size of those parent tracks.
This means two subgrids that span the same tracks but have different padding can contribute different sizes to the same parent track. The tracks stay shared, so the content of both aligns. Only the contribution to sizing differs. How subgrid content influences track sizing in general is covered in a later chapter.
Spanning rules at a glance
- Put the span on the subgrid item.
grid-column: 2 / 5orgrid-row: span 3fixes how many tracks it gets. - Declare
subgridon the matching template property,grid-template-columnsfor columns andgrid-template-rowsfor rows. - Place children with local line numbers, starting at 1. Negative numbers count from the subgrid's own end edge.
- Use
spaninstead of-1when the parent's tracks are implicit. - Place children explicitly when optional content could shift the others out of their intended tracks.
Common mistakes
- Declaring
subgridwithout a span. The item spans one track, so the subgrid has one track. Addgrid-columnorgrid-rowwith a span. - Using parent line numbers inside the subgrid.
grid-column: 3 / 4on a child of a panel that starts at parent line 2 selects the third local line, not parent line 3. Count from the subgrid's start. - Forgetting
display: grid.grid-template-columns: subgridhas no effect on an element that is not a grid container. - Counting tracks instead of lines. A three-track span has four lines.
grid-column: 1 / 4covers all three tracks, whilegrid-column: 1 / 3covers two.
Exercise
A parent grid has grid-template-columns: repeat(8, 1fr). A section is declared with grid-column: 3 / 7; display: grid; grid-template-columns: subgrid;. (a) How many column tracks does the subgrid have? (b) Which parent columns does a child with grid-column: 2 / 4 occupy? (c) Which parent column does a child with grid-column: -2 / -1 occupy?
Show answer
(a) The section spans parent lines 3 to 7, which is four tracks (columns 3, 4, 5 and 6), so the subgrid has four column tracks and five local lines. (b) Local line 2 is parent line 4 and local line 4 is parent line 6, so the child occupies parent columns 4 and 5. (c) Local line -1 is parent line 7 and -2 is parent line 6, so the child occupies parent column 6.
Exercise
A card grid places each .card with grid-row: span 3; grid-template-rows: subgrid;. Some cards lack a body paragraph, and their "Read more" links appear in the middle row instead of the bottom row. What change fixes this, and why does it work?
Show answer
Place the link explicitly with .card > a { grid-row: 3; }. Without it, auto-placement fills the second row with the link when the paragraph is missing. With explicit local row 3, the link always lands in the third borrowed row, which is the shared footer row across cards.