CSS Grid Items
In this page:
item {
grid-column: start / end;
grid-row: start / end;
}
Grid Line Numbers
A grid with 3 columns has 4 vertical grid lines (numbered 1 to 4), and a grid with 2 rows has 3 horizontal lines -- grid-column and grid-row place an item by naming which lines it starts and ends between.
- Accepted values:
- grid-column — start line / end line, e.g. 1 / 3
- grid-row — start line / end line
- span n — spans several tracks, e.g. span 2
- 1 — the last line of the explicit grid
- grid-area — row-start / column-start / row-end / column-end, or an area name
- auto — automatic placement
Example: Grid Line Numbers
<style>
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 4px;
background: #eee;
}
.container div {
background: #ddd;
padding: 8px;
}
.item {
grid-column: 1 / 2;
background: steelblue;
color: white;
}
</style>
<div class="container">
<div class="item">Between lines 1 and 2</div>
</div>
grid-column: start / end
grid-column: 1 / 3 places an item starting at column line 1 and ending at column line 3, meaning it spans across the first two column tracks between those lines. Naming both start and end lines explicitly gives you precise, predictable placement regardless of how the grid is otherwise structured.
Under the hood, grid-column is shorthand for the two longhand properties grid-column-start and grid-column-end, which you can also set independently when only one edge of an item needs to move.
- Accepted values:
- grid-column: 1 / 3 — from line 1 to line 3
- grid-column: 2 / span 2 — start at 2, span two
- grid-column: 1 / -1 — full width
- grid-column: auto — default
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: grid-column: start / end
<style>
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 4px;
background: #eee;
}
.container div {
background: #ddd;
padding: 8px;
}
.item {
grid-column: 1 / 3;
background: steelblue;
color: white;
}
</style>
<div class="container">
<div class="item">Spans the first two tracks</div>
</div>
grid-row: start / end
grid-row works identically to grid-column but for the vertical axis -- grid-row: 2 / 4 spans an item across row tracks 2 and 3, stopping at row line 4. Combining an explicit grid-row with an explicit grid-column pins an item to an exact rectangular block anywhere in the grid.
Just like grid-column, grid-row is shorthand for grid-row-start and grid-row-end, which are useful to set independently when repositioning only one edge of an item without disturbing the other.
- Accepted values:
- grid-row: 1 / 3 — from line 1 to line 3
- grid-row: 2 / span 2 — start at 2, span two
- grid-row: 1 / -1 — full height
- grid-row: auto — default
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: grid-row: start / end
<style>
.container {
display: grid;
grid-template-rows: 60px 60px 60px;
gap: 4px;
background: #eee;
}
.container div {
background: #ddd;
}
.item {
grid-row: 2 / 4;
background: steelblue;
color: white;
}
</style>
<div class="container">
<div class="item">Spans rows 2 and 3</div>
</div>
Spanning with the span Keyword
Instead of naming both lines, grid-column: span 2 tells an item to span 2 columns starting from wherever it would normally be placed -- more flexible than hardcoding exact line numbers when content order might change.
- Accepted values:
- span <n> — spans n tracks
- grid-column: span 2 — two columns wide
- grid-row: span 3 — three rows tall
- grid-column: 1 / span 2 — starts at line 1
Example: Spanning with the span Keyword
<style>
.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 4px;
background: #eee;
}
.item {
grid-column: span 2;
background: steelblue;
color: white;
padding: 8px;
}
</style>
<div class="container">
<div class="item">Spans 2 columns from wherever it lands</div>
</div>
grid-area Shorthand for All Four Lines
grid-area: 1 / 1 / 3 / 3 sets row-start, column-start, row-end, column-end in one declaration (that order), placing an item into a 2x2 block starting at the top-left corner.
- Accepted values:
- grid-area: <row-start> / <col-start> / <row-end> / <col-end> — four lines
- grid-area: <name> — places in a named area
- grid-area: span 2 / span 3 — spans only
- auto — default
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: grid-area Shorthand for All Four Lines
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 4px;
background: #eee;
}
.item {
grid-area: 1 / 1 / 3 / 3;
background: steelblue;
color: white;
padding: 8px;
}
</style>
<div class="container">
<div class="item">2x2 block from the top-left corner</div>
</div>
- Counting line numbers from 0, when grid lines start at 1.
- Confusing the number of tracks with lines, since three columns have four lines.
- Using
spanwithout a number or wrong order such asgrid-column: spanalone.
CSS Grid is supported in Chrome 57+, Firefox 52+, Safari 10.1+ and Edge 16+.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: