grid-template-areas
grid-template-areas allows you to define named regions in a grid using ASCII art notation. This makes complex layouts intuitive and easy to visualize.
Template Areas Syntax
CSS
/* Define grid areas with ASCII art */
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr 60px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 20px;
}
/* Place items in areas */
header {
grid-area: header;
}
aside {
grid-area: sidebar;
}
main {
grid-area: main;
}
footer {
grid-area: footer;
}Named Area Examples
CSS
/* Dashboard layout */
.dashboard {
display: grid;
grid-template-columns: 200px 1fr 250px;
grid-template-rows: 60px 1fr 60px;
grid-template-areas:
"nav nav nav"
"sidebar main right"
"footer footer footer";
gap: 20px;
}
/* Mobile responsive areas */
.layout {
display: grid;
grid-template-areas:
"header"
"sidebar"
"main"
"footer";
grid-template-columns: 1fr;
}
@media (min-width: 768px) {
.layout {
grid-template-columns: 250px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}Complex Layout Example
CSS
/* Magazine-style layout */
.magazine {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto auto auto auto;
grid-template-areas:
"hero hero hero"
"article featured featured"
"article sidebar sidebar"
"footer footer footer";
gap: 20px;
}
.magazine > .hero { grid-area: hero; }
.magazine > article { grid-area: article; }
.magazine > .featured { grid-area: featured; }
.magazine > aside { grid-area: sidebar; }
.magazine > footer { grid-area: footer; }Note
Template areas use ASCII notation for intuitive layouts. Name regions, place elements using grid-area. Great for complex, responsive layouts.
Next
The fr unit: [The fr Unit](/css/fr-unit).