CSS Grid Generator · Tutorial
How to build a page layout with CSS grid areas
8 min readintermediateUpdated
Many sites need the same skeleton, a header across the top, a sidebar and main content side by side, and a footer. This tutorial builds it with named grid areas, explains each line of CSS, and adds a mobile version that stacks the sections.
What you need
- The CSS Grid Generator
- The sample from the Try it button
Steps
Set the tracks
The sample uses two columns,
220px 1fr, a fixed sidebar and a main area that takes the rest. The rows areauto 1fr auto, so the header and footer fit their content and the middle row stretches.Draw the map with areas
In grid-template-areas, each line is a row and each word is a cell.
header headermakes the header span both columns, andfooter footerdoes the same at the bottom.grid-template-areas: "header header" "sidebar main" "footer footer";Assign each item to an area
Click each item in the preview and type its area name in grid-area: header, sidebar, main and footer. The order in the HTML no longer matters; each element goes to its named place.
Copy the CSS and HTML
Copy both. In your site, use meaningful elements such as
<header>,<nav>,<main>and<footer>, and give each one itsgrid-area.Stack it on phones
Add a media query that switches to one column and a new map. Only two properties change; the items keep their area names.
@media (max-width: 700px) { .grid { grid-template-columns: 1fr; grid-template-areas: "header" "main" "sidebar" "footer"; } }
Try it yourself
Opens the CSS Grid Generator with this tutorial's sample loaded.
Common mistakes
- Rows of different lengths
- Every line in the areas map must have the same number of cells. Use a dot for empty cells, as in
header .. - L-shaped areas
- An area must be a rectangle. If you need an L shape, split it into two areas.
- Forgetting min-height
- For the footer to sit at the bottom of short pages, give the grid
min-height: 100vh(or 100dvh on mobile).
Next steps
Go back to the CSS Grid Generator, or keep going with one of these:
FAQ
Do grid areas affect screen readers?
No. Screen readers follow the HTML order, so keep the source order logical, usually header, main, sidebar, footer.
Can I mix areas with line numbers?
Yes. Items without an area can still use grid-column and grid-row.