Sub topic 7
Spanning rows and columns
This chapter covers how a grid item can occupy more than one track. It assumes you know how to define columns and rows with grid-template-columns and grid-template-rows, that you can place items by line number, and that you understand fr, repeat() and gap. Basic HTML and the box model are taken as known.
What spanning means
A grid item normally sits in one cell, the area where one row track and one column track cross. Spanning makes an item cover several adjacent tracks. The item's box then covers those tracks and the gaps between them.
You can describe a span in two ways:
- By start and end lines, for example
grid-column: 1 / 3. - By a start (or end) line plus a count of tracks, for example
grid-column: 1 / span 2.
Both forms produce the same result when the numbers agree. Counting tracks is usually easier to read, and counting lines is more precise.
Lines versus tracks
A grid with three columns has four column lines, numbered 1 to 4 from the start edge. Line 1 is the left edge in a left-to-right language. Tracks sit between lines.
The most common mistake is to confuse the end line with the last track. In grid-column: 1 / 3, the item starts at line 1 and ends at line 3. It covers tracks A and B, which is two columns, not three.
The longhand and shorthand properties
Each axis has a shorthand and two longhands.
| Shorthand | Longhands | Axis |
|---|---|---|
| `grid-column` | `grid-column-start`, `grid-column-end` | Inline (columns) |
| `grid-row` | `grid-row-start`, `grid-row-end` | Block (rows) |
The shorthand takes the start value, a slash, then the end value. If you give only one value, it sets the start, and the end defaults to auto, which means a span of one track.
Spanning with `span`
The span keyword counts tracks from the opposite line. It can go on either side of the slash.
.featured {
grid-column: 1 / 3;
grid-row: 1 / span 2;
}Here grid-column: 1 / 3 covers two columns by naming both lines. grid-row: 1 / span 2 starts at row line 1 and covers two rows, so it ends at row line 3.
When span is the start value, the item ends at the named line and extends backward:
.tail {
grid-column: span 2 / 5;
}This item ends at column line 5 and starts at line 3. It needs a grid with at least four columns, so that line 5 exists.
You can also use span alone. grid-column: span 2 means "cover two columns starting wherever auto-placement puts me." This is useful when you want an item to be wide but don't care about its exact position.
A complete example
The following page shows a featured card that spans two columns and two rows in a four-column grid. The other cards fill the remaining cells.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Spanning demo</title>
<style>
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 100px;
gap: 1rem;
}
.grid > div {
background: #dde7f5;
padding: 0.5rem;
}
.featured {
grid-column: 1 / span 2;
grid-row: 1 / span 2;
background: #f5dde0;
}
.wide {
grid-column: span 3;
}
</style>
</head>
<body>
<div class="grid">
<div class="featured">Featured</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div class="wide">Wide</div>
<div>7</div>
</div>
</body>
</html>The featured item takes columns 1 and 2 and rows 1 and 2. Items 2 to 5 flow into the free cells to its right. Item 5 lands in row 2, column 4, because the featured item blocks columns 1 and 2. Then .wide needs three adjacent free columns. Row 2 has only one free cell left, so auto-placement moves it to row 3 and leaves that gap behind.
Gaps and spanned items
Gaps are part of a spanned item's width. An item that spans two columns with a 16px gap is two column widths plus 16px wide. This keeps a spanning item flush with the items above and below it, so you do not need to compensate for the gap.
Overlap
Spanning does not require that items avoid each other. If two items are explicitly placed on the same cells, they overlap, and later items in source order paint on top. You can control this with z-index, which works on grid items even without position.
.base {
grid-column: 1 / 4;
grid-row: 1 / 3;
}
.badge {
grid-column: 3 / 4;
grid-row: 1 / 2;
z-index: 1;
}Overlap is a legitimate technique for badges and captions on images. It is also a common source of bugs when two items are placed by accident on the same cell, so check for it when something seems to vanish.
Interaction with auto-placement
Auto-placed items with a span look for the first run of free cells large enough to hold them. By default, the algorithm moves forward only. If a wide item does not fit in the current row, the cursor moves on and the skipped cells stay empty. Later, smaller items do not backfill those holes unless you set grid-auto-flow: row dense.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
grid-auto-flow: row dense;
gap: 8px;
}Dense packing fills holes, but it can display items out of source order. That is a problem for keyboard focus and screen readers, which follow the DOM order, not the visual order. Use it for content where order does not matter, such as image galleries.
Spans that exceed the explicit grid
If an item spans beyond the last explicit track, the browser creates implicit tracks to hold it. With three declared columns, grid-column: 3 / span 2 creates a fourth column. Its width comes from grid-auto-columns, which defaults to auto. This often produces a surprisingly narrow or oddly sized extra column. If you see one, check whether a span or line number points past your declared tracks.
Negative line numbers count from the end of the explicit grid. grid-column: 1 / -1 spans every explicit column, which is a reliable way to make a full-width item such as a header, regardless of how many columns exist. It does not include implicit columns.
Spanning and `fr` tracks
A spanning item contributes to the size of the tracks it covers. With fixed sizes this has no effect. With auto or min-content tracks, a spanning item's content is distributed across the tracks it crosses, but only after items that span a single track have been sized. Content in a wide spanning item rarely forces a narrow fr column to grow, since fr tracks have an automatic minimum of auto. If a long word inside a spanning item causes unexpected overflow, set minmax(0, 1fr) on the tracks.
Common errors
- Reading
1 / 3as three columns. It is two. - Using
span 0. This is invalid, and so is a negative span. Spans must be positive integers. - Giving both start and end the same line, such as
2 / 2. The browser treats this as a one-track span. - Writing a start that is after the end, such as
3 / 1. The browser swaps them. - Expecting
grid-column: 1 / -1to cover implicit columns.
Exercise
A grid has grid-template-columns: repeat(5, 1fr). Write the declaration that makes an item start at column line 2 and cover three columns. Then state which line it ends at, and say what grid-column: 1 / -1 would cover.
Show answer
grid-column: 2 / span 3; (or 2 / 5). It ends at line 5, covering tracks 2, 3 and 4. grid-column: 1 / -1 covers all five explicit columns, from line 1 to line 6.
Exercise
In a three-column grid with auto-placement and no dense packing, a child has grid-column: span 2 and the previous item sits in column 2 of the current row. Where does the child go, and what happens to the cell in column 3?
Show answer
Only column 3 is free, which is one track, so the span of two does not fit. The child moves to the next row and starts at column 1. The column 3 cell in the first row stays empty, unless grid-auto-flow: row dense lets a later single-cell item fill it.