Sub topic 9
Placing and spanning items across columns
This chapter assumes you know basic HTML and CSS, the box model, and how to build a 12-column grid with display: grid, repeat(), fr units, and gap. Here you will place items on that grid and make them cover several columns.
Grid lines, not columns
CSS Grid places items by lines, not by column numbers. A grid with 12 columns has 13 vertical lines. Line 1 is the left edge of the first column. Line 13 is the right edge of the last column. Negative numbers count from the end, so -1 is always the last line.
A column's gutter sits between two columns, so an item that spans several columns also covers the gutters between them.
Using grid-column
The grid-column property is shorthand for grid-column-start and grid-column-end. You can write it in several forms:
| Declaration | Meaning |
|---|---|
| `grid-column: 1 / 5;` | Start at line 1, end at line 5 (4 columns) |
| `grid-column: 1 / span 4;` | Start at line 1, cover 4 columns |
| `grid-column: span 4;` | Cover 4 columns from wherever auto-placement puts the item |
| `grid-column: 1 / -1;` | Cover every column, from the first line to the last |
Use span when the width matters more than the position. Use explicit line numbers when the item must start in a specific place.
A working example
This markup has a main area and a sidebar. The first CSS rule set creates the container and the grid. The last two rules give the items their widths.
<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;
}The main item takes 8 columns. The sidebar takes the remaining 4. Because 8 + 4 = 12, they fit on one row. The .page container caps the width at 72rem, centers it, and supplies the side margins through padding-inline.
How auto-placement fills rows
Items without an explicit position are placed in source order. The browser puts each item in the next free space on the current row. If the item's span does not fit, it moves to the next row.
Suppose you add a third item with grid-column: span 6 after the sidebar. The first row is full, so the new item starts a second row at line 1. Rows grow to the height of their tallest item.
Starting items at a chosen line
You can leave empty columns by choosing a start line. This centers a narrower block:
.intro {
grid-column: 3 / span 8;
}The .intro element starts at line 3 and covers 8 columns, so columns 1, 2, 11, and 12 stay empty. The result is a symmetric 2-column inset on both sides.
Common errors
- Spans that add up to more than 12. An 8-column item followed by a 6-column item wraps to a new row. This is correct behavior, but it often surprises people who expected one row.
- Mixing up lines and columns.
grid-column: 1 / 4covers 3 columns, not 4. The end value is a line, not a column. - Spanning more columns than exist.
span 14in a 12-column grid creates extra implicit columns and can cause horizontal overflow. Keep spans at or below the column count of the current layout. - Placing items on gutters. Gutters are not tracks. You cannot place an item "in" a gutter. Use
gapfor spacing, not extra empty columns.
Exercise
A grid has 12 columns. You want a card to start at the third column and end after the tenth column. Write the grid-column declaration two ways, and say how many columns it covers.
Show answer
grid-column: 3 / 11; or grid-column: 3 / span 8;. The card starts at line 3 and ends at line 11, so it covers columns 3 through 10, which is 8 columns.
Exercise
Three items have grid-column: span 5, span 5, and span 4 in a 12-column grid, in that order. How many rows do they occupy, and which items share a row?
Show answer
Two rows. The first two items take 10 columns on row 1. The third item needs 4 columns, but only 2 remain, so it moves to row 2.