Tutorials CSS Grid Areas & Templates
Ch 32 / 60
Chapter 32 Intermediate 9 min read CSS

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.

What you'll learn
Full syntax and rules for grid-template-areas
Using dots . for empty cells
Connecting containers and items with grid-area
Responsive layout reflow with media queries
grid-template shorthand
7 real-world layout patterns with live demos

How 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:

Container CSS
.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 60px 1fr 48px;
  grid-template-areas:
    "header  header"
    "sidebar main  "
    "footer  footer";
}
Items CSS
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }
Result — live rendered layout
header
sidebar
main
Why named areas beat line numbers
With line numbers: 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:

✓ Valid patterns
— Each area name forms a rectangle
— 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
✗ Invalid — silently ignored
— L-shaped or non-rectangular areas
— 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
✓ Valid — rectangular areas, consistent columns
grid-template-areas:
  "header  header  header"
  "sidebar main    main  "
  "footer  footer  footer";
✗ Invalid — L-shape, inconsistent columns
/* Invalid — main is L-shaped */
grid-template-areas:
  "header  header"
  "main    sidebar"
  "main";    ← wrong col count
Using dots for empty cells
grid-template-areas:
  "logo  .     nav  "
  "hero  hero  hero "
  "main  main  aside";
logo
.
nav
hero
main
aside
CSS — grid-template-areas syntax
.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:

Mobile (default) — single column stack
header
nav
main
sidebar

grid-template-columns: 1fr

Desktop (@media min-width:768px) — sidebar layout
header
nav
sidebar
main
aside

grid-template-columns: 80px 1fr 60px

CSS — responsive layout reflow
/* ── 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:

❌ Longhand (verbose)
.page {
  grid-template-columns: 200px 1fr;
  grid-template-rows: 64px 1fr 48px;
  grid-template-areas:
    "header  header"
    "sidebar main  "
    "footer  footer";
}
✓ Shorthand (compact)
.page {
  grid-template:
    "header  header" 64px
    "sidebar main  " 1fr
    "footer  footer" 48px
    / 200px 1fr;
}
grid-template shorthand syntax
The syntax is: "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.
CSS — grid-template shorthand
/* 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
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all CSS Grid area and template properties with visual layout patterns.
Download PDF

Try It Yourself

Build layouts with named areas. Try changing the template strings, adding or removing areas, and using dots for empty cells:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 32

Questions & Answers
Syntax rules:
— 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.
Named areas offer several advantages over numeric line 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.
The pattern uses mobile-first defaults and media queries to redefine only the container properties — the HTML and item CSS stay completely static:

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-height
After all rows: / column-sizes

Example:
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.
The "Holy Grail" is a classic web layout with five sections: a full-width header, full-width footer, and three equal-height columns in the middle (left sidebar, main content, right sidebar). It was famously difficult to achieve with floats or tables — CSS Grid makes it trivial.

Implementation:
grid-template-columns: 200px 1fr 200px
grid-template-rows: 64px 1fr 48px
Template:
"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.
Multiple Choice Questions
Question 1 of 5
In grid-template-areas, what does a . (dot) represent?
Question 2 of 5
Which of the following grid-template-areas values is INVALID?
Question 3 of 5
You want to make a layout responsive — the sidebar should stack below main on mobile and appear to the left on desktop. What should you do?
Question 4 of 5
In the grid-template shorthand, where are the column sizes written?
Question 5 of 5
If you write grid-template-areas: "a a" "b b" "a a", what happens?
Chapter Summary
  • grid-template-areas defines 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: name on 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-areas inside media queries. HTML and item CSS never change. The layout restructures completely.
  • grid-template shorthand: "names" row-height per 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.
~9 min read
Intermediate level
Chapter 32 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel