Sub topic 5
What makes an area definition valid
This chapter assumes you can write a basic grid container, size columns with fr units and gap, and place items with grid-area. Those topics come earlier in this tutorial. Here the focus is the rules a grid-template-areas value must follow. Browsers do not repair a broken map. They throw away the whole declaration, often without any visible error.
Why validity matters
A grid-template-areas value is a picture of your layout written as text. The browser reads it as a table of cells. If the table does not describe a clean set of rectangles, the browser cannot turn it into grid areas. It then treats the declaration as invalid and ignores it, as it does with any invalid CSS value.
The result is hard to spot. Your grid-area rules still run, but no named areas exist for them to match. Items fall back to automatic placement and stack in source order. Nothing in the console tells you why.
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>All the examples below use this markup.
The rules
Four rules make an area definition valid.
- Every row is a quoted string.
- Every row has the same number of cells.
- Each area name forms one solid rectangle.
- Each cell is either a valid name or a null cell token (
.).
Rule 1: each row is a quoted string
Each row of the map is a separate string in quotes. Single or double quotes both work. Unquoted names are a syntax error.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}The line breaks are only for readability. The browser sees three strings, so the following is equivalent:
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas: "header header" "sidebar main" "footer footer";
}Aligning the names in columns, as the multi-line form allows, is the easiest way to check the rules by eye.
Rule 2: every row has the same number of cells
Inside a string, whitespace separates cells. The cell count of every string must match. This map is invalid because the second row has one cell and the others have two:
.page {
display: grid;
grid-template-columns: 220px 1fr;
/* INVALID: row 2 has 1 cell, rows 1 and 3 have 2 */
grid-template-areas:
"header header"
"main"
"footer footer";
}A missing name is the usual cause. You may mean main to stretch across the row, but the browser will not guess. Write "main main".
Rule 3: each name must form one rectangle
Each name must cover a contiguous block that is a perfect rectangle. Repeating a name across adjacent cells in a row or column makes the area span them. Three shapes break this rule.
- L-shapes and other non-rectangles. The name covers a block with a corner missing.
- Disconnected repeats. The same name appears in two places with other cells between them.
- Diagonal-only contact. Two cells touch only at a corner.
.page {
display: grid;
grid-template-columns: 220px 1fr;
/* INVALID: "sidebar" is an L-shape */
grid-template-areas:
"sidebar sidebar"
"sidebar main"
"footer footer";
}The sidebar cells in this map are the whole first row plus the left cell of the second row. That is an L, not a rectangle, so the whole declaration is dropped.
This one is also invalid, because the name header is split by another area:
.page {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
/* INVALID: "header" appears twice with "main" between */
grid-template-areas:
"header main header"
"footer footer footer";
}The fix is to redesign the shape so it is solid. Here you would give the two outer cells different names, such as left and right.
Rule 4: names are identifiers, and `.` means empty
A name must be a valid CSS identifier. It cannot start with a digit, so "1col" is invalid. Names are case-sensitive, so Main and main are two different areas. Hyphens and underscores are fine.
To leave a cell empty, use the null cell token, a full stop:
.page {
display: grid;
grid-template-columns: 220px 1fr 120px;
grid-template-areas:
"header header header"
"sidebar main ."
"footer footer footer";
}The right cell of the middle row stays empty. No item can be placed there by name, though auto-placed items can still fall into it.
A run of adjacent dots counts as one token, not several. "sidebar main ..." has three cells, and the ... is a single null cell. To leave three cells empty, separate the dots with spaces: ". . .".
Null cells have one rule of their own. They do not have to form rectangles, because they are not an area. Several separate . cells in one map are fine.
Summary of valid and invalid maps
| Map | Valid? | Reason |
|---|---|---|
| `"a a" "b c"` | Yes | `a` is a 2-by-1 rectangle |
| `"a a" "b"` | No | Rows have different cell counts |
| `"a a" "a b"` | No | `a` is an L-shape |
| `"a b a"` | No | `a` is disconnected |
| `"a ." ". b"` | Yes | Null cells need not be rectangles |
| `"a b" "a b"` | Yes | Each name is a 1-by-2 rectangle |
| `"1a b"` | No | `1a` is not a valid identifier |
| `a b` (no quotes) | No | Rows must be quoted strings |
A validation checklist
You can run any map through the same sequence of questions. The diagram shows the order.
How areas relate to the track lists
The area map and the track lists describe the same grid, and the browser reconciles them.
- The number of strings sets the number of explicit rows.
- The number of cells per row sets the number of explicit columns.
grid-template-columnsandgrid-template-rowssize those tracks.
A mismatch between the map and the track lists is not a syntax error. It is a layout surprise. If the map has two columns but grid-template-columns lists three tracks, the grid has three explicit columns. The third column has no names in it. If the track list is shorter than the map, the extra columns exist but are sized automatically, which is rarely what you want.
.page {
display: grid;
/* Map has 2 columns; this lists 3 tracks, so a third unnamed column appears */
grid-template-columns: 220px 1fr 100px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}The simple habit is to count the columns in your map and write a track list with the same number of entries.
Using the areas: what a missing match does
The areas only matter once items refer to them. grid-area: sidebar looks up an area with that exact name.
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }If a name is misspelled (grid-area: sidbar), no area matches. The browser does not report an error, and the item is not placed in any area you drew. It ends up auto-placed or in an extra implicit track, so your layout looks scrambled. A later chapter covers the implicit named lines that make this work.
Two symptoms point to a validity problem rather than a typo.
- Every item stacks in one column in source order. The whole map was probably dropped.
- Only one item is out of place. Probably one
grid-areaname does not match.
Edge cases
An area that is a single cell
A name used once is a valid 1-by-1 rectangle. Single-cell areas are common for small widgets such as a logo or a button.
An area that spans rows and columns
A name may cover a block in both directions, as long as the block is a rectangle.
.page {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto auto auto;
grid-template-areas:
"hero hero side"
"hero hero side"
"a b c";
}Here hero is 2 by 2 and side is 1 by 2. Both are valid.
Whitespace
Extra spaces or tabs between cells do not matter. Only the cell count and the names do. Use the spacing to line up columns, which makes shape errors visible.
Rules are about the string, not the HTML
The map never needs to match the HTML structure. Items can appear in any DOM order, and grid-area places them by name. Keep in mind that this also lets visual order drift from source order. Chapter 13 covers the accessibility consequences.
A repaired example
Here is an invalid map and its fix, in full.
.page {
display: grid;
grid-template-columns: 220px 1fr;
gap: 1rem;
/* INVALID: sidebar is an L-shape and row 3 is short */
grid-template-areas:
"header header"
"sidebar main"
"sidebar"
}There are two faults. The last row has one cell, and sidebar would be a 1-by-2 block in the first column only if the last row were "sidebar footer". The author probably wants a footer across the bottom. A valid version:
.page {
display: grid;
grid-template-columns: 220px 1fr;
gap: 1rem;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.page > header { grid-area: header; }
.page > aside { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }Every row has two cells, each name is a rectangle, and every name has a matching grid-area rule.
Exercise
This map is meant to place a sidebar on the left spanning two rows, with a header and main content on the right. Is it valid? If not, name the broken rule and fix it. grid-template-columns: 200px 1fr; grid-template-areas: "sidebar header" "sidebar main" "main";
Show answer
It is invalid. The third row has one cell while the others have two, which breaks the equal-cell-count rule, so the whole declaration is ignored. The sidebar and header are fine. The intended layout needs a full-width row at the bottom or a third cell, so one fix is "sidebar header" / "sidebar main" / "footer footer", with a footer item given grid-area: footer.
Exercise
Which of these maps is valid, and why do the others fail? (a) "nav nav" "main aside" "nav footer" (b) "top top top" "left . right" (c) "a b" "b a"
Show answer
Only (b) is valid. In (a), nav occupies the first row and the left cell of the third row with main in between, so it is disconnected. In (c), a and b each touch only at corners, so neither is a rectangle. In (b), top is a 3-by-1 rectangle, left and right are single cells, and the null cell is allowed.