Sub topic 9
Placing and spanning items across columns
This chapter assumes you know basic HTML and CSS, the box model, and how to declare a 12-column grid with display: grid, repeat(), fr units, and gap. Here you will learn how to decide which columns an item occupies, how grid lines are numbered, and which mistakes cause items to land in the wrong place.
Grid lines, not columns
A grid with twelve columns has thirteen vertical grid lines. Lines are numbered from 1 on the left, and each column sits between two neighbouring lines. Column 1 lies between line 1 and line 2. Column 12 lies between line 12 and line 13.
grid-column positions an item by naming a start line and an end line. This is the central idea of the chapter. When you write grid-column: 1 / 5, the item starts at line 1 and ends at line 5. It covers columns 1 to 4, which is four columns wide.
The gutters do not get line numbers of their own. With gap, the gutter sits between two tracks and is part of the space the lines divide. An item that spans several columns also covers the gutters between them.
Two ways to say "how wide"
You can describe an item's placement by its lines or by its width.
| Syntax | Meaning | Columns covered |
|---|---|---|
| `grid-column: 1 / 5` | Start at line 1, end at line 5 | 1 to 4 |
| `grid-column: 3 / span 6` | Start at line 3, cover six columns | 3 to 8 |
| `grid-column: span 4` | Cover four columns, start wherever auto-placement puts it | 4 columns |
| `grid-column: 1 / -1` | Start at line 1, end at the last line | All columns |
| `grid-column: 7` | Start at line 7, cover one column | 7 |
The shorthand grid-column: A / B expands to grid-column-start: A and grid-column-end: B. Use the longhand properties when you want to change only one side in a later rule.
Negative line numbers
Negative numbers count from the end of the explicit grid. The explicit grid is the set of tracks you declared with grid-template-columns. Line -1 is the last line, and -2 is the line before it. So grid-column: 1 / -1 always means "full width", whether the grid has 4 columns or 12. This is why the responsive example later in this chapter can use one rule for every breakpoint.
The 8 and 4 layout
The following markup and CSS reproduce the common main-plus-sidebar layout. The main content spans eight columns and the sidebar spans four.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}Neither rule names a start line. The browser's auto-placement algorithm puts .main in the first free spot, which is line 1. It then places .sidebar next to it, starting at line 9. The two items sum to twelve columns, so they fill the row exactly.
If you want the sidebar on the left without changing the markup, you could write explicit lines:
.main {
grid-column: 5 / span 8;
}
.sidebar {
grid-column: 1 / span 4;
grid-row: 1;
}The grid-row: 1 on the sidebar matters. Without it, auto-placement may put the sidebar on a second row, because the main item was already placed first and the sidebar's start is to the left of it. Placing both items in row 1 keeps them side by side. Be aware that this changes the visual order but not the source order. The final chapters of this tutorial cover why that matters for keyboard and screen reader users.
How auto-placement behaves
When an item has no explicit position, the browser fills the grid row by row, left to right. It uses a cursor that only moves forward by default. Three results follow.
- An item with
span 8is placed at the first position where eight free columns remain in the current row. - If the next item does not fit in the rest of the row, it moves to the next row. The columns left empty stay empty.
- An item with an explicit column but no explicit row is placed in the first row where that column range is free.
You can see the effect of rule 2 with three items that span 8, 6, and 4 columns. The first takes columns 1 to 8. The second needs six columns but only four remain, so it wraps to the next row. The third then fits next to it, in columns 7 to 10.
<div class="grid demo">
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
</div>.demo {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1rem;
}
.a { grid-column: span 8; }
.b { grid-column: span 6; }
.c { grid-column: span 4; }Item B begins at line 1 on the second row, so C starts at line 7. Columns 9 to 12 of row 1 stay empty. If you need to avoid such holes, change the spans so that each row adds up to twelve, or set grid-auto-flow: dense. Dense packing lets later items fill earlier gaps, but it can also reorder items visually, so use it with care.
Placing items on specific rows
Columns are only one axis. Use grid-row to place items vertically. Rows are created automatically as items need them, and their height follows the content unless you set grid-auto-rows.
.feature {
grid-column: 1 / span 7;
grid-row: 1 / span 2;
}
.note-a {
grid-column: 8 / -1;
}
.note-b {
grid-column: 8 / -1;
}The feature spans two rows on the left. The two notes each take a row on the right, in columns 8 to 12. Because the notes name no row, auto-placement sends them to the first free cells that match their columns, which are rows 1 and 2.
Overlapping items
Grid permits more than one item in the same cell. Items that overlap are painted in source order, so a later item sits on top of an earlier one. You can change that with z-index.
.hero-image {
grid-column: 1 / -1;
grid-row: 1;
}
.hero-title {
grid-column: 2 / span 6;
grid-row: 1;
align-self: end;
}Both items sit in row 1. The image spans the full width and the title is laid over it, in the lower part of the row. This is a deliberate overlap. An accidental one is a common bug, covered below.
Named lines
Numbers are fragile when the layout changes. You can name lines in the template and refer to the names instead.
.grid {
display: grid;
grid-template-columns:
[full-start] 1fr
[content-start] repeat(10, 1fr) [content-end]
1fr [full-end];
column-gap: 1.5rem;
}
.text { grid-column: content-start / content-end; }
.banner { grid-column: full-start / full-end; }The names content-start and content-end are labels you choose. The result has twelve columns, and .text covers the ten middle ones. If you later change the number of middle columns, the rules that use the names keep working.
Common mistakes
- Counting columns instead of lines.
grid-column: 1 / 4covers three columns, not four. To cover four, write1 / 5or1 / span 4. - Spans that exceed the grid.
span 8on a 4-column grid creates extra implicit columns, which usually makes the page overflow horizontally. Set a smaller span at narrow widths. - Using
-1with implicit tracks. Negative numbers ignore implicit columns, so the item may not reach the right edge. - Reversed lines. If the start is after the end, the browser swaps them.
grid-column: 5 / 2behaves like2 / 5. This can hide a typo. - Unplanned overlaps. Two items given the same explicit cells will stack. Check each row's spans to confirm they do not collide.
- Forgetting that the gap is included. A
span 4item is wider than four columns by three gutters. That is intended, but it matters when you compute widths by hand.
Checking your placement
Browser developer tools can draw the grid. In Firefox and Chromium-based browsers, the Layout or Grid overlay shows line numbers and names on top of the page. Turn it on whenever an item lands somewhere you did not expect. The numbers on the overlay are the same ones you use in grid-column.
Exercise
A grid uses grid-template-columns: repeat(12, 1fr). You want a banner to start at the third column and end at the end of the tenth column. Write the grid-column declaration in two different ways, and say how many columns the banner covers.
Show answer
grid-column: 3 / 11 or grid-column: 3 / span 8. The banner starts at line 3 (the left edge of column 3) and ends at line 11 (the right edge of column 10), so it covers 8 columns: columns 3 to 10.
Exercise
A card has grid-column: 1 / -1 and the grid is declared with four columns. Later, a media query changes the grid to twelve columns. Does the card need a new rule to stay full width? Explain.
Show answer
No. Line -1 always refers to the last line of the explicit grid, so the card spans all four columns before the media query and all twelve afterwards, with no change to its own rule.
Exercise
You place .sidebar { grid-column: 1 / span 4 } and .main { grid-column: 5 / span 8 } in a grid where .main comes first in the HTML. The sidebar appears on the second row. What is the cause and the fix?
Show answer
Auto-placement handles .main first and puts it in row 1. The sidebar's start line is to the left of the cursor, so by default it moves to the next row. Setting grid-row: 1 on both items puts them in the same row.