Sub topic 5
What makes an area definition valid
This chapter assumes you can write a basic grid-template-areas layout and assign items with grid-area. It covers the rules the browser enforces on an area map, and the errors you get when you break them.
The rules
A grid-template-areas value is a list of quoted strings. Each string is one row, and each name inside it is one cell. The browser accepts the whole declaration only if it passes these checks:
- Every row string has the same number of cells.
- Each name forms a single rectangle. It can span several rows and columns, but it cannot have holes or an L shape.
- A name that appears in more than one cell must be contiguous.
- Cells are separated by whitespace. A
.(one or more dots) marks an empty cell.
If any check fails, the browser discards the entire grid-template-areas declaration. It does not repair the map or ignore only the bad row. No areas exist, and items placed with grid-area: name fall back to automatic placement.
A valid map
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}Every row has two cells. header covers two cells in row one, and footer covers two cells in row three. Both are rectangles. sidebar and main are single cells.
Common invalid maps
The table shows three frequent mistakes.
| Map | Problem |
|---|---|
| `"header header"` then `"sidebar"` | Rows have different cell counts (2 and 1). |
| `"a b"` then `"a a"` | `a` covers three cells in an L shape, which is not a rectangle. |
| `"a b a"` | `a` appears twice with `b` between, so it is not contiguous. |
The uneven row is the easiest to make by accident. Here the second row has one cell, so the whole declaration is dropped:
.broken {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar";
}The fix is to add a second cell to the short row, either a new name or a dot:
.fixed {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar .";
}The . leaves the second cell of row two empty. No item can be placed there by name.
Empty cells
Use dots when you want a gap in the layout. Several dots in a row, such as ..., still count as one empty cell. Write a single . per cell to keep the strings easy to read. Align the names in columns in your source so a short row stands out.
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas:
"nav main ."
"nav main ads";
}Row one has three cells, and so does row two. nav and main each span two rows. The top-right cell is empty.
The map and the column tracks
The area map defines the grid's explicit columns and rows by its shape. If grid-template-columns lists fewer tracks than the map has cells per row, the browser still creates the extra columns from the map, and they get automatic width. If you list more tracks than the map has columns, the extra tracks exist but have no names. Keep the two counts equal so every column has the size you intended.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main";
}Debugging an area that does not appear
When an item ignores its grid-area, check these in order:
- Count the cells in each row string.
- Look for a name that forms an L shape or appears in two separate places.
- Check that the name in
grid-areamatches the name in the map exactly. Names are case-sensitive. - Open browser developer tools and turn on the grid overlay. If the declaration was dropped, the overlay shows no named areas.
The browser's CSS inspector also shows the grid-template-areas line struck through when the value is invalid.
Exercise
This map is invalid. Give two separate reasons it fails, then write a valid version that keeps the same intent (a full-width header, then a sidebar and main). "header header header" "sidebar main" "sidebar sidebar main"
Show answer
Reason one: the first row has three cells and the second row has two, so the rows are uneven. Reason two: with three columns, sidebar in rows two and three is fine only if both rows list it in matching columns, but here main sits in column two in row two and column three in row three, so main is not a rectangle. A valid version is:
"header header header"
"sidebar main main"
"sidebar main main"