Sub topic 1
Quick recap of grid tracks, lines and spanning
Subgrid builds directly on a handful of grid ideas: tracks, lines, gaps, and placement with span. This chapter refreshes those ideas, with the details that matter later. It also points out the places where people often go wrong.
Prerequisites
This chapter assumes you already know:
- Basic CSS selectors and the box model.
- How to turn on a grid with
display: gridand define it withgrid-template-columns,grid-template-rows,frunits andgap. - How to place an item with
grid-columnandgrid-row, using line numbers andspan.
If any of these is new to you, learn it first. This chapter is a refresher and does not teach grid from scratch.
Tracks and lines
A grid has two kinds of structure, and it is easy to mix them up.
- A track is a single column or a single row. It is a space that content occupies.
- A grid line is the boundary between tracks, or the outer edge of the grid. Lines have no size.
A grid with four columns has four column tracks and five column lines. Lines are numbered from 1, starting at the start edge. Negative numbers count back from the end edge, so -1 is the last line.
| Concept | Count for 4 columns | Counted from |
|---|---|---|
| Column tracks | 4 | 1 (first column) |
| Column lines | 5 | 1 (left edge in left-to-right text) or -1 (right edge) |
Line numbers follow the writing direction. In a right-to-left language, line 1 is the right edge. Subgrid inherits this behavior, so your placement code stays correct in both directions.
Defining tracks
You create the explicit grid with grid-template-columns and grid-template-rows. Each value in the list is one track size.
.layout {
display: grid;
grid-template-columns: 12rem 1fr 2fr;
grid-template-rows: auto 1fr auto;
}This defines three columns and three rows. The first column is fixed at 12rem. The other two share the leftover space in a 1:2 ratio. The rows are sized by their content, except the middle row, which takes the remaining height.
The sizing functions you will see most often are:
fr: a share of the free space left after fixed tracks and gaps are subtracted.auto: sized from the content, and it can also stretch to fill free space.minmax(min, max): a range, for exampleminmax(10rem, 1fr).repeat(count, size): shorthand for repeating a track size, for examplerepeat(4, 1fr).max-contentandmin-content: the widest and narrowest the content can reasonably be.
Responsive repeats
For a card grid, repeat() is often combined with auto-fill or auto-fit:
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}The browser creates as many columns of at least 16rem as fit in the container, and each column then grows to share the free space. With auto-fill, empty columns are kept. With auto-fit, empty columns collapse to zero width. The difference only shows when there are fewer items than columns.
This pattern is the one subgrid will extend in a later chapter. The number of columns changes with the viewport, so you cannot hard-code line numbers for a nested layout. That is a large part of why subgrid exists.
Fr units and overflow
A plain 1fr is shorthand for minmax(auto, 1fr). The auto minimum means a track will not shrink below the min-content size of its items. A long unbreakable word or a wide image can push a 1fr column wider than you intended. When you need the column to stay within its share, write minmax(0, 1fr).
Gaps
The gap property sets the gutter between tracks. It is shorthand for row-gap and column-gap, in that order.
.grid {
gap: 0.5rem 1rem; /* row-gap 0.5rem, column-gap 1rem */
}Two facts about gaps matter later:
- Gaps exist only between tracks. There is no gap at the outer edge of the grid.
- A gap behaves like a fixed-size track that sits between two lines. An item that spans several tracks also spans the gaps between them, so it is wider than the sum of the tracks alone.
Placing items with line numbers
By default, the grid auto-places items one per cell. To put an item somewhere specific, use grid-column and grid-row. Both are shorthands for a start line and an end line, separated by a slash.
.header {
grid-column: 1 / -1; /* from the first line to the last explicit line */
}
.sidebar {
grid-column: 1 / 2; /* the first column track */
grid-row: 2 / 4; /* the second and third row tracks */
}The end line is exclusive in the sense that the item stops at that line. grid-column: 1 / 2 occupies one track, not two. The number of tracks an item covers is the end line minus the start line.
Spanning
Instead of naming the end line, you can say how many tracks to cover with span. These two declarations are equivalent:
.a { grid-column: 2 / 4; }
.b { grid-column: 2 / span 2; }You can also give only a span and let the grid choose the start position through auto-placement:
.wide { grid-column: span 2; }
.tall { grid-row: span 3; }The span form is the one to remember for subgrid. In the card pattern later in this tutorial, each card is placed with grid-row: span 3. That sentence means "this item takes three row tracks of the parent", and the item's own position comes from auto-placement.
Local numbering
Every grid item's start and end are measured against the lines of the grid that contains it. Numbers are never global. If a grid item is itself a grid, its children use that inner grid's own line numbers, starting again at 1. Keep this in mind. Subgrid keeps the numbering local but ties the sizes to the parent.
Implicit tracks
If an item is placed outside the explicit grid, or if there are more auto-placed items than cells, the grid adds tracks. These are the implicit tracks. Their size comes from grid-auto-rows and grid-auto-columns, which default to auto.
.feed {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(5rem, auto);
gap: 1rem;
}This grid declares three columns and no rows. Each row that appears as items are added is at least 5rem tall and grows with its content.
There is one catch: negative line numbers count from the end of the explicit grid only. In the .feed example, the explicit grid has zero rows, so grid-row: 1 / -1 does not cover all the generated rows. It spans nothing useful. When you need an item to cover every row, set the rows explicitly or use a known span.
Auto-placement
Items without a position are placed in source order, filling the first available cell. A spanning item that does not fit in the current row moves to the next row, which can leave holes behind it. The grid-auto-flow: dense setting lets later items back-fill those holes, but it changes the visual order away from the source order. That can confuse keyboard users and screen reader users, so use it with care.
For subgrid the main point is simpler. An auto-placed card that uses grid-row: span 3 reserves three rows, and the next card goes beside it. Each row of cards therefore consumes a fixed block of three parent rows. This only works when every card takes the same number of rows.
Putting it together
The following page defines a three-column grid with a full-width header, a sidebar that spans two rows, and auto-placed content. You can save it as an HTML file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Grid recap</title>
<style>
.layout {
display: grid;
grid-template-columns: 10rem minmax(0, 1fr) minmax(0, 1fr);
grid-auto-rows: minmax(4rem, auto);
gap: 0.5rem 1rem;
}
.layout > * {
background: #e8eef7;
padding: 0.5rem;
}
.header { grid-column: 1 / -1; }
.sidebar { grid-column: 1; grid-row: 2 / span 2; }
.wide { grid-column: 2 / span 2; }
</style>
</head>
<body>
<div class="layout">
<header class="header">Header (lines 1 to -1)</header>
<nav class="sidebar">Sidebar (rows 2 and 3)</nav>
<main class="wide">Wide item (columns 2 and 3)</main>
<section>Auto-placed A</section>
<section>Auto-placed B</section>
</div>
</body>
</html>Reading it through:
- The header covers every explicit column, so it takes the whole first row.
- The sidebar is pinned to column 1 and covers rows 2 and 3.
- The wide item is auto-placed in row 2 because it needs two free columns, and column 1 of that row is taken by the sidebar.
- The two sections fill the next free cells in row 3, in columns 2 and 3.
Why this recap matters for subgrid
A grid container defines tracks for its direct children only. A child that is itself a grid creates a new, independent set of tracks, even when it sits across several parent tracks. The two grids share nothing: not sizes, not lines, not gaps. The next chapter shows what that costs you in real layouts, and the chapters after it show how the subgrid keyword fixes it.
Three ideas from this chapter come back repeatedly:
- Lines are boundaries and tracks are spaces. A span of N tracks runs between two lines that are N apart.
- Gaps sit between tracks, and a spanning item includes the gaps it crosses.
- Line numbers are local to the grid that contains the item.
Common mistakes
- Off-by-one errors.
grid-column: 1 / 3covers two tracks, not three. Count lines, not tracks. - Negative lines in an implicit grid.
-1refers to the end of the explicit grid, not the last row that exists. - Unexpected overflow with
1fr. The implicitautominimum lets content widen the track. Useminmax(0, 1fr)to prevent it. - Forgetting that
gapis row first.gap: 1rem 2remmeans a 1rem row gap and a 2rem column gap. - Assuming numbering is global. An item inside a nested grid counts lines in that nested grid, not the outer one.
Exercise
A grid is defined with grid-template-columns: repeat(5, 1fr) and gap: 1rem. How many column lines does it have, and which grid-column values place an item across the second, third and fourth columns? Give one form with line numbers and one with span.
Show answer
Five tracks give six column lines, numbered 1 to 6 (or -1 to -6 from the end). The second to fourth columns run from line 2 to line 5, so write grid-column: 2 / 5. With span it is grid-column: 2 / span 3. The item also covers the two gaps between those three columns.
Exercise
A grid declares grid-template-columns: repeat(3, 1fr) and no rows. Four items are added. Why does grid-row: 1 / -1 on the first item not cover both generated rows, and what would you change?
Show answer
The rows are implicit because no grid-template-rows exists, and -1 counts from the end of the explicit grid, which has no row tracks. The item therefore does not span the generated rows. Declare the rows explicitly, for example grid-template-rows: repeat(2, auto), or use a known span such as grid-row: 1 / span 2.