The grid-template-areas
property in CSS Grid Layout allows you to assign names to grid areas, creating a visual representation of your layout. This makes your CSS more readable and easier to maintain, especially for complex designs.
Example 1: Basic Website Layout
.container {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-gap: 10px;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Explanation
This code defines a classic website layout. The grid-template-areas
property maps out the structure, and each grid item is assigned to its named area using the grid-area
property.
Example 2: Two-Column Blog Layout
.blog-layout {
display: grid;
grid-template-areas:
"post meta"
"post comments";
grid-template-columns: 3fr 1fr;
grid-gap: 15px;
}
.post { grid-area: post; }
.meta { grid-area: meta; }
.comments { grid-area: comments; }
Explanation
Here, we create a two-column layout where the main post content spans two rows on the left, while the metadata and comments occupy the right column.
Example 3: Empty Grid Cells
.card-layout {
display: grid;
grid-template-areas:
"image title"
"image ."
"image cta";
grid-template-rows: auto 1fr auto;
}
.card-image { grid-area: image; }
.card-title { grid-area: title; }
.card-cta { grid-area: cta; }
Explanation
A period (.
) in the grid-template-areas
declaration signifies an empty or unoccupied grid cell, allowing for more complex and asymmetrical layouts.
Example 4: Responsive Layout
.responsive-container {
display: grid;
grid-template-areas:
"hero"
"content"
"aside";
}
@media (min-width: 768px) {
.responsive-container {
grid-template-areas: "hero hero" "content aside";
}
}
Explanation
This example demonstrates how to create a responsive layout. On smaller screens, the areas are stacked; on screens 768px or wider, they rearrange into a two-column format.
Example 5: Overlapping Areas
.overlapping-layout {
display: grid;
grid-template-areas:
"background background"
". foreground";
}
.background-item { grid-area: background; z-index: 1; }
.foreground-item { grid-area: foreground; z-index: 2; }
Explanation
Grid areas can be made to overlap. You can then control the stacking order of the items within those areas using the z-index
property.
Example 6: Centered Content
.centered-layout {
display: grid;
height: 100vh;
grid-template-areas:
". . ."
". content ."
". . .";
grid-template-columns: 1fr auto 1fr;
grid-template-rows: 1fr auto 1fr;
}
.content-box { grid-area: content; }
Explanation
By using empty grid cells around a named area, you can easily center content both horizontally and vertically within its parent container.
Example 7: Complex Dashboard Layout
.dashboard {
display: grid;
grid-template-areas:
"header header header"
"nav main charts"
"nav main stats";
grid-template-columns: 200px 1fr 250px;
grid-template-rows: auto 1fr auto;
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.charts { grid-area: charts; }
.stats { grid-area: stats; }
Explanation
grid-template-areas
is highly effective for organizing complex layouts like dashboards, making the structure immediately clear from the CSS itself.