Sub topic 7
Spanning rows and columns
This chapter assumes you can create a grid container, define tracks with grid-template-columns and grid-template-rows, and number grid lines starting at 1. If line numbers are new to you, read the previous chapter on line-based placement first.
What spanning does
By default, each grid item fills one cell. Spanning makes an item cover several adjacent tracks. You can do this with start and end lines, or with the span keyword.
Spanning with line numbers
grid-column and grid-row are shorthands for a start line and an end line, separated by a slash.
.featured {
grid-column: 1 / 3;
grid-row: 1 / span 2;
}The item starts at column line 1 and ends at line 3, so it covers two columns. The end line is not included in the span. The row value starts at line 1 and spans two tracks, so it covers two rows.
Spanning with the span keyword
span N means "cover N tracks from the start line". It is useful when you care about the size of the item more than its exact end line.
.wide {
grid-column: span 2;
}
.tall {
grid-row: span 3;
}With only span, the browser picks the start position through auto-placement. The item takes the next free spot that has enough room.
You can also count from the end of the grid with negative line numbers. -1 is the last line of the explicit grid.
.banner {
grid-column: 1 / -1;
}This item stretches across every explicit column, however many there are.
A complete example
The markup and CSS below form a small page you can run as shown.
<!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: 80px;
gap: 8px;
}
.grid div { background: #cde; padding: 8px; }
.hero { grid-column: 1 / span 2; grid-row: 1 / span 2; background: #9bd; }
.strip { grid-column: 1 / -1; background: #bd9; }
</style>
</head>
<body>
<div class="grid">
<div class="hero">Hero</div>
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div class="strip">Strip</div>
</div>
</body>
</html>The hero covers a 2 by 2 block in the top left. A and B fill the top right, C and D fill the next row, and the strip takes a full row below.
Common errors
- Overlap. If two items are explicitly placed on the same cells, they overlap. The later item in the source order paints on top.
- Counting cells instead of lines.
grid-column: 1 / 3covers two columns, not three. - Using
-1with implicit tracks. Negative lines count from the explicit grid only. Tracks created bygrid-auto-rowsare not included. - Gaps in auto-placement. A large spanning item may not fit in the current row. The browser moves it down and leaves a hole. The
densevalue ofgrid-auto-flowcan fill such holes, but it can change the visual order away from the source order.
Exercise
A grid has five explicit columns. Write a declaration that makes an item start at column line 2 and cover three columns. Then write one that makes an item cover every explicit column.
Show answer
grid-column: 2 / span 3; (or grid-column: 2 / 5;) covers three columns starting at line 2. grid-column: 1 / -1; covers all explicit columns.