Sub topic 8
Naming areas with grid-template-areas
This chapter assumes you can define columns and rows with grid-template-columns and grid-template-rows, and that you know how grid lines are numbered. Here you will describe a layout as a picture in CSS and then assign elements to the named regions.
The working model
grid-template-areas takes one quoted string per row. Each word in a string names one cell in that row. Cells that share a name are merged into one area. The strings must line up, so every row has the same number of words.
Each item is then placed with grid-area, which takes the area name. Behind the scenes, the browser creates implicit named lines (header-start, header-end, and so on) around each area. Because of that, areas stay tied to the track definitions you wrote.
A 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; }The template has three strings, so there are three rows. Each string has two words, so there are two columns. The header and footer names appear twice in a row, so those areas span both columns. The 1fr row gives the middle band all leftover height, which pushes the footer down on short pages.
Leaving cells empty
A period marks an empty cell. You can use several periods in a row, such as ..., and they still count as one cell.
.page {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas:
"header header header"
". main ."
"footer footer footer";
}Here main sits in the middle column, with empty cells on each side.
Common errors
- The template is ignored. Rows with different word counts make the whole declaration invalid. The browser drops it without a warning, and items fall back to auto-placement.
- Non-rectangular areas. An area must form a rectangle. An L-shape such as
"a a" "a b" "c a"is invalid. - Name typos. If
grid-area: sidbarmatches no name, the browser creates a new implicit line name instead of failing. The item then ends up auto-placed. - Mismatched track counts. The template can define more columns than
grid-template-columnslists. Extra columns become implicit tracks sized bygrid-auto-columns. Keep the two in sync.
Areas and lines together
You can mix both approaches. The implicit names let you place an item against an area's edge, for example grid-column: main-start / span 2. Use this sparingly. It is clearer to define a new area when a region needs a name.
Exercise
You write grid-template-areas: "nav nav" "side main" "side foot"; and set grid-template-columns: 200px 1fr;. Is the template valid? Which areas span more than one cell, and in which direction?
Show answer
Yes, it is valid. Every row has two words, and each named area forms a rectangle. nav spans two columns in row 1. side spans two rows in column 1. main and foot each occupy one cell.
Exercise
Write a grid-template-areas value for a narrow-screen layout that stacks header, main, sidebar, and footer in one column.
Show answer
grid-template-areas: "header" "main" "sidebar" "footer"; (with grid-template-columns: 1fr;)