Grid template areas
Overview
Line numbers are precise, but grid-row: 2 / 4; grid-column: 1 / 2 doesn’t tell you much at a glance. Grid template areas let you name the parts of a layout and draw it with text, like ASCII art. The CSS ends up looking like the design.
Naming areas
Two steps. First, give each item an area name:
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
footer { grid-area: footer; }Then draw the layout on the container:
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"nav main"
"footer footer";
gap: 1rem;
}- Each string is a row.
- Each word in a string is a cell; the number of words must match the number of columns.
- Repeating a name across cells makes that area span them —
header headerspans two columns. - Extra spaces are fine, so you can line the names up to make the template readable.
The rules
- Areas must be rectangles. An L-shaped area (
"a a" "a b") is invalid, and the browser ignores the whole declaration. - Every row must have the same number of cells.
- Use a
.(or several, like...) for an empty cell:
grid-template-areas:
"header header header"
"nav main ."
"footer footer footer";- Items without a
grid-areaare auto-placed into whatever cells remain (and implicit tracks if needed).
Rearranging layouts
This is where template areas shine. To change the layout for a different screen size, redraw the template — the items don’t change:
.page {
display: grid;
gap: 1rem;
grid-template-areas:
"header"
"main"
"nav"
"footer";
}
@media (width >= 48rem) {
.page {
grid-template-columns: 200px minmax(0, 1fr);
grid-template-areas:
"header header"
"nav main"
"footer footer";
}
}On phones, the navigation sits below the main content; on wider screens, it moves to a sidebar. Each layout is readable at a glance.
Do keep accessibility in mind: if the visual order differs a lot from the HTML order, keyboard and screen-reader users experience a different sequence. Moving a sidebar beside the content is fine; scrambling the order of steps in a form is not.
The grid-template shorthand
grid-template combines areas, row sizes and column sizes in one declaration — each row’s size goes after its string, and the column sizes after a slash:
.page {
display: grid;
min-height: 100dvh;
grid-template:
"header header header" auto
"nav main aside" 1fr
"footer footer footer" auto
/ 12rem 1fr 14rem;
}It’s compact and shows everything about the layout in one place — but some teams prefer separate properties for readability. Either is fine.
Named lines from areas
Every named area automatically creates named lines: header-start and header-end (for both rows and columns). You can place other items using them:
.banner { grid-column: main-start / aside-end; } /* span from main to aside */Areas vs line numbers
| Use template areas when… | Use line numbers when… |
|---|---|
| laying out major page regions | placing many repeated items |
| the layout changes at breakpoints | items span arbitrary ranges |
| you want CSS that reads like the design | overlapping items in a cell |
They mix freely: name the big regions, then place details with numbers or spans inside them.
Readability tip: align the area names in your template so the columns line up. Future you — and your teammates — will be able to see the layout without opening a browser.
A responsive article page
Use grid-template-areas to lay out a page with header, toc (table of contents), article, related and footer. On narrow screens everything is one column in this order: header, article, toc, related, footer. From 48rem wide, use two columns: the toc in a 14rem left column beside the article, and related below the article in the same column. From 72rem, use three columns with related on the right. Keep the HTML in a sensible reading order.
<div class="page">
<header>Header</header>
<article>Article</article>
<nav class="toc">Contents</nav>
<aside class="related">Related</aside>
<footer>Footer</footer>
</div>
<style>
* { box-sizing: border-box; }
.page > * { background: #eaf0fc; padding: 1rem; }
/* your code here */
</style><div class="page">
<header>Header</header>
<article>Article</article>
<nav class="toc">Contents</nav>
<aside class="related">Related</aside>
<footer>Footer</footer>
</div>
<style>
* { box-sizing: border-box; }
.page > * { background: #eaf0fc; padding: 1rem; }
.page > header { grid-area: header; }
.page > article { grid-area: article; }
.page > .toc { grid-area: toc; }
.page > .related { grid-area: related; }
.page > footer { grid-area: footer; }
.page {
display: grid;
gap: 1rem;
grid-template-areas:
"header"
"article"
"toc"
"related"
"footer";
}
@media (width >= 48rem) {
.page {
grid-template-columns: 14rem minmax(0, 1fr);
grid-template-areas:
"header header"
"toc article"
"toc related"
"footer footer";
}
}
@media (width >= 72rem) {
.page {
grid-template-columns: 14rem minmax(0, 1fr) 16rem;
grid-template-areas:
"header header header"
"toc article related"
"footer footer footer";
}
}
</style>Summary
- Name items with
grid-area: name, then draw the layout as strings ingrid-template-areas. - Each string is a row; each word is a cell; repeating a name makes an area span cells.
- Areas must be rectangles; use
.for an empty cell. - Changing only
grid-template-areas(and track sizes) in a media query rearranges the whole layout. - Named areas create named lines like
header-startandheader-endthat you can place other items on.