Grid template areas

Topic 4 of 4 in Grid fundamentals6 min readBeginnerFree course

Overview

Line numbers are precise, but grid-row: 2 / 4; grid-column: 1 / 2 doesn’t tell you much at a glance. Grid template areas let you name the parts of a layout and draw it with text, like ASCII art. The CSS ends up looking like the design.

Naming areas

Two steps. First, give each item an area name:

CSS
header  { grid-area: header; }
nav     { grid-area: nav; }
main    { grid-area: main; }
footer  { grid-area: footer; }

Then draw the layout on the container:

CSS
.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
  gap: 1rem;
}

grid-template-areas: “header header” “nav main” “footer footer” with grid-template-columns: 200px 1fr gives a wide layout: header across the top, nav beside main, footer across the bottom. For narrow screens, grid-template-areas: “header” “main” “nav” “footer” stacks them in one column, with main before nav.

  • Each string is a row.
  • Each word in a string is a cell; the number of words must match the number of columns.
  • Repeating a name across cells makes that area span them — header header spans two columns.
  • Extra spaces are fine, so you can line the names up to make the template readable.

The rules

  • Areas must be rectangles. An L-shaped area ("a a" "a b") is invalid, and the browser ignores the whole declaration.
  • Every row must have the same number of cells.
  • Use a . (or several, like ...) for an empty cell:
CSS
grid-template-areas:
  "header header header"
  "nav    main   ."
  "footer footer footer";
  • Items without a grid-area are auto-placed into whatever cells remain (and implicit tracks if needed).

Rearranging layouts

This is where template areas shine. To change the layout for a different screen size, redraw the template — the items don’t change:

CSS
.page {
  display: grid;
  gap: 1rem;
  grid-template-areas:
    "header"
    "main"
    "nav"
    "footer";
}

@media (width >= 48rem) {
  .page {
    grid-template-columns: 200px minmax(0, 1fr);
    grid-template-areas:
      "header header"
      "nav    main"
      "footer footer";
  }
}

On phones, the navigation sits below the main content; on wider screens, it moves to a sidebar. Each layout is readable at a glance.

Do keep accessibility in mind: if the visual order differs a lot from the HTML order, keyboard and screen-reader users experience a different sequence. Moving a sidebar beside the content is fine; scrambling the order of steps in a form is not.

The grid-template shorthand

grid-template combines areas, row sizes and column sizes in one declaration — each row’s size goes after its string, and the column sizes after a slash:

CSS
.page {
  display: grid;
  min-height: 100dvh;
  grid-template:
    "header header header" auto
    "nav    main   aside"  1fr
    "footer footer footer" auto
    / 12rem 1fr 14rem;
}

A classic page layout with grid: header across the top, nav, main and aside in the middle row, footer across the bottom. Code: body { min-block-size: 100dvh; display: grid; grid-template: “header header header” auto “nav main aside” 1fr “footer footer footer” auto / 12rem 1fr 14rem; }. The 1fr middle row grows, so the footer stays at the bottom on short pages.

It’s compact and shows everything about the layout in one place — but some teams prefer separate properties for readability. Either is fine.

Named lines from areas

Every named area automatically creates named lines: header-start and header-end (for both rows and columns). You can place other items using them:

CSS
.banner { grid-column: main-start / aside-end; }   /* span from main to aside */

Areas vs line numbers

Use template areas when… Use line numbers when…
laying out major page regions placing many repeated items
the layout changes at breakpoints items span arbitrary ranges
you want CSS that reads like the design overlapping items in a cell

They mix freely: name the big regions, then place details with numbers or spans inside them.

Readability tip: align the area names in your template so the columns line up. Future you — and your teammates — will be able to see the layout without opening a browser.

Exercise

A responsive article page

Use grid-template-areas to lay out a page with header, toc (table of contents), article, related and footer. On narrow screens everything is one column in this order: header, article, toc, related, footer. From 48rem wide, use two columns: the toc in a 14rem left column beside the article, and related below the article in the same column. From 72rem, use three columns with related on the right. Keep the HTML in a sensible reading order.

HTML
<div class="page">
  <header>Header</header>
  <article>Article</article>
  <nav class="toc">Contents</nav>
  <aside class="related">Related</aside>
  <footer>Footer</footer>
</div>

<style>
  * { box-sizing: border-box; }
  .page > * { background: #eaf0fc; padding: 1rem; }
  /* your code here */
</style>

Summary

  • Name items with grid-area: name, then draw the layout as strings in grid-template-areas.
  • Each string is a row; each word is a cell; repeating a name makes an area span cells.
  • Areas must be rectangles; use . for an empty cell.
  • Changing only grid-template-areas (and track sizes) in a media query rearranges the whole layout.
  • Named areas create named lines like header-start and header-end that you can place other items on.