Sub topic 3
Naming regions with grid-template-areas
This chapter assumes you know basic CSS selectors and the box model, the HTML semantic elements header, main, aside and footer, and length units such as px, rem and fr. You should already be able to turn an element into a grid container with explicit columns. Here you will learn how to name regions of a grid and draw the layout as text.
Why name regions
With line-based placement you write rules such as grid-column: 1 / 3 and grid-row: 2. These work, but the numbers hide the layout. To see the page shape, you must picture the lines in your head.
grid-template-areas lets you draw the layout in your CSS. Each quoted string is one row. Each word in the string names one cell. When the same name appears in neighboring cells, those cells join into one area.
The HTML structure
The markup stays plain. It does not contain layout information, only meaning.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>The four children of .page will each be placed in a named region.
Writing the area map
Start with a grid container, define the column tracks, and then add the map.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }Read the map as a picture:
The map has three strings, so the grid has three rows. Each string has two names, so the grid has two columns. That matches grid-template-columns: 220px 1fr, which defines two column tracks.
What the map does and does not do
The map defines the shape of the grid and the names of its regions. It does not size the tracks. The column widths come from grid-template-columns. The row heights come from grid-template-rows, or, as here, from the content, because the rows are created automatically with auto height.
You can add grid-template-rows when you want control over height:
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
}Here the middle row takes the leftover height, so the footer sits at the bottom of the viewport even when the content is short.
Reading the syntax closely
A few details of the syntax matter.
- Each row is a quoted string. Strings are separated by whitespace, including line breaks. Putting each string on its own line is a convention that makes the map look like the layout.
- Names inside a string are separated by whitespace. Extra spaces are fine, so you can align columns for readability.
- A name is a custom identifier. You choose it. It does not have to match the HTML tag, so
sidebarcan be the area for anaside. - A period (
.) marks an empty cell. Several periods in a row, such as..., also count as one empty cell.
Here is a map that uses an empty cell:
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas:
"title title ."
"nav main ads"
"nav footer ads";
}The top right cell is empty, and nothing can be placed in it by name. It stays part of the grid and still takes space from its column track.
Placing elements with grid-area
The names in the map do nothing until an element asks for one. That is the job of grid-area. When its value is a single name, it places the element in the area with that name:
.page > header { grid-area: header; }The element then fills the whole area, however many cells that area covers. The header above covers two cells, so it stretches across both columns.
Order in the HTML does not matter
Because every child names its own area, the order of the children in the HTML no longer decides where they land. These two documents produce the same layout:
<div class="page">
<footer>Footer</footer>
<main>Main content</main>
<header>Header</header>
<aside>Sidebar</aside>
</div>The footer still appears at the bottom. This is powerful, and it carries a cost for accessibility, which a later chapter covers. Keyboard focus and screen readers follow the HTML order, not the visual order. Keep the source order meaningful and use areas to rearrange only when you have a good reason.
Elements without an area
A child that does not name an area, or names an area that does not exist, is not placed in the map. The browser places it with auto-placement into the next free cell. This often causes a surprise:
.page > aside { grid-area: sidebr; } /* typo: no such area */The sidebar loses its place. The grid does not warn you. If an element ends up in the wrong spot, check that the spelling in grid-area exactly matches the map. Names are case-sensitive.
What makes a map valid
The browser accepts a map only if it follows the rules below. If it breaks any rule, the entire grid-template-areas declaration is ignored, and the grid falls back to a single column of auto-placed items.
- Every row has the same number of cells. A map with a two-name row and a three-name row is invalid.
- Every named area is a rectangle. Cells that share a name must form a solid block. An L-shape or a U-shape is invalid.
- A name's cells must be connected. The same name may not appear in two separate places. Use a different name for each separate region.
- A map needs at least one cell. An empty string is not a valid row.
Compare a valid map with two invalid ones:
/* Valid: header and footer are rectangles */
.ok {
grid-template-areas:
"header header"
"main side"
"footer footer";
}
/* Invalid: rows have different numbers of cells */
.bad-uneven {
grid-template-areas:
"header header"
"main side extra"
"footer footer";
}
/* Invalid: "a" forms an L-shape */
.bad-shape {
grid-template-areas:
"a a"
"a b";
}The second block fails because row two has three cells while the others have two. The third fails because a covers three cells in an L-shape, which is not a rectangle.
Map and track counts
The map and the track definitions are two views of the same grid, so they should agree. If the map has three columns and grid-template-columns lists two tracks, the grid gets three columns anyway. The third column is an implicit one, sized auto. The result is rarely what you intended. Count the names in the longest row, and make grid-template-columns define the same number of tracks.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Everything stacks in one column | The map is invalid (uneven rows, non-rectangular area) | Make every row the same length and every area a rectangle |
| One element is out of place | The `grid-area` name is misspelled or the element has no `grid-area` | Match the name to the map exactly |
| An area is wider or narrower than expected | The column tracks do not match the map | Define the same number of tracks as names per row |
| Footer floats up under short content | No `grid-template-rows` and no minimum height | Set `min-height: 100vh` and use `auto 1fr auto` rows |
| Two items overlap | Two elements name the same area | Give each element its own area, or wrap them in one element |
Note the last row. Nothing stops two elements from naming the same area. They will overlap in that region. Overlap is sometimes useful, for example when you place a caption on an image, but it is a bug when it happens by accident.
Putting it together
Here is a complete page you can save and open in a browser. It uses the map from this chapter, with colored backgrounds so you can see each area.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Template areas</title>
<style>
body { margin: 0; font-family: sans-serif; }
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
min-height: 100vh;
}
.page > header { grid-area: header; background: #cfe8ff; padding: 1rem; }
.page > aside { grid-area: sidebar; background: #ffe9b8; padding: 1rem; }
.page > main { grid-area: main; background: #d6f5d6; padding: 1rem; }
.page > footer { grid-area: footer; background: #eed6f5; padding: 1rem; }
</style>
</head>
<body>
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>
</body>
</html>Try three experiments:
- Change
"footer footer"to"footer"and watch the layout collapse. The map is now invalid because the rows have different lengths. - Swap
sidebarandmainin the second row and also swap the column sizes. The sidebar moves to the right with no HTML change. - Replace the first
headerin the top row with a period. The header loses its left cell, so it no longer matches a rectangle, and the map becomes invalid.
Summary of the model
grid-template-areasdescribes a grid of cells, one string per row, one name per cell.- Repeating a name makes an area, and an area must be a rectangle.
grid-area: nameplaces an element into the named area.- Track sizes still come from
grid-template-columnsandgrid-template-rows. - A broken map is ignored as a whole, and a misspelled name quietly auto-places the element.
Exercise
This map is meant to put a sidebar on the left of a 3-row page, but the sidebar disappears into one stacked column. Find two reasons the map is invalid, and write a corrected version. grid-template-areas: "header header header" "side main" "side footer footer" "side side footer";
Show answer
Reason 1: the second row has two cells, while the first has three, so the rows are uneven. Reason 2: "side" and "footer" are not both rectangles in the last two rows. "footer" covers cells in an L-shape across rows three and four, and the fourth row also breaks the "side" shape. A corrected, three-row map with three columns: grid-template-areas: "header header header" "side main main" "side footer footer"; Every row has three cells, and every area (header, side, main, footer) is a rectangle. Set grid-template-columns to three tracks, such as 200px 1fr 1fr.
Exercise
A page has .page > aside { grid-area: sidebar; }, but the map names the region sidebar-nav. What happens, and how do you fix it?
Show answer
Nothing matches the name sidebar, so the aside is not placed in the map. The browser auto-places it into the next free cell, and the grid shows no error. Fix it by making the names match exactly, either grid-area: sidebar-nav in the rule or renaming the region in the map to sidebar.