Each quoted string represents one row of the grid, and each space-separated word within it names the area occupying that specific cell — repeating the same name across adjacent cells, in a single row or across multiple rows, makes that named area span across all of those cells as one combined region. An individual grid item is then assigned to one of these named areas via the separate grid-area property, matching by name, which makes the overall layout structure visually apparent directly from reading the grid-template-areas declaration itself, functioning almost like an ASCII-art sketch of the actual intended layout.
1Understanding Grid-template-areas
Each quoted string represents one row of the grid, and each space-separated word within it names the area occupying that specific cell — repeating the same name across adjacent cells, in a single row or across multiple rows, makes that named area span across all of those cells as one combined region. An individual grid item is then assigned to one of these named areas via the separate grid-area property, matching by name, which makes the overall layout structure visually apparent directly from reading the grid-template-areas declaration itself, functioning almost like an ASCII-art sketch of the actual intended layout.
grid-template-areas reads almost like a visual ASCII-art sketch of the actual intended layout structure directly in the CSS, which makes it one of the most readable, self-documenting ways to define a page's overall grid layout compared to manually calculating column/row line numbers.
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"sidebar header"
"sidebar main";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }2Practical Example
Here is a real-world application of Grid-template-areas showing how it is used in production CSS code.
.layout {
grid-template-areas:
"header header"
"nav content"
"footer footer";
}3Best Practices
Follow these guidelines when working with Grid-template-areas:
1. Use grid-template-areas for page-level layouts, like header/sidebar/main/footer, where its visual, sketch-like readability makes the overall structure immediately clear from the CSS alone
2. Assign each grid item to its named area via the grid-area property, matching the exact name used in grid-template-areas
3. Use a period to represent an intentionally empty grid cell within a grid-template-areas definition, rather than a real, occupied area name
Tip: grid-template-areas reads almost like a visual ASCII-art sketch of the actual intended layout structure directly in the CSS, which makes it one of the most readable, self-documenting ways to define a page's overall grid layout compared to manually calculating column/row line numbers.
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"sidebar header"
"sidebar main";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }