Sub topic 8
Naming areas with grid-template-areas
This chapter assumes you can write a grid container with grid-template-columns and grid-template-rows, that you know what fr units and gap do, and that you have placed items by line number. If any of that is unfamiliar, review the earlier chapters first.
Why name areas
Line numbers work, but they make a layout hard to read. The rule grid-column: 1 / 3 says nothing about what lives there. With grid-template-areas, you draw the layout as text inside the CSS. Each quoted string is one row, and each word in the string is one cell. Items then choose a region by name.
The property is set on the grid container. Items opt in with grid-area.
A classic page layout
.layout {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.layout > header { grid-area: header; }
.layout > aside { grid-area: sidebar; }
.layout > main { grid-area: main; }
.layout > footer { grid-area: footer; }Read the template row by row. Three strings give three rows. Each string has two names, which gives two columns. The name header appears twice in the first row, so the header covers both columns. The sidebar is in column 1 of row 2, and main is in column 2.
The grid-template-columns and grid-template-rows values still control track sizes. The areas only say which cells belong together. In this example the middle row is 1fr, so it takes the leftover height, and the footer is pushed to the bottom of the viewport because of min-height: 100vh.
The matching HTML is short:
<div class="layout">
<header>Site title</header>
<aside>Navigation</aside>
<main>Content</main>
<footer>Footer</footer>
</div>The source order does not have to match the visual order. You can move the sidebar after main in the HTML and the layout stays the same. Be careful with this, because keyboard focus and screen readers follow source order, not the visual layout.
How the strings are parsed
The rules are strict, and breaking them makes the whole declaration invalid. When that happens, the browser ignores the property and your items fall back to auto-placement.
- Every row string must have the same number of cells.
- Names are separated by whitespace. The amount of whitespace does not matter, so you can align columns for readability.
- A name that appears more than once must form a single rectangle. An L-shape or a split region is invalid.
- A period (
.) marks an empty cell. Several periods in a row, such as..., count as one empty cell.
This template leaves a hole in the top right and aligns the columns visually:
.dashboard {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto auto;
grid-template-areas:
"title title . "
"stats chart chart ";
}The cell in row 1, column 3 is empty. Nothing is placed there unless an item is auto-placed into it, which the next sections cover.
What grid-area does
grid-area has two roles. With a single name, such as grid-area: sidebar, it places the item in the named area. It also works as a shorthand for four line values in this order:
.item {
grid-area: 1 / 2 / 3 / 4; /* row-start / column-start / row-end / column-end */
}Note the order: row-start, column-start, row-end, column-end. This is the reverse of what many people expect, since grid-column and grid-row list the column first when written separately in the code. Use the named form when you can and avoid the four-value form unless you need it.
Implicit line names
When you define areas, the browser also creates line names for you. An area called main produces a line named main-start and a line named main-end on each axis. This means you can place an item using those names:
.banner {
grid-column: main-start / main-end;
grid-row: 1;
}The reverse also works. If you define lines named x-start and x-end, then x is usable as an area name, even if no template declares it. This connection is why areas and named lines feel like one system. Named lines get their own chapter later.
Changing the layout with media queries
Because the layout is one declaration, you can redefine it for other screen sizes. You change the template and the track sizes, and the items stay untouched.
.layout {
display: grid;
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
@media (min-width: 768px) {
.layout {
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}On narrow screens everything stacks in one column, with the sidebar after the main content. At 768px and wider, the sidebar moves beside it. The element rules with grid-area are written once.
The diagram below shows the wide layout as a set of regions.
Areas and the implicit grid
The template defines the explicit grid. Its size is the number of strings (rows) and the number of cells in each string (columns). If grid-template-rows lists fewer tracks than the template has rows, the extra rows are still created, and they are sized by grid-auto-rows, which defaults to auto. If grid-template-rows lists more tracks than the template has rows, the extra tracks exist in the explicit grid but belong to no named area.
Items that do not have a grid-area or other placement are auto-placed into cells that no named area occupies... in practice, they can still land in any free cell, including cells inside an area that no item has claimed. Areas are not reserved spaces. If you create an area named ad and nothing uses it, the cells stay empty, but auto-placed items may flow into them. To avoid surprises, give every direct child of the container an explicit grid-area.
Items can overlap areas
Two items can be given the same area. They will stack in the same cells, and the later one in source order paints on top, unless z-index says otherwise. This is useful for captions over images:
.card {
display: grid;
grid-template-areas: "stack";
}
.card > img,
.card > .caption {
grid-area: stack;
}
.card > .caption {
align-self: end;
}The single-cell template creates one area named stack. Both children go into it, and align-self: end moves the caption to the bottom of the cell.
Common mistakes
| Symptom | Likely cause |
|---|---|
| All items pile up in the first cell or flow in source order | The template is invalid, so it was ignored |
| An item ignores its area | The name in `grid-area` has a typo or different case. Area names are case-sensitive |
| Area is the wrong size | The tracks are sized by `grid-template-columns` and `grid-template-rows`, not by the template |
| Quotes missing | Each row must be its own quoted string |
| Items appear in a surprising order for keyboard users | Source order differs from visual order |
Areas or line numbers
Use named areas when the layout has a few clear regions and each region has a distinct role, such as a page shell or a dashboard. Use line numbers or spans when items repeat, when content is dynamic, or when you only need one item to cover more than one track. A gallery of 40 photos is a poor fit for areas, because you cannot name 40 regions sensibly.
Exercise
A grid has the template "nav nav" "side main" and a rule .x { grid-area: sidebar; }. The .x item is not placed where you expect. Name two separate problems in this setup.
Show answer
First, the template defines the area side, not sidebar, so the name in grid-area does not match any area and the item is auto-placed (area names must match exactly). Second, the template itself has no row of cells that is invalid here, so the only fix needed is the name, but it is also worth checking that both strings have two cells each. If either row had a different cell count, the entire template would be ignored. The primary cause is the name mismatch.
Exercise
Write a grid-template-areas value for a grid with three columns where a hero area covers the entire first row, list covers columns 1 and 2 of the second row, and aside sits in column 3 of the second row.
Show answer
grid-template-areas: "hero hero hero" "list list aside"; Both strings have three cells, and each repeated name forms a rectangle.