Full answer
grid-template-areas lets you draw the layout as text. Each string is a row. Each word is a name for a cell, and a name repeated across cells makes one area. A dot . means an empty cell. Areas must be rectangles.
Then each item picks its place with grid-area: name. The HTML order is unchanged, only the visual place is set in CSS.
For a typical page:
- Wide screens: header on top across both columns, sidebar on the left, main on the right, footer at the bottom.
- Small screens: one column, with the areas stacked in the order that makes sense to read.
To switch, write the one-column layout as the default and add a media query for the wide layout. This is mobile first. Only the template changes. The items keep their grid-area names.
Some points:
grid-template-columnssizes the columns.200px 1fris a fixed sidebar and a flexible main.- If an item has a name that is not in the template, the browser creates implicit tracks. Check spelling.
- Visual reordering does not change the order in which screen readers and keyboard users move through the page. Keep HTML in a logical order, and do not use areas to put important content out of sequence.
Below 600px, the four blocks are stacked: header, main, sidebar, footer. At 600px and above, the header and footer span two columns, and the sidebar sits to the left of main, although it comes after main in the HTML.
Resize the window to check both layouts, and tab through the page to see the order is still sensible.
<style>
body { margin: 0; font-family: sans-serif; }
.page {
display: grid; gap: 8px;
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "side" "footer";
}
@media (min-width: 600px) {
.page {
grid-template-columns: 160px 1fr;
grid-template-areas:
"header header"
"side main"
"footer footer";
}
}
header { grid-area: header; background: #cde; }
aside { grid-area: side; background: #fdc; }
main { grid-area: main; background: #dfc; }
footer { grid-area: footer; background: #eee; }
</style>
<div class="page">
<header id="h">Header</header>
<main id="m">Main</main>
<aside id="s">Sidebar</aside>
<footer id="f">Footer</footer>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Each string is a row; repeated names form an area.
- Areas must be rectangles; a dot is an empty cell.
- grid-area on items assigns them to a named area.
- Redefine the template in a media query to change the layout.
- Visual reordering does not change reading or tab order.
Common mistakes
- Writing an area map that is not a rectangle, which makes the whole declaration invalid.
- Using grid areas to move important content far from its place in the HTML, which confuses keyboard and screen reader users.
- Starting with the wide layout and removing parts for mobile, instead of starting with the small layout.
Follow-ups
- What happens if an item names an area that does not exist?
- How does a dot in grid-template-areas work?
Tips
- Explain that the strings are rows and the repeated names are areas.
- Mention reading order as the downside of visual reordering.