How to create a CSS Grid layout with named grid areas
· Category: HTML & CSS
Short answer
CSS Grid's grid-template-areas lets you define a visual map of your layout in CSS, assigning names to elements and rearranging them easily for different breakpoints.
Steps
- Set
display: gridon the container. - Define columns and rows with
grid-template-columnsandgrid-template-rows. - Create a text-based area map using
grid-template-areas. - Assign each child to an area with
grid-area.
Example
.layout {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
gap: 1rem;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }
Tips
- Named areas make responsive redesigns trivial: just redefine the template string in a media query.
- Use
repeat()andminmax()for fluid columns without media queries. - Keep the area map visually aligned in your CSS for readability.