CSS Grid Items
By default each grid item takes the next free cell. The item properties let you place an item precisely, make it span several rows or columns, and override its alignment.
Item properties at a glance
| Property | Example | Meaning |
|---|---|---|
grid-column-start / -end |
2 / 4 |
Start and end column line |
grid-row-start / -end |
1 / 3 |
Start and end row line |
grid-column |
2 / 4 or span 2 |
Shorthand for start / end |
grid-row |
1 / 3 or span 2 |
Shorthand for start / end |
grid-area |
header or 1 / 2 / 3 / 4 |
Named area, or row-start / col-start / row-end / col-end |
justify-self |
start, end, center, stretch |
Horizontal alignment in the cell |
align-self |
same | Vertical alignment in the cell |
place-self |
center |
Shorthand for both |
order |
-1 |
Changes auto-placement order |
Spanning with line numbers and span
You can give explicit line numbers (2 / 4) or say how many tracks to cover (span 2). Combining them works too: 1 / span 3.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.g { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 55px; gap: 6px; }
.g div { background: #90a4ae; color: white; padding: 6px; border-radius: 6px; }
.wide { grid-column: span 2; background: #ef6c00 !important; }
.tall { grid-row: span 2; background: #6a1b9a !important; }
.big { grid-column: 2 / 4; grid-row: 3 / 5; background: #00838f !important; }
</style>
</head>
<body>
<div class="g">
<div class="wide">span 2 cols</div><div>3</div><div class="tall">span 2 rows</div>
<div>5</div><div>6</div><div>7</div>
<div class="big">2 / 4 × 3 / 5</div>
<div>9</div><div>10</div>
</div>
</body>
</html>Note: The end line is exclusive in the sense that
grid-column: 2 / 4covers columns 2 and 3 — the item stretches from line 2 up to line 4.
Overlapping items
Two items can occupy the same cells. Later items are painted on top, and z-index works on grid items without needing position. This is a clean way to put text over an image.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.hero { display: grid; height: 220px; border-radius: 12px; overflow: hidden; }
.hero > * { grid-area: 1 / 1; } /* everything in the same cell */
.bg { background: linear-gradient(160deg, #1a237e, #00bcd4); }
.sun { width: 90px; height: 90px; border-radius: 50%; background: #ffeb3b; justify-self: end; align-self: start; margin: 20px; }
.text { align-self: end; padding: 16px; color: white; font-size: 26px; font-weight: 700; z-index: 1; }
</style>
</head>
<body>
<div class="hero">
<div class="bg"></div>
<div class="sun"></div>
<div class="text">Stacked in one cell</div>
</div>
</body>
</html>Self-alignment
justify-self and align-self override the container's justify-items / align-items for a single item.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.g { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 90px; gap: 6px; }
.g div { background: #c5e1a5; border: 2px solid #558b2f; padding: 6px; }
.a { justify-self: start; }
.b { justify-self: end; align-self: end; }
.c { place-self: center; }
</style>
</head>
<body>
<div class="g">
<div>stretch (default)</div>
<div class="a">justify-self: start</div>
<div class="b">end / end</div>
<div class="c">place-self: center</div>
<div>stretch</div>
<div style="align-self: start">align-self: start</div>
</div>
</body>
</html>Dense packing
When spanning items leave holes, grid-auto-flow: dense on the container lets later, smaller items fill them. Great for photo walls; just remember the visual order may no longer match the HTML order.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 12px; font-size: 13px; }
.g { display: grid; grid-template-columns: repeat(3, 50px); grid-auto-rows: 50px; gap: 4px; }
.g div { background: #f48fb1; display: grid; place-items: center; border-radius: 4px; }
.g .w { grid-column: span 2; background: #ad1457; color: white; }
.dense { grid-auto-flow: dense; }
</style>
</head>
<body>
<div>normal<div class="g"><div>1</div><div class="w">2</div><div class="w">3</div><div>4</div><div>5</div></div></div>
<div>dense<div class="g dense"><div>1</div><div class="w">2</div><div class="w">3</div><div>4</div><div>5</div></div></div>
</body>
</html>Tip: Name your lines for readability:
grid-template-columns: [full-start] 1fr [content-start] 600px [content-end] 1fr [full-end];thengrid-column: content;.
Test yourself
Which declaration makes an item cover three columns starting wherever it lands?
- grid-column: 3
- grid-column: span 3
- grid-column: 1 / 3
- grid-columns: 3
grid-column: 2 / 4 covers which columns?
- 2, 3 and 4
- 2 and 3
- Only 4
- 4 and 5
What does grid-auto-flow: dense do?
- Removes all gaps
- Lets later items fill earlier holes in the grid
- Makes rows shorter
- Forces one column