CSS Grid Template Areas
Grid-template-areas lets you draw a page layout in ASCII, name each area once, and redraw the whole thing by changing the parent's template string.
- Level
- Beginner
- Published
- Updated
- Reading time
- 4 min
Try it
4 live demos
Your browser can't run this demo
It needs :has(), which this browser doesn't support yet. It works in Chrome 105+, Edge 105+, Firefox 121+ and Safari 15.4+.
Needs :has()
.ga-page {
display: grid;
grid-auto-columns: 1fr;
gap: .4rem;
grid-template-areas:
"header"
"nav"
"main"
"aside"
"";
"header header"
"nav nav"
"main aside"
"";
"header header header header"
"nav main main aside"
"";
}
One page layout at three widths, redrawn by its template
Other ways to build it
Trailhead
Walks worth the early alarm
Six short loops, each under two hours, with water and shade on the way.
See the loops
grid-template-areas:
"logo ... ... menu"
"title title title ...."
"... text text ...."
"... ... ";
Empty cells with dots
See the codeLake trail
A 6 km loop with one steep climb and a long, flat walk back along the shore.
Overlap with an area's line names
See the codeThe reading order trap
See the codeBuild it
8 snippets to copy
CSS Grid Template Areas
HTML
<div class="ga-page">
<header class="ga-header">header</header>
<nav class="ga-nav">nav</nav>
<main class="ga-main">main</main>
<aside class="ga-aside">aside</aside>
<footer class="ga-footer">footer</footer>
</div>
CSS
/* the demo radios switch between these same three
templates; on a page, media queries do it */
/* phone: one column. With grid-auto-columns: 1fr,
the template strings alone set the column count */
.ga-page {
display: grid;
grid-auto-columns: 1fr;
gap: .4rem;
grid-template-areas:
"header"
"nav"
"main"
"aside"
"footer";
}
/* each child names its area once and never changes */
.ga-header { grid-area: header; background: #bae6fd; }
.ga-nav { grid-area: nav; background: #ddd6fe; }
.ga-main { grid-area: main; background: #a7f3d0; min-height: 8rem; }
.ga-aside { grid-area: aside; background: #fde68a; }
.ga-footer { grid-area: footer; background: #fecaca; }
.ga-page > * {
padding: .5rem .7rem;
border-radius: 7px;
font-family: ui-monospace, monospace;
}
/* tablet: two columns */
@media (min-width: 40rem) {
.ga-page {
grid-template-areas:
"header header"
"nav nav"
"main aside"
"footer footer";
}
}
/* desktop: four columns, main twice as wide */
@media (min-width: 64rem) {
.ga-page {
grid-template-areas:
"header header header header"
"nav main main aside"
"footer footer footer footer";
}
}
Empty cells with dots
The landing layout here uses dots for the empty corners of a diagonal grid, and the striped background shows through every empty cell.
HTML
<div class="ga-dots">
<p class="ga-logo">
<svg viewBox="0 0 576 512" aria-hidden="true"><path fill="currentColor" d="M256.5 0c14.7 0 28.2 8.1 35.2 21l216 400c6.7 12.4 6.4 27.4-.8 39.5-7.2 12.1-20.3 19.5-34.3 19.5l-432 0c-14.1 0-27.1-7.4-34.3-19.5s-7.5-27.1-.8-39.5l216-400 2.9-4.6C231.7 6.2 243.6 0 256.5 0zM170.4 249.9l26.8 26.8c6.2 6.2 16.4 6.2 22.6 0l43.3-43.3c6-6 14.1-9.4 22.6-9.4l42.8 0-72.1-133.5-86.1 159.4zM496.5 160a80 80 0 1 1 0-160 80 80 0 1 1 0 160z"/></svg>
<span class="ga-sr">Trailhead</span>
</p>
<p class="ga-menu">Menu</p>
<h2 class="ga-title">Walks worth the early alarm</h2>
<p class="ga-text">Six short loops, each under two hours, with water and shade on the way.</p>
<p class="ga-cta">See the loops</p>
</div>
CSS
/* a dot is an empty cell; a run of dots is still one cell,
so the strings can line up like a table */
.ga-dots {
display: grid;
grid-auto-columns: 1fr;
grid-auto-rows: minmax(2.9rem, auto);
gap: .45rem;
padding: .45rem;
border-radius: 10px;
/* the stripes show through every empty cell */
background: repeating-linear-gradient(-45deg, #e7e9ee 0 6px, #ffffff 6px 12px);
grid-template-areas:
"logo ... ... menu"
"title title title ...."
"... text text ...."
"... ... cta cta";
}
.ga-dots > * {
margin: 0;
padding: .5rem .7rem;
border-radius: 7px;
background: #ffffff;
border: 1px solid #d5d9e0;
}
.ga-logo { grid-area: logo; display: grid; place-items: center; background: #ffd444; }
.ga-logo svg { width: 1.6rem; height: 1.4rem; }
.ga-menu { grid-area: menu; }
.ga-title { grid-area: title; font-size: 1.3rem; }
.ga-text { grid-area: text; }
.ga-cta { grid-area: cta; font-weight: 700; background: #57d9a3; }
/* the logo name, kept for screen readers */
.ga-sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
Overlap with an area's line names
The hero art has grid-area: hero plus grid-row-end: span 2, so it starts at its area's top line and runs one row further, under the first row of the card. The card has z-index: 1, so it sits on top at the overlap. Check "Show the areas" to see dashed outlines of where the template places each area, and watch the hero reach past its own outline.
HTML
<div class="ga-ov">
<div class="ga-hero" role="img" aria-label="Hills under a setting sun"></div>
<div class="ga-card-box">
<h2>Lake trail</h2>
<p>A 6 km loop with one steep climb and a long, flat walk back along the shore.</p>
</div>
</div>
CSS
.ga-ov {
display: grid;
grid-template-columns: 1fr minmax(0, 16rem) 1fr;
grid-template-rows: 6.5rem 3rem auto;
grid-template-areas:
"hero hero hero"
". card ."
". card .";
padding-bottom: 1rem;
border-radius: 12px;
background: #ede9e0;
}
/* starts at hero-start and runs one row past hero-end,
under the first row of the card */
.ga-hero {
grid-area: hero;
grid-row-end: span 2;
border-radius: 12px 12px 0 0;
background:
radial-gradient(circle at 72% 55%, #ffe39a 0 1.2rem, transparent 1.25rem),
linear-gradient(#2c2a6b, #b5578f 70%, #ffb06b);
}
/* later in the source, so it paints on top;
z-index makes the order explicit */
.ga-card-box {
grid-area: card;
z-index: 1;
padding: .9rem 1rem;
border-radius: 12px;
background: #ffffff;
box-shadow: 0 10px 24px rgba(0, 0, 0, .2);
}
The reading order trap
The template draws nav under the header, but in the HTML nav comes after main and aside. The small numbers on each button show the source order, which is the order Tab follows. Press Tab after the checkbox and watch focus go Home, Read the guide, Save, Trails, Maps, Contact, jumping from aside back up to nav. In a browser that supports reading-flow: grid-rows (Chromium 137 and later), the checkbox makes Tab follow the visual rows instead: Home, Trails, Maps, Read the guide, Save, Contact.
HTML
<!-- nav comes last in the HTML but is drawn second -->
<div class="ga-order">
<header class="ga-header"><button type="button">Home</button></header>
<main class="ga-main"><button type="button">Read the guide</button></main>
<aside class="ga-aside"><button type="button">Save</button></aside>
<nav class="ga-nav">
<button type="button">Trails</button>
<button type="button">Maps</button>
</nav>
<footer class="ga-footer"><button type="button">Contact</button></footer>
</div>
CSS
.ga-order {
display: grid;
grid-auto-columns: 1fr;
gap: .4rem;
grid-template-areas:
"header header header"
"nav nav nav"
"main main aside"
"footer footer footer";
/* Tab follows the rows on screen, where supported.
Without it, Tab follows the HTML: header, main,
aside, then back up to nav */
reading-flow: grid-rows;
}
.ga-order > * {
display: flex;
gap: .4rem;
padding: .45rem .55rem;
border-radius: 7px;
}
.ga-order .ga-header { grid-area: header; background: #bae6fd; }
.ga-order .ga-nav { grid-area: nav; background: #ddd6fe; }
.ga-order .ga-main { grid-area: main; background: #a7f3d0; }
.ga-order .ga-aside { grid-area: aside; background: #fde68a; }
.ga-order .ga-footer { grid-area: footer; background: #fecaca; }
.ga-order button:focus-visible {
outline: 3px solid #111827;
outline-offset: 2px;
}
Understand it
How it works and 4 properties
grid-template-areas lets you draw a layout in the stylesheet the way it looks on the page, with each area named once in the child and the whole grid written as a multi-line string in the parent. Change the string and the layout redraws itself, so a page that stacks on a phone, goes two-wide on a tablet, and splits into four columns on a desktop needs three template strings and no other layout rule.
The demo below has five areas: header, nav, main, aside, and footer. Radios switch the parent's grid-template-areas between one column (every area its own row), two columns (main and aside side by side), and four columns (nav, main, and aside in one row under the header). The columns are equal because the parent has grid-auto-columns: 1fr and no explicit column list, so the column count comes from the template string alone.
How it works
A grid with five children, each with grid-area set to its area name. The parent has display: grid, grid-auto-columns: 1fr, a gap, and grid-template-areas. Rows are not set, so they size to their content. The code block beside the layout shows the current template with each area name colored to match its box.
The template is a visual map of the grid, written row by row. "header header header header" makes the header span four columns in that row. An area can appear in multiple rows: "nav" "nav" "nav" makes nav span three rows.
Every row must have the same number of cells, and that count is the number of columns. "header header" and "main aside" both have two cells per row, so they produce a two-column grid. If the rows have different cell counts, the declaration is invalid.
A run of dots is easier to read: "logo ... ... menu" lines the columns up and makes four cells, but the serialized value collapses each run to one dot, so the computed style reads "logo . . menu".
Accessibility notes
The reading-order variant uses buttons for the demo. Real navigation should use links.
Each area in a real page needs its own content and semantics: a <header>, <nav>, <main>, <aside>, and <footer> with real headings, links, and text.
Mistakes worth avoiding
- Making non-rectangular areas. Each area has to form a single connected rectangle.
"header main header"is invalid because the header is split. - Forgetting that
grid-areais shorthand for four properties. If you setgrid-area: headerand thengrid-row-end: span 2, the span overrides the row-end value the area set. - Setting
grid-template-columnswhen you want equal columns from the template. If you set bothgrid-template-columnsandgrid-template-areas, the columns come from the explicit list and the template places areas into those tracks. - Assuming
reading-flowis supported everywhere.
Ship it
8 features in 4 browsers, 5 questions
Browser support
| Feature | ||||
|---|---|---|---|---|
grid | 57 | 52 | 10.1 | 16 |
:has() | 105 | 121 | 15.4 | 105 |
custom properties | 49 | 31 | 10 | 16 |
mix-blend-mode | 41 | 32 | 7.1 | 79 |
CSS counters | 4 | 2 | 3.1 | 12 |
reading-flow | 137 | No | No | 137 |
prefers-reduced-motion | 74 | 63 | 10.1 | 79 |
:focus-visible | 86 | 4 | 15.4 | 86 |
Grid itself landed in Chrome 57, Firefox 52, Safari 10.1, and Edge 16, and grid-template-areas was part of that first release. reading-flow is newer and experimental: it landed in Chrome 137 and Edge 137, and is not supported in Firefox or Safari. mix-blend-mode is listed because the overlap variant uses it for the area outlines.
What you can build with it
- Page layouts that reflow at breakpoints. The main demo: one column on a phone, two on a tablet, four on a desktop.
- Landing and promo layouts with empty corners. The dots variant.
- Overlapping areas. The hero variant.
- Card layouts where every card has the same internal grid. A grid of cards, each with
grid-template-areasfor its own header, image, and footer. - Form layouts. A two-column form where labels and inputs are placed by area names, and some inputs span both columns.
Frequently asked questions
What is grid-template-areas?
grid-template-areas is a way to draw a grid layout as a multi-line string in the stylesheet, where each row is one quoted line and each word is a cell. Repeating a word spans it across cells. Each child gets grid-area: <name> once, and the template places it. Change the template and the whole layout redraws.How do I make equal columns with grid-template-areas?
grid-auto-columns: 1fr and leave grid-template-columns unset.Can grid areas overlap?
grid-row-start, grid-column-start, and so on. Place an item with grid-area: <name> and then extend it past its area with grid-row-end: span 2 or a column span.Why does my tab order jump around the page?
reading-flow: grid-rows (Chromium only) to make Tab follow the visual rows.What are the dots in a grid-template-areas string?
"... ..." and ". ." both produce two empty cells.