CSS grid-template-areas: Named Grid Areas
Naming grid areas without line numbers
The old way was floats with clearfix and margin math, or grid with line numbers. Template areas let you name regions like header, sidebar, main, and drop them in place.
The old hack vs the modern fix
View demo.header {
grid-column: 1 / -1;
}
.sidebar {
grid-column: 1;
grid-row: 2;
}
.main {
grid-column: 2;
grid-row: 2;
}
.footer {
grid-column: 1 / -1;
grid-row: 3;
}.layout {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}Grid browser support
Live demo
Why the modern way wins
Readable layout
The grid structure is visible at a glance. No counting lines or guessing spans.
One place to change
Add or remove a row in the template string. Child items use grid-area names only.
No line math
No grid-column: 1 / 3 or grid-row: 2. Name the area and the browser places it.
- Lines Saved
- 12 → 6
- No line-number placement
- Old Approach
- Floats or line numbers
- Clearfix, margin hacks, or grid-column/row
- Modern Approach
- Named areas
- grid-template-areas + grid-area
How it works
The old approach was either float-based layouts with clearfix and tricky margins, or grid with explicit grid-column and grid-row line numbers. Line numbers work but are hard to read and refactor.
The modern approach is grid-template-areas: you write a string that looks like your layout. Each quoted row lists area names; repeat a name to span columns. Then on each child you set grid-area: header (or whatever name). The layout is defined in one place and reads like a simple map.