Sub topic 3
Naming regions with grid-template-areas
This chapter assumes you know basic CSS selectors and the box model, semantic HTML elements such as header, main, aside and footer, and how to make a grid with display: grid, grid-template-columns, fr units and gap. It covers the grid-template-areas property. The next chapter covers placing elements into the areas you name.
Why name regions
With numbered grid lines, a page layout needs rules like grid-column: 1 / 3. You have to count lines to see what the layout looks like. grid-template-areas lets you draw the layout as text in your stylesheet. The CSS reads like a sketch of the page.
The starting markup
Here is a plain page structure. The children are ordinary semantic elements.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>The syntax
Add grid-template-areas to the grid container. Each quoted string is one row. Each name inside a string is one cell. The names are separated by spaces.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
}This defines a grid with three rows and two columns. The column count comes from the number of names in each string. You size the columns with grid-template-columns, as before. The row heights are not set here, so they size to their content.
Reading the map
When a name repeats across neighboring cells, those cells merge into one area that spans them.
| Row string | Cell 1 | Cell 2 | Result |
|---|---|---|---|
| `"header header"` | header | header | `header` spans both columns |
| `"sidebar main"` | sidebar | main | `sidebar` is narrow, `main` is wide |
| `"footer footer"` | footer | footer | `footer` spans both columns |
The first column is 220px wide, so sidebar is 220px wide. The second column is 1fr, so main takes the remaining space.
Connecting names to elements
Naming an area does not move anything by itself. An area name is only a label until an element claims it. The grid-area property does this. It is covered fully in the next chapter. This is the minimum needed to see the layout work:
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }The area names are identifiers you choose. They do not have to match the element names. Here, aside goes into the area called sidebar.
Common mistakes
- Rows with different numbers of names. Every row string must have the same count.
"header header"followed by"sidebar"invalidates the property. - Forgetting the column sizes. The area map sets how many columns exist, but not how wide they are. Without
grid-template-columns, the columns share space equally by default sizing. - Using a name for an empty cell. To leave a cell empty, use a period:
"sidebar . main". A period stands for an unnamed cell. - Expecting areas to reorder the HTML. Placement changes the visual position only. The source order stays the same. Source order matters for keyboard and screen reader users, and a later chapter covers it.
Quick check
Exercise
A grid has three columns. You write grid-template-areas: "nav nav nav" "side main main"; and the browser ignores the whole declaration. Why, and how do you fix it if you want side in column 1 and main across columns 2 and 3?
Show answer
The first row has three names and the second row has three names, so the counts match, but the second row is side main main while the first is nav nav nav. That is valid. The actual error is that the declaration lacks the required quoting between rows only if a quote is missing, so check the quotes first. If the real string was "nav nav nav" "side main", the rows have 3 and 2 names, which is invalid. Fix it by giving both rows the same count: "nav nav nav" "side main main".