π What is grid-template-areas?
grid-template-areas is a CSS Grid container property that allows you to create layouts using named areas instead of manual line numbers. It makes complex layouts easier to read, maintain, and modify β almost like drawing your layout using ASCII art.
π― Why Use grid-template-areas?
- Creates readable & descriptive layouts
- Makes designing page structures intuitive
- Easy to rearrange item positions without touching HTML
- Perfect for responsive layout changes
- Helps avoid confusing grid-line numbers
π Syntax
Basic Syntax
grid-template-areas:
"header header header"
"sidebar content content"
"footer footer footer";Each string becomes a visual βmapβ of your layout. Repeated names allow areas to span multiple rows or columns.
π§± Assigning Areas to Items
Use grid-area inside grid items to connect them to named areas.
Assigning Items
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: content; }
footer { grid-area: footer; }π Visual Example

π§ͺ Full Example Layout
Complete Layout Example
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 80px auto 60px;
grid-template-areas:
"header header header"
"sidebar content content"
"footer footer footer";
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: content; }
footer { grid-area: footer; }This creates a classic header/sidebar/content/footer layout using only names.
π₯ Overlapping Areas (Optional)
grid-template-areas does NOT support overlapping areas, but each grid cell must belong to exactly one named area or a dot (.).
Using Dots for Empty Space
grid-template-areas:
"header header ."
"sidebar content ."
"footer footer .";Note
π§© Responsive Layouts with Areas
One of the biggest advantages of areas is how easy it is to rearrange the layout for different screen sizes.
Responsive Example
.container {
display: grid;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"content"
"sidebar"
"footer";
}
@media (min-width: 800px) {
.container {
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
}Mobile β stacked layout
Desktop β traditional sidebar layout
π§ Rules to Remember
- Every row must have the same number of area names
- Dotted areas . represent empty cells
- Each named area must form a perfect rectangle (no L-shapes!)
- grid-area names must match exactly
- You can repeat area names to make them span multiple tracks
β οΈ Common Mistakes
- Mismatched column count in each row of area definitions
- Misspelled area names (wonβt map to items)
- Trying to make non-rectangular areas (not allowed)
- Using grid-template-areas without assigning grid-area to children
Note
π grid-template-areas vs grid-template-rows/columns
| Feature | grid-template-areas | Line-based Layout |
|---|---|---|
| Ease of Use | Very easy & visual | More technical |
| Readability | High | Low for complex layouts |
| Precision | Good | Excellent |
| Best For | Page layouts, dashboards | Complex dynamic positioning |
π₯ Summary
grid-template-areas is one of the most powerful and user-friendly features of CSS Grid. It turns layout creation into a readable, logical, and visual process β no more guessing line numbers.
Learn more βMDN Docs π