Grid Template Areas for Page Layout
What you'll learn: How to define entire page structures using a visual, ASCII-art-style syntax that makes your layout intentions crystal clear.
The Visual Blueprint Approach
Instead of placing grid items with line numbers or spans, you can draw your layout directly in CSS using grid-template-areas. Think of it like sketching your page structure with text labels—each label represents a named area where content will go.
You define the structure on the grid container using a string for each row, where each word represents a cell. Then you assign grid items to those areas using the grid-area property (which you've already learned).
How It Works
On your grid container, declare your layout "map":
.container {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
Each quoted string is one row. Column names that repeat horizontally span multiple columns. Names that appear in multiple rows span multiple rows.
Then place your items:
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
The Analogy
It's like labeling rooms on an architectural floor plan—you draw rectangles and write "kitchen," "bedroom," etc. CSS Grid lets you do the same thing with text, and the browser builds the layout to match your diagram.
Empty Cells
Use a period (.) to represent empty grid cells:
grid-template-areas:
"header header header"
". main sidebar"
"footer footer footer";
Key Takeaway: grid-template-areas lets you define entire page layouts using readable, visual ASCII-art strings that make your structure immediately obvious to anyone reading your CSS.