Sub topic 5
Spanning Tracks and Placing Items Inside a Subgrid
This chapter assumes you know display: grid, grid-template-columns and grid-template-rows, fr units, gap, and placing items with grid-column and grid-row using line numbers and span. It also assumes you have seen the subgrid keyword. Here you will learn how a subgrid gets its tracks and how to place items inside it.
A subgrid gets tracks from the area it spans
A subgrid does not choose how many tracks it has. The number comes from the parent grid area the subgrid occupies. If the subgrid item spans three parent rows, it has exactly three rows.
.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;
}grid-row: span 3 makes each .card cover three parent rows. grid-template-rows: subgrid tells the card to use those three rows as its own. Without the span, the card would cover one parent row and would get only one subgrid row.
<div class="cards">
<article class="card">
<h2>Short title</h2>
<p>Brief text.</p>
<a href="#">Read more</a>
</article>
<article class="card">
<h2>A much longer title that wraps onto two lines</h2>
<p>Longer body text that takes more room than the first card.</p>
<a href="#">Read more</a>
</article>
</div>The card has three direct children. Auto-placement puts them into the three subgrid rows in order: title, text, link.
Local line numbering
Inside a subgrid, line numbers restart at 1. They are relative to the subgrid, not to the parent. The first line of the subgrid is line 1, even if it sits at parent line 5.
Take a subgrid that spans parent rows 4 to 7. It has three rows and four lines.
| Subgrid line | Parent line |
|---|---|
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
| 4 | 7 |
Negative numbers also count from the subgrid. -1 is the last line of the subgrid, not the last line of the parent. This is why grid-column: 1 / -1 on a child of a subgrid spans the whole subgrid and nothing more.
Placing items with local lines
Children of a subgrid use ordinary placement properties. You count from the subgrid's own lines.
.card .title {
grid-row: 1;
}
.card .meta {
grid-row: 2 / 4;
}.title sits in the first subgrid row. .meta covers the second and third subgrid rows. Both use local numbers, so the placement stays correct wherever the card lands in the parent.
Spanning columns
The same rule applies on the column axis. A form row that spans all parent columns inherits all of them.
form {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.75rem 1rem;
}
.field {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}grid-column: 1 / -1 on .field spans both parent columns. The subgrid therefore has two columns, with the same widths as the parent's. A label in column 1 and an input in column 2 of every .field line up with each other.
Common mistakes
- Placing children with parent line numbers instead of local numbers.
- Using
span 3but giving the card more than three direct children, so the extras fall into implicit tracks. - Expecting
-1to point at the parent's last line.
Exercise
A .card has grid-row: 2 / 5 and grid-template-rows: subgrid. How many rows does the subgrid have, and which parent line does the subgrid's line 3 correspond to?
Show answer
The card spans parent lines 2 to 5, which is three rows. Subgrid line 1 is parent line 2, so subgrid line 3 is parent line 4.
Exercise
A .field uses grid-column: 1 / -1 and grid-template-columns: subgrid inside a parent with two columns. A child of .field uses grid-column: 2. Which column does it land in?
Show answer
The second column, which is the parent's second column, because the subgrid inherits both columns and line 2 is the start of the second one.