Skip to content
Tutorials Center
My learning

Topics

Lesson 76 of 139 ยท Grid

Grid Template Areas

Name regions of a CSS grid with grid-template-areas, place items with grid-area, leave cells empty, and rearrange a whole page layout at a breakpoint.

Line numbers are precise, but they are hard to read. Six months from now, will grid-column: 2 / 4; grid-row: 1 / 3 mean anything to you? Grid template areas let you name the parts of your layout and then draw the layout as a little text picture right in your CSS.

It is one of the most readable features in all of CSS, and it makes responsive layouts almost trivial: to rearrange the page, you redraw the picture.

Drawing a layout

Two steps. First, give each item a name with grid-area:

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

The names are your choice. They are plain identifiers, so do not quote them, and avoid spaces.

Second, on the container, draw the grid with grid-template-areas. Each quoted string is one row, and each word in a string is one cell:

CSS
.site {
  display: grid;
  grid-template-columns: 160px 1fr;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
}

The header and footer names appear twice in their rows, so they span both columns. The extra spaces that line up the columns are optional, but they make the "picture" much easier to read.

A page drawn with grid-template-areas

Change the first row to "nav header" and the nav jumps up to the top-left corner. Change the last row to "nav footer" and the nav runs all the way down. You only edit the picture; the items follow.

The rules of the picture

The browser is strict about the shape you draw. If the template breaks a rule, the whole declaration is ignored, as if you never wrote it.

  • Every row must have the same number of cells. "a a b" and "c d" together is invalid.
  • Every named area must be a rectangle. An L-shape such as "a a" over "a b" is invalid.
  • One name, one area. You cannot use the same name in two separate places.

When a layout does not appear at all, check these three rules first. DevTools can also show area names as an overlay in the grid inspector, which makes mistakes obvious.

Empty cells

To leave a cell empty, use a dot . (or several dots together, such as ..., which some people use to keep columns aligned):

CSS
.card-grid {
  grid-template-areas:
    "title  title"
    ".      price"
    "button button";
}
Empty cells with dots

How names relate to lines

Naming an area also names the lines around it. An area called main creates lines called main-start and main-end in both directions. You can use them with the placement properties from the previous lesson:

CSS
.highlight {
  grid-column: main-start / main-end;
  grid-row: main-start;
}

You can also name lines directly in your track list with square brackets, which is handy for full-bleed article layouts:

CSS
.article {
  display: grid;
  grid-template-columns:
    [full-start] 1fr
    [content-start] minmax(0, 60ch)
    [content-end] 1fr
    [full-end];
}
.article > *    { grid-column: content; }
.article > .wide { grid-column: full; }

Writing grid-column: content works because the browser looks for lines named content-start and content-end. You will build this layout in Grid Patterns.

grid-area does two jobs

grid-area is also a shorthand for all four placement lines, in the order row-start / column-start / row-end / column-end:

CSS
.box { grid-area: 2 / 1 / 4 / 3; }
/* same as grid-row: 2 / 4; grid-column: 1 / 3; */

When you give it a single name, the browser treats it as "use the area with this name". Most of the time you will use the named form, because that is the readable one.

Rearranging layouts at breakpoints

This is where areas shine. Because the layout lives in one property, a media query can swap in a completely different arrangement, and the HTML never changes.

The pattern is mobile-first: start with a single column where everything stacks, then redraw for wider screens. (More on this approach in Mobile-First CSS and Media Queries.)

One layout per breakpoint

The media query measures the preview pane, not your whole browser window, because the preview is its own page. Narrow, the main content comes before the navigation, so phone readers see the article first. Wide, the navigation moves to a sidebar. One grid-template-areas per breakpoint, and the layout is fully described in each.

Remember the source order. Moving the nav below the main content visually does not change the tab order. If keyboard users should reach the nav first, keep it first in the HTML. A "skip to content" link (see HTML Navigation) helps either way.

The grid-template and grid shorthands

You can combine rows, columns and areas in one grid-template declaration. Each row's height goes after its string, and the column sizes go after a slash at the end:

CSS
.site {
  grid-template:
    "header header" auto
    "nav    main"   1fr
    "footer footer" auto
    / 160px 1fr;
}

It is compact, but many teams find the separate properties easier to read and edit. Use whichever your team prefers. The even bigger grid shorthand also resets the implicit-grid properties, so it can surprise you; it is safest to avoid it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which template is valid?

    Every row must have the same number of cells, and each name must form a single rectangle. Only the third option follows both rules.
  2. 2. How do you leave a cell empty in grid-template-areas?

    A dot, or a run of dots with no spaces between them, marks an unnamed, empty cell.
  3. 3. What happens if your grid-template-areas value breaks one of the rules?

    An invalid template is thrown away entirely, so the grid falls back to whatever value applied before.
  4. 4. Your grid-template-areas value defines an area named sidebar. Which lines can you now refer to?

    A named area in grid-template-areas creates lines called sidebar-start and sidebar-end, in both the row and column directions.

Summary

  • Name items with grid-area: name; and draw the layout with grid-template-areas, one string per row.
  • Every row needs the same number of cells and every area must be a single rectangle, or the declaration is ignored.
  • Use . for empty cells.
  • Named areas create name-start and name-end lines; you can also name lines yourself with [brackets].
  • Redraw the template inside a media query to rearrange a whole page without touching the HTML, but keep source order sensible.

Next: Grid Alignment, where you control how items sit inside their cells and how the whole grid sits in its container.