Sub topic 1
Quick recap of grid tracks, lines and spanning
Subgrid builds directly on a few core Grid ideas. This chapter refreshes them so the later chapters can focus on what is new. You will see how tracks and lines relate, how line numbers count, and how an item spans several tracks.
Prerequisites
This chapter assumes you already know:
- Basic CSS selectors and the box model.
- How to turn on a grid with
display: gridand define tracks withgrid-template-columns,grid-template-rows,frunits andgap. - How to place items with
grid-columnandgrid-row.
If any of these are unfamiliar, review them first. This chapter is a refresher, not an introduction.
Tracks and lines
A grid has two kinds of building blocks:
- A track is a single row or column. It is the space where content sits.
- A grid line is the boundary between tracks, or at the outer edge of the grid.
A grid with three columns has four column lines. Lines are numbered from 1 at the start edge. Negative numbers count back from the end, so -1 is always the last line of the explicit grid.
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto 1fr;
gap: 1rem;
}This declares three column tracks and two row tracks. The gap property adds space between tracks. It does not add space at the outer edges.
Placing items by line number
An item is placed by naming the lines where it starts and ends. The first value is the start line and the second is the end line. The item covers every track between them.
.header {
grid-column: 1 / 4;
}
.sidebar {
grid-column: 1 / 2;
grid-row: 2;
}Here .header runs from line 1 to line 4, so it covers all three columns. The shorthand 1 / -1 gives the same result and keeps working if you add more columns later. .sidebar occupies only the first column, in the second row.
Spanning
The span keyword lets you say how many tracks an item covers instead of naming an end line.
.feature {
grid-column: 2 / span 2;
grid-row: span 2;
}2 / span 2 starts at line 2 and covers two tracks, ending at line 4. A bare grid-row: span 2 lets auto-placement choose the start line and then covers two rows.
Spanning matters for subgrid because the number of tracks an item spans decides how many tracks a subgrid can inherit. A grid item that spans three rows has three rows available to share with its own children.
Gaps and spanned tracks
When an item spans several tracks, it also covers the gaps between them. A card that spans two rows with a 1rem gap is taller than two row tracks by that gap. Keep this in mind. Subgrid has to decide what to do with those gaps, and a later chapter covers it.
The nested grid problem in one line
Any grid item can itself be a grid container. By default, though, a nested grid defines its own independent tracks. Its lines do not match the parent's lines, even when the sizes look similar. The next chapter shows why that breaks alignment.
Exercise
A grid has grid-template-columns: repeat(4, 1fr). How many column lines does it have, and which grid-column value makes an item start at line 2 and cover two tracks?
Show answer
It has five column lines, because n tracks have n + 1 lines. Use grid-column: 2 / span 2 or grid-column: 2 / 4. Both cover tracks 2 and 3.