Placing grid items
Overview
Auto-placement fills cells in order, which is often all you need. But grid also lets you place items exactly — on specific lines, spanning specific tracks, even overlapping. This is where grid leaves every older layout technique behind.
Placing by line numbers
Grid lines are numbered from 1, starting at the top and the inline-start side:
.item {
grid-column-start: 2;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
}The shorthands take start / end:
.item {
grid-column: 2 / 4; /* from line 2 to line 4: columns 2 and 3 */
grid-row: 1 / 3; /* rows 1 and 2 */
}Give only a start line and the item spans one track: grid-column: 3.
span
span N covers N tracks from the start line (or back from the end line):
.wide { grid-column: 2 / span 2; } /* lines 2 to 4 */
.tall { grid-row: span 3; } /* 3 rows, wherever auto-placement puts it */Negative line numbers
Lines are also numbered backwards from the end of the explicit grid: -1 is the last line, -2 the one before it.
.full-width { grid-column: 1 / -1; } /* always the full width, however many columns */
.last-col { grid-column: -2 / -1; }1 / -1 is one of the most useful grid snippets: a header or full-bleed image spans all columns even if you later change the number of columns.
grid-area for all four
.item { grid-area: 2 / 1 / 4 / 3; } /* row-start / column-start / row-end / column-end */The order is the opposite of what you might expect from margin (it’s row-start, column-start, row-end, column-end). grid-area also accepts a name from grid-template-areas, as the next lesson shows.
Mixing placed and auto-placed items
You can place some items and let the rest flow. Auto-placement skips cells that are already taken, so unplaced items fill the gaps in order. Placed items are positioned first; then auto-placed items fill in around them.
Named lines
Line numbers become hard to read in big layouts. You can name lines inside the track list with square brackets:
.page {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] minmax(0, 60rem) [content-end]
minmax(1rem, 1fr) [full-end];
}
.page > * { grid-column: content; } /* content-start / content-end */
.page > .full-bleed { grid-column: full; } /* full-start / full-end */When lines are named x-start and x-end, you can use just x as a shorthand for the area between them. This “full-bleed” layout keeps text in a readable central column while letting chosen images or banners span the whole width — a pattern you’ll reuse in Page layouts.
Overlapping items
Grid items can occupy the same cells. They then stack, with later items on top (use z-index to change the order — it works on grid items without position):
.hero { display: grid; }
.hero > * { grid-area: 1 / 1; } /* every child in cell (1, 1) */
.hero .caption { align-self: end; } /* caption at the bottom */This is an excellent replacement for absolute positioning in overlays: unlike an absolutely positioned caption, a grid-stacked caption still affects the size of the cell. If the caption is taller than the image on a small screen, the cell grows instead of the text spilling out.
Placement and accessibility
Placement changes where things appear, not their order in the HTML. Keyboard focus and screen readers follow the source order. If you place a later element visually first, keyboard users will experience a different order from what they see. Keep the HTML in a sensible reading order and use placement for layout, not reordering. See Source order and reading order.
Tip: turn on line numbers in the DevTools grid overlay (Layout panel → “Show line numbers”). Placement by number is easy when you can see the numbers.
A magazine-style layout
On a 4-column, 3-row grid (all 1fr, rows minmax(8rem, auto)), place: the .lead story across the full width of the first row (use negative line numbers); .photo in column 1 spanning rows 2–3; .story in columns 2–3 of row 2; .ad in the last column spanning rows 2–3. Leave .note to be auto-placed. Then make a .hero element where a caption sits on top of an image in the same grid cell, aligned to the bottom — without absolute positioning.
<div class="mag">
<article class="lead">Lead story</article>
<figure class="photo">Photo</figure>
<article class="story">Story</article>
<aside class="ad">Ad</aside>
<p class="note">Note</p>
</div>
<div class="hero">
<img src="https://picsum.photos/id/29/800/400" alt="Mountains at sunrise" />
<p class="caption">Mountains at sunrise</p>
</div>
<style>
* { box-sizing: border-box; }
.mag > * { background: #eaf0fc; margin: 0; padding: 1rem; }
.caption { background: rgb(0 0 0 / 0.6); color: white; margin: 0; padding: 0.5rem 1rem; }
/* your code here */
</style><div class="mag">
<article class="lead">Lead story</article>
<figure class="photo">Photo</figure>
<article class="story">Story</article>
<aside class="ad">Ad</aside>
<p class="note">Note</p>
</div>
<div class="hero">
<img src="https://picsum.photos/id/29/800/400" alt="Mountains at sunrise" />
<p class="caption">Mountains at sunrise</p>
</div>
<style>
* { box-sizing: border-box; }
.mag > * { background: #eaf0fc; margin: 0; padding: 1rem; }
.caption { background: rgb(0 0 0 / 0.6); color: white; margin: 0; padding: 0.5rem 1rem; }
.mag {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, minmax(8rem, auto));
gap: 1rem;
}
.lead { grid-column: 1 / -1; }
.photo { grid-column: 1; grid-row: 2 / span 2; }
.story { grid-column: 2 / 4; grid-row: 2; }
.ad { grid-column: -2 / -1; grid-row: 2 / 4; }
/* .note is auto-placed into the first empty cell: row 3, column 2 */
.hero {
display: grid;
}
.hero > * {
grid-area: 1 / 1; /* both children in the same cell */
}
.hero img { width: 100%; height: auto; display: block; }
.caption { align-self: end; } /* sit at the bottom of the cell */
</style>Summary
grid-columnandgrid-rowtake a start and end line:grid-column: 2 / 4.span Ncovers N tracks; negative numbers count from the end, so1 / -1spans the whole explicit grid.grid-area: row-start / column-start / row-end / column-endsets all four at once.- Lines can be named in the template, such as
[content-start], and used for placement. - Items placed in the same cell overlap — a cleaner alternative to absolute positioning for overlays.