Grid: Areas — CSS

grid-template-areas draws the page layout as a map, written on the parent (the grid container). Each piece of text in quote marks (a string) is one row, and…

In the previous lesson, items filled the cells of the grid in order, from left to right. For a page layout, that is not enough: the header has to be at the top across the full width, the sidebar on the left, and the footer at the bottom.

Grid has a way to do this. You give each part of the page a name, such as header, sidebar and content. Then you draw the layout with those names, the way you would draw a floor plan of a house. In this lesson we call that drawing the map. For a single item that only needs to be wider, there is a shorter way: span, which makes the item cover several columns.

Draw the layout with grid-template-areas

The first rule holds the map. The four rules below it give each item an area name:

.page {
  display: grid;
  grid-template-columns: 120px 1fr;
  grid-template-areas:
    "header  header"
    "sidebar content"
    "footer  footer";
  gap: 8px;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }

The result: the header at the top across the full width, a narrow sidebar on the left, wide content on the right, and the footer at the bottom across the full width. So there are two parts to write: the map on the parent (the grid container), and an area name on each item.

To leave a cell empty, write a dot . in place of a name, for example ". footer".

Cover several columns with span

This rule makes the header cover two columns:

.header {
  grid-column: span 2;
}

grid-column: span 2 makes the item cover two columns next to each other, with no map needed. Another way is to write line numbers: grid-column: 1 / 3, which means from column line 1 to column line 3. Lines are the edges of the columns: before the first column, between two columns, and after the last column. So a two-column grid has three lines.

⚠️ The map is there, so why is the layout a mess?

💡 Change the layout without touching the HTML. Rewrite grid-template-areas inside a media query (a CSS rule that only applies at certain screen widths), for example as a single column for phone screens. Every part of the page moves, and the HTML does not change.

TL;DR: grid-template-areas draws the page layout as a map, written on the parent (the grid container). Each piece of text in quote marks (a string) is one row, and each word in it is the area name for one column. The same name side by side makes one area, and a dot . leaves a cell empty. Then each item gets grid-area: name to go into the area with that name. For one item that only needs to be wider, use grid-column: span 2 (it covers two columns).

⚡ Draw the map

Write:

.page {
  display: grid;
  grid-template-columns: 120px 1fr;
  grid-template-areas:
    "header  header"
    "sidebar content"
    "footer  footer";
  gap: 8px;
}

Meaning: grid-template-areas is a map of the page layout. Each piece of text in quote marks (a string) is one row of the grid, and each word inside it is the area name for one column. The same name side by side is joined into one area, so the header covers two columns.

⚡ Link each item to it

Write:

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer  { grid-area: footer; }
Guess first: the map is there, but the items get no grid-area. What happens?

No item goes into an area of the map. The names in the map are not linked to any element.

Meaning: grid-area: header puts .header in the area named header in the map. The name must be spelled exactly as it is in the map.

⚡ The word count must fit

Guess first: in the map "a b" "c d e", the first row has two words and the second has three. Does part of the map still work?

No. The browser ignores the whole map.

Meaning: Every string needs as many words as there are columns. A cell (one square of the grid) that you want to leave empty gets a dot ., for example ". footer".

What you will learn