CSS Grid Areas & Templates
grid-template-areas is the most readable way to
build page layouts in CSS. Instead of remembering line numbers, you draw the layout in plain text — like
ASCII art — and the browser builds it. This chapter covers the full syntax, all the rules, responsive reflow
patterns, the grid-template shorthand, and a gallery of seven real-world
layout patterns.
grid-template-areas. for empty cells
grid-areagrid-template shorthandHow grid-template-areas Works
grid-template-areas defines the layout visually
using quoted strings. Each string is one row. Within each string, space-separated names are the columns.
Repeating a name across adjacent cells makes that item span all of them. Items self-place with grid-area: name:
grid-column: 1/-1 — you have to mentally map numbers
to positions. With named areas: grid-area: header — the code reads like
a description of the layout. When you change the grid (add a column, rearrange sections), you only update
the template strings — every item automatically follows.
Rules and Syntax
grid-template-areas has specific rules. Breaking
them causes the entire property to be ignored silently:
— All rows have the same column count
— Use
. for intentionally empty cells— Names are valid CSS identifiers (letters, digits, hyphens)
— Strings can be aligned with spaces for readability
— Items use
grid-area: name to self-place
— Rows with different column counts
— Area names containing spaces
— Unquoted strings (each row needs quotes)
— Using a name in the template that no item references (warning only)
— Fractional or non-integer spans implied by the shape
grid-template-areas: "header header header" "sidebar main main " "footer footer footer";
/* Invalid — main is L-shaped */ grid-template-areas: "header header" "main sidebar" "main"; ← wrong col count
grid-template-areas: "logo . nav " "hero hero hero " "main main aside";
.container {
display: grid;
grid-template-columns:200px 1fr 160px;
grid-template-rows: 64px 1fr 48px;
grid-template-areas:
"header header header" /* row 1 — 3 cols all "header" */
"sidebar main aside " /* row 2 — 3 different areas */
"footer footer footer"; /* row 3 — all "footer" */
}
/* Items self-place with matching grid-area names */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
/* Dots for empty cells */
.header-grid {
grid-template-areas:
"logo . nav" /* middle cell is empty */
"hero hero hero";
}
/* Multiple dots are also valid for empty cells */
.grid {
grid-template-areas:
"a . ."
". b ."
". . c"; /* diagonal placement */
}
Responsive Layout Reflow
The single most powerful use of grid-template-areas is responsive reflow. The HTML stays identical. The
layout transforms completely by just redefining the template strings inside media queries:
grid-template-columns: 1fr
grid-template-columns: 80px 1fr 60px
/* ── Mobile first (default) ── */
.page {
display: grid;
grid-template-columns:1fr;
grid-template-areas:
"header"
"nav"
"main"
"sidebar"
"footer";
min-height: 100vh;
}
/* ── Tablet ── */
@media (min-width: 640px) {
.page {
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"nav nav "
"sidebar main "
"footer footer";
}
}
/* ── Desktop ── */
@media (min-width: 1024px) {
.page {
grid-template-columns: 200px 1fr 160px;
grid-template-areas:
"header header header"
"nav nav nav "
"sidebar main aside "
"footer footer footer";
}
}
/* Items never change — HTML order stays the same */
.header { grid-area: header; }
.nav { grid-area: nav; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
grid-template — The Three-in-One Shorthand
grid-template is a shorthand that combines grid-template-rows, grid-template-columns, and grid-template-areas in one declaration. The syntax is compact but expressive
— each row line contains both the area names and the row height:
"area-names" row-height per row, then / column-sizes at the end after a slash. The column definition comes last,
after the slash. Row heights are optional — omit for auto.
/* Syntax: "area-names" row-height per row / col-sizes */
.page {
display: grid;
grid-template:
"header header" 64px
"sidebar main " 1fr
"footer footer" 48px
/ 200px 1fr; /* column sizes after slash */
}
/* Without explicit row heights (auto) */
.grid {
display: grid;
grid-template:
"nav nav nav "
"hero hero hero"
/ 1fr 1fr 1fr;
}
/* ⚠ grid-template does NOT include auto-rows/columns or
auto-flow — use the grid shorthand or set those separately */
/* Team preference note:
Longhand is often clearer for maintainability.
Shorthand is great for concise component styles. */
7 Real-World Layout Patterns
Every pattern below uses grid-template-areas. Each
card shows the ASCII template string, a live rendered preview, and the column definition used:
Quick Reference
| Property | Where | Value | Controls |
|---|---|---|---|
| grid-template-areas | Container | Quoted row strings | Defines named area layout |
| grid-area | Item | Area name string | Places item into named area |
| grid-template-columns | Container | Track sizes | Column widths |
| grid-template-rows | Container | Track sizes | Row heights |
| grid-template | Container | "areas" heights / cols | Shorthand: areas + rows + cols |
Try It Yourself
Build layouts with named areas. Try changing the template strings, adding or removing areas, and using dots for empty cells:
Quick Check — Chapter 32
— Each row is a quoted string of space-separated area names
— All strings must have the same number of names (equal columns per row)
— Use
. (dot, one or more) for an intentionally empty cell— Area names must be valid CSS identifiers — no spaces
Shape rules:
— An area (a name that appears in multiple cells) must form a rectangle
— L-shapes, T-shapes, and disconnected same-name areas are invalid and the entire property is ignored
Connection rules:
— Each area name in the template should have exactly one item with
grid-area: name— If an area name has no matching item, it defines an empty area (valid but nothing fills it)
— Items without a matching grid-area are auto-placed as normal
Violating rectangle or equal-column rules causes silent failure — the layout falls back to normal grid auto-placement.
Readability — The template strings form a visual picture of the layout. Reading the CSS, you can immediately see the page structure without mentally mapping numbers to positions.
Maintainability — When you modify the grid (add a column, change a breakpoint layout), you only update the template strings. Every item follows automatically. With line numbers, you'd need to update every affected item's placement values.
Responsiveness — You can completely restructure the layout for different screen sizes by redefining template strings in media queries. The HTML order and item CSS stay unchanged — only the container's template changes.
Self-documentation — Code like
grid-area: sidebar
is self-explanatory. Code like grid-column: 1 / 2; grid-row: 2 / 4
requires a mental model of the grid structure.Line numbers are still useful for component-level item placement (a featured card spanning 2 columns in a card grid) — named areas shine at the page/section layout level.
1. Default (mobile): Single column, all areas stacked vertically.
grid-template-columns: 1fr;grid-template-areas: "header" "nav" "main" "sidebar" "footer";2. Media query (tablet/desktop): Introduce multiple columns and a two-dimensional template.
grid-template-columns: 200px 1fr;grid-template-areas: "header header" "nav nav" "sidebar main" "footer footer";The HTML never changes. Items remain as
.header { grid-area: header } etc. The browser handles the reflow
automatically.This is one of the most elegant responsive layout techniques in CSS — no JavaScript, no absolute positioning, and the layout intent is readable directly from the CSS.
grid-template combines grid-template-rows, grid-template-columns, and grid-template-areas in one declaration.Syntax:
Each line has:
"area-names" row-heightAfter all rows:
/ column-sizesExample:
grid-template: "header header" 64px "sidebar main" 1fr "footer footer" 48px / 200px 1fr;This means: 3 rows (64px, 1fr, 48px) with named areas, and 2 columns (200px, 1fr).
Important limitation:
grid-template only covers the
explicit grid — it does not reset grid-auto-rows, grid-auto-columns, or grid-auto-flow. For those, use the full grid shorthand or set them separately.
Implementation:
grid-template-columns: 200px 1fr 200pxgrid-template-rows: 64px 1fr 48pxTemplate:
"header header header""sidebar main aside ""footer footer footer"The three middle columns are automatically equal height because they share the same row track (the
1fr row). This was the layout that required table hacks or complex float
tricks for over a decade. With named areas, it's 10 lines of readable CSS.
grid-template-areas, what does a . (dot) represent?grid-template-areas
values is INVALID?grid-template shorthand, where are the
column sizes written?grid-template-areas: "a a" "b b" "a a", what happens?grid-template-areasdefines layout using quoted row strings. Each string = one row, space-separated names = columns. Repeated names in adjacent cells = the item spans those cells.- Rules: Areas must be rectangular. All rows must have equal columns. Use
.for empty cells. Names are CSS identifiers (no spaces). Invalid shapes are silently ignored. grid-area: nameon items matches them to the container's template — this is the only connection needed. Items can be in any HTML order.- Responsive reflow: redefine
grid-template-columns+grid-template-areasinside media queries. HTML and item CSS never change. The layout restructures completely. grid-templateshorthand:"names" row-heightper row +/ col-sizes. Does not cover auto-rows/columns or auto-flow.- Named areas beat line numbers for page/section layouts — they're readable, self-documenting, and easy to update. Use line numbers for component-level spanning.
- 7 key patterns: classic 2-col, Holy Grail (3-col), right sidebar, full-page app, blog+ads, dashboard, landing page — all expressible in a few lines of template areas.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.