Sub topic 6
Placing items by grid line
This chapter covers line-based placement: how grid lines are numbered, how grid-column and grid-row use them, and the errors that come up most often. It assumes you can already write a grid container with grid-template-columns and grid-template-rows, and that you know the fr unit, repeat() and gap. It also assumes you know the box model and normal document flow.
Lines, not cells
A grid is made of tracks (columns and rows). Between and around the tracks are grid lines. A grid with three columns has four vertical lines. A grid with two rows has three horizontal lines. In general, a grid with N tracks has N + 1 lines.
Lines are numbered starting at 1, not 0. In a left-to-right language, column line 1 is the left edge of the grid and row line 1 is the top edge. When you place an item, you tell the browser which lines the item should start and end on. The item then fills the area between those lines.
Because line numbers describe edges, the numbers are always one more than the number of tracks. This is the most common source of off-by-one mistakes.
The grid-column and grid-row shorthands
grid-column and grid-row each take a start line and an end line, separated by a slash.
.featured {
grid-column: 1 / 3;
grid-row: 1 / span 2;
}Here the item starts at column line 1 and ends at column line 3, so it covers two columns. It starts at row line 1 and spans two rows.
The shorthands expand into four longhand properties:
| Shorthand | Longhands |
|---|---|
| `grid-column: 1 / 3` | `grid-column-start: 1; grid-column-end: 3` |
| `grid-row: 2 / 4` | `grid-row-start: 2; grid-row-end: 4` |
Use the shorthands in most code. Use a longhand when you want to set only one edge, for example when a media query changes only where an item ends.
If you give only one value, such as grid-column: 2, the item starts at that line and spans one track.
A complete example
The following page can be run as shown. It creates a four-column grid and places three items by line.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Grid line placement</title>
<style>
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 80px);
gap: 8px;
}
.grid div {
background: #dbeafe;
border: 1px solid #1d4ed8;
padding: 8px;
}
.a { grid-column: 1 / 3; grid-row: 1; }
.b { grid-column: 3 / 5; grid-row: 1 / 3; }
.c { grid-column: 1 / -1; grid-row: 3; }
</style>
</head>
<body>
<div class="grid">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</div>
</body>
</html>Item A covers columns 1 and 2 in the first row. Item B covers columns 3 and 4 and runs across rows 1 and 2. Item C stretches across the whole third row.
Negative line numbers
Lines can also be counted from the end. -1 is the last line of the explicit grid, -2 is the one before it, and so on. This lets you say "to the far edge" without knowing how many columns there are.
.banner {
grid-column: 1 / -1;
}This item always spans every explicit column, even if you later change repeat(4, 1fr) to repeat(6, 1fr).
Spans
Instead of an end line, you can give a number of tracks to cover with the span keyword.
.wide { grid-column: 2 / span 3; } /* lines 2 to 5 */
.tall { grid-row: span 2; } /* start decided by auto-placement */span 3 means "cover three tracks from the start". With only span N, the item has no fixed start, so the auto-placement algorithm picks a spot where it fits. You can also put the span at the start: grid-column: span 2 / 5 makes the item end at line 5 and cover two tracks before it.
Spans are a good choice when the size of the item matters more than its position. Line numbers are a good choice when the position matters.
Reading the order of values
The start and end values can be in either order in the shorthand only if you remember the rule: the first value is the start, the second is the end. If the start line is greater than the end line, the browser swaps them. So grid-column: 3 / 1 behaves like 1 / 3. Do not rely on this. It makes code harder to read.
If the start and end are the same line, such as grid-column: 2 / 2, the end is treated as span 1, and the item covers one track.
Overlapping items
Grid placement allows items to share cells. Nothing prevents two items from being given the same lines.
.photo { grid-column: 1 / 4; grid-row: 1 / 3; }
.caption { grid-column: 1 / 3; grid-row: 2; z-index: 1; align-self: end; }The photo covers a 3-by-2 block. The caption sits in the lower left of that block. Items are painted in source order, so a later item appears on top of an earlier one. Set z-index to change this. Items in a grid accept z-index even without position.
Explicit and implicit lines
When you place an item on a line that does not exist, the browser creates implicit tracks to reach it. For example, in a grid with three explicit columns, grid-column: 5 places the item at column line 5. The browser adds implicit columns so that line exists.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.stray { grid-column: 5; }Implicit columns get a size of auto unless you set grid-auto-columns. This often surprises people when a typo or a miscount produces a thin extra column. If part of your layout is unexpectedly narrow, check that no item refers to a line past the end of the explicit grid.
Mixing placed and auto-placed items
Items with no grid-column or grid-row are auto-placed in the cells that are still free. Items with explicit positions are placed first. Auto-placed items then flow around them in source order.
This has a consequence worth knowing. If you place one item on row 1 and leave the others unplaced, the others fill the remaining cells in order, skipping the occupied area. Reordering your HTML can therefore change the layout, even if some items are positioned.
Checking your lines in the browser
Every major browser's developer tools can draw the grid lines and show their numbers. In Chrome, Edge and Firefox, select an element with display: grid and turn on the grid overlay in the Layout panel. Firefox also shows negative line numbers. Use the overlay whenever a placement does not match what you expected.
Common errors
- Counting tracks instead of lines. To cover columns 2 and 3, write
2 / 4, not2 / 3. - Forgetting that numbering starts at 1.
grid-column: 0is invalid, and the declaration is ignored. - Using
-1with implicit tracks. It refers to the end of the explicit grid only. - Placing items beyond the explicit grid by accident. This creates narrow, auto-sized implicit tracks.
- Writing
grid-column: 1-3. The slash is required. Use1 / 3. - Assuming items cannot overlap. Two items given the same area will stack, with no warning.
Exercise
A container has grid-template-columns: repeat(5, 1fr). Write one declaration that makes an item cover columns 2, 3 and 4, and another that makes it reach the right edge from column 3 regardless of how many columns are later added.
Show answer
For columns 2 to 4, use grid-column: 2 / 5 (or 2 / span 3), because the end line is the line after column 4. To reach the right edge from column 3 no matter how many columns exist, use grid-column: 3 / -1.
Exercise
In a grid with three explicit columns, an item has grid-column: 5. What happens, and how do you control the width of the extra column?
Show answer
The browser creates implicit columns so that column line 5 exists, and the item sits in the fifth column. Implicit columns are auto sized by default. Set grid-auto-columns (for example grid-auto-columns: 100px) to control their width, or fix the placement so the item stays within the explicit grid.