CSS Reference ยท Grid
The grid-template-areas property
Reference for grid-template-areas: draw a grid layout with named areas as strings, leave cells empty with dots, and place items with grid-area. Examples included.
The grid-template-areas property lets you draw a layout. Each string is one row, each word in a string names one cell, and cells with the same name join into a single area. Items are then placed by name with grid-area. It gives you a layout that you can read straight from the CSS, and you can redraw it inside a media query.
CSS
.page { display: grid; grid-template-columns: 180px 1fr; grid-template-areas: "header header" "sidebar main" "footer footer"; }
Try it
Each dot is an empty cell that no area uses.
Syntax
CSS
grid-template-areas: none | <string>+;
- Initial value:
none - Applies to: grid containers
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
none |
No named areas. |
"a b c" |
One string per row; each whitespace-separated word names a column cell. |
. (one or more dots) |
An empty cell that belongs to no area. |
Usage notes
- Rectangles only. An area name must cover a rectangle of cells. An L-shaped area makes the whole declaration invalid.
- Equal rows. Every string must have the same number of cells, and the strings define how many rows and columns the explicit grid has at minimum.
- Implicit line names. An area called
mainalso creates lines namedmain-startandmain-endon both axes, so you can use them withgrid-column. - Sizes come separately. Areas only name cells. Use
grid-template-columnsandgrid-template-rowsto size them, or thegrid-templateshorthand to do both. - Responsive layouts. Override the property in a media query to restack a layout; the HTML order stays the same.
- Accessibility. The visual order can differ from the source order. Keep the DOM in a logical reading order, because keyboard focus and screen readers follow the DOM.
- Names are identifiers. Use letters, digits and hyphens, without spaces. Names are case-sensitive.
Browser support
Supported in all current browsers.
Related
grid-areaโ places an item in a named areagrid-templateโ rows, columns and areas in one declarationgrid-template-columns- Lesson: Grid Template Areas