CSS Grid
CSS Grid is a two-dimensional layout system: it arranges items in rows and columns at the same time. Where Flexbox lays out a single line of items, Grid lets you design a whole page skeleton — header, sidebar, content, footer — or a gallery where every card lines up perfectly in both directions.
Grid vocabulary
| Term | Meaning |
|---|---|
| Grid container | The element with display: grid |
| Grid item | A direct child of the container |
| Grid line | The dividing lines between tracks, numbered from 1 |
| Track | A single row or column |
| Cell | The space where one row and one column cross |
| Area | A rectangle of one or more cells |
| Gap | Space between tracks (also called gutters) |
Your first grid
Define the columns with grid-template-columns. Items fill the cells left to right, creating new rows as needed.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.grid {
display: grid;
grid-template-columns: 100px 100px 100px;
gap: 10px;
}
.grid div { background: #26a69a; color: white; padding: 18px 0; text-align: center; border-radius: 6px; font-size: 20px; }
</style>
</head>
<body>
<div class="grid">
<div>1</div><div>2</div><div>3</div>
<div>4</div><div>5</div><div>6</div>
<div>7</div>
</div>
</body>
</html>The fr unit
Fixed pixel columns don't adapt. The fr unit means "a fraction of the free space". 1fr 2fr makes the second column twice as wide as the first, and both stretch with the container.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.grid { display: grid; gap: 8px; margin-bottom: 12px; }
.a { grid-template-columns: 1fr 1fr 1fr; }
.b { grid-template-columns: 1fr 2fr; }
.c { grid-template-columns: 120px 1fr; }
.grid div { background: #5c6bc0; color: white; padding: 14px; border-radius: 6px; }
</style>
</head>
<body>
<div class="grid a"><div>1fr</div><div>1fr</div><div>1fr</div></div>
<div class="grid b"><div>1fr</div><div>2fr</div></div>
<div class="grid c"><div>120px</div><div>1fr (the rest)</div></div>
</body>
</html>Tip:
repeat(4, 1fr)is shorthand for1fr 1fr 1fr 1fr.
Placing items on lines
Grid lines are numbered starting at 1. An item can span from one line to another with grid-column and grid-row, like grid-column: 1 / 3 (from line 1 to line 3, i.e. two columns). Negative numbers count from the end, so 1 / -1 means "full width".
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; margin: 6px; }
.page {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-rows: 50px 140px 40px;
gap: 6px;
}
.page > * { border-radius: 6px; padding: 8px; color: white; }
header { grid-column: 1 / -1; background: #ef5350; }
nav { background: #ab47bc; }
main { background: #42a5f5; }
footer { grid-column: 1 / -1; background: #66bb6a; }
</style>
</head>
<body>
<div class="page">
<header>Header (1 / -1)</header>
<nav>Sidebar</nav>
<main>Main content</main>
<footer>Footer</footer>
</div>
</body>
</html>Responsive without media queries
A famous one-liner creates as many columns as fit, each at least 100px wide:
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px; }
.cards div { height: 70px; border-radius: 10px; display: grid; place-items: center; font-size: 30px;
background: linear-gradient(135deg, #ffb74d, #f06292); }
</style>
</head>
<body>
<p>Resize the frame — the number of columns changes automatically.</p>
<div class="cards"><div>🍎</div><div>🍊</div><div>🍋</div><div>🍉</div><div>🍇</div><div>🍓</div><div>🍒</div><div>🥝</div></div>
</body>
</html>Grid or Flexbox?
| Use Grid when… | Use Flexbox when… |
|---|---|
| You need rows and columns aligned | Items flow in a single line |
| The layout drives the content (page skeleton) | The content drives the size (buttons, tags) |
| You want items to span multiple cells | You want items to wrap naturally |
They work together well: a Grid page layout often contains Flexbox components.
Test yourself
What does 1fr represent?
- One pixel
- One font-relative unit
- One share of the free space in the grid container
- One row
Which declaration makes an item span the full width of the grid?
- grid-column: 1 / -1
- grid-column: full
- width: 100fr
- grid-row: 1 / -1
What is the main difference between Grid and Flexbox?
- Grid is older
- Grid is two-dimensional; Flexbox is one-dimensional
- Flexbox cannot wrap
- Grid only works on images