CSS Grid

Named areas

We can divide our grid into named areas and then position the grid items not by column and row numbers, but by name with the grid-template-areas property. We can then use these names in any place a grid position is expected.

Syntax

.wrapper {
grid-template-areas: "header header"
"sidebar content"
"footer footer"
}

grid-template-areas expects a set of single or double-quoted strings. Each set represents a row, and each name inside the set represents a column. The names inside the sets must be separated by whitespace.

Code example: sidebar layout

In the example below we have written a grid layout with named areas for the four main components: header, sidebar, content and footer. The header and footer are fixed height rows, while the sidebar and content rows are flexible.

<div class="wrapper">
<div id="header">header</div>
<div id="sidebar">sidebar</div>
<div id="content">content</div>
<div id="footer">footer</div>
</div>
/* defining the grid and various named areas */
.wrapper {
display: grid;
grid-template-areas: "header header"
"sidebar content"
"footer footer";
grid-template-columns: 150px 1fr;
grid-template-rows: 100px 1fr 100px;
}
/* positioning the items on the grid using
the named areas */
#header {
grid-area: header;
}
#sidebar {
grid-area: sidebar;
}
#content {
grid-area: content;
}
#footer {
grid-area: footer;
}
header
sidebar
content
footer