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

  1. Set display: grid on the container.
  2. Define columns and rows with grid-template-columns and grid-template-rows.
  3. Create a text-based area map using grid-template-areas.
  4. 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() and minmax() for fluid columns without media queries.
  • Keep the area map visually aligned in your CSS for readability.