devcrea

grid-template-areas: Named CSS Grid Layouts

How to use grid-template-areas to build a readable, named CSS Grid layout, plus a full responsive dashboard example and common mistakes to avoid.

// The short answer

grid-template-areas replaces hard-to-read row and column coordinates with named regions such as "header header" "sidebar main". Assign matching grid-area names to the child elements, keep the strings rectangular, and change the map inside a media query when the mobile layout needs a different visual order.

Full walkthrough, edge cases and why it works
In this guide · 5 sections
  1. 01The Visual Power of Named Grid Areas
  2. 02Basic Syntax: Defining Your Grid
  3. 03Real-World Example: Responsive Dashboard Layout
  4. 04Common Mistakes and Invalid Grids
  5. 05grid-template-areas vs. Line Numbers

Tracking down cryptic grid column and row numbers across multiple media queries creates fragile, hard-to-maintain frontend architecture. Switching to the grid-template-areas property solves this by replacing invisible coordinates with a visual, ASCII-style map directly inside your CSS. This approach transforms complex responsive shifts into instantly readable code.

  • Browser Support: Widely supported (Chrome 57+, Safari 10+, Edge 16+, Firefox 52+)
  • Default Value: none
  • Applies To: Grid containers (display: grid)
  • Prerequisite: Child elements must have assigned grid-area names

The Visual Power of Named Grid Areas

CSS Grid usually forces you to think in terms of start and end lines. When layouts get complex, keeping track of grid-column: 1 / 3 or grid-row: 2 / 5 becomes a heavy cognitive load.

Named grid areas flip this logic entirely. You name your layout components first, then arrange those names into a string that literally looks like the layout you want to build. Anyone looking at your stylesheet instantly understands the page structure without needing to inspect the HTML or open the browser dev tools.

Diagram of a browser layout divided into named grid areas for header, sidebar, main content, and footer.
Naming each region of the layout turns an abstract grid of numbered lines into a readable, self-documenting page structure.

Basic Syntax: Defining Your Grid

To use this layout method, you need two things: a parent container with the template map, and child elements tagged with corresponding names. You define the map using strings, where each string represents a row, and each word inside the string represents a column cell.

Assigning grid-area to Child Elements

Before mapping the layout, tell CSS which element corresponds to which name. You do this using the grid-area property on the child elements. The names you choose do not need quotes.

css4 lines
.site-header { grid-area: header; }
.site-nav { grid-area: sidebar; }
.site-main { grid-area: content; }
.site-footer { grid-area: footer; }

Once your elements have their tags, you arrange them in the parent container. Repeating a name across multiple cells merges those cells into a single, larger area.

css9 lines
.container {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 40px;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}

Creating Empty Cells with Dots

Sometimes you need empty space in your grid design. You can create unnamed, empty cells by using a period (.) or a sequence of periods (like ...).

This is incredibly useful for asymmetrical layouts or floating elements. Just ensure the dots are separated from your named areas by a space.

css7 lines
.hero-section {
  display: grid;
  grid-template-areas:
    "image text text"
    "image . ."
    "image button .";
}

Real-World Example: Responsive Dashboard Layout

To see the real power of grid-template-areas, look at how it handles responsive design. Let's build a standard admin dashboard. It needs a top header, a left sidebar for navigation, a main content area, and a footer.

Desktop View Configuration

On wide screens, the sidebar sits on the left, spanning the middle and bottom rows. The header spans the entire top row.

css10 lines
.dashboard {
  display: grid;
  min-height: 100vh;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "sidebar footer";
}

Mobile Stacking with Media Queries

When the screen size drops, line-based grids require you to manually redefine start and end points for every single element. With grid-template-areas, you leave the child elements completely alone. You only rewrite the visual map on the parent container.

css11 lines
@media (max-width: 768px) {
  .dashboard {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas:
      "header"
      "sidebar"
      "main"
      "footer";
  }
}

The layout instantly stacks vertically. You achieve a complete structural overhaul just by changing four lines of highly readable text.

Common Mistakes and Invalid Grids

Even though the syntax is forgiving, a few strict rules dictate whether your grid map will render or completely break.

  • Non-rectangular areas: a named area must form a perfect rectangle. You cannot create "L" or "T" shaped areas. If you try to wrap a sidebar under the main content using the same name, the entire declaration becomes invalid.
  • Mismatched columns: every row string must contain the exact same number of cells (words or dots). If row one has three columns, row two must also have three columns.
  • Missing quotes: each row must be wrapped in its own set of quotation marks.

grid-template-areas vs. Line Numbers

Choosing between named areas and numerical lines depends on the specific needs of your UI component, and often comes down to the same Grid vs. Flexbox tradeoffs you weigh at the macro-layout level.

Feature grid-template-areas Numerical Placement (grid-column / grid-row)
Readability High. The CSS visually mirrors the final layout. Low. Requires mental math to visualize the grid.
Maintenance Easy. Rearrange the entire layout in one parent block. Hard. Requires updating values on every individual child element.
Overlapping Elements Not supported. Areas cannot overlap each other. Fully supported. Multiple elements can share the same numerical tracks.
Complex/Dynamic Grids Limited. Best for high-level macro layouts (page shells). Ideal. Better for dense micro-layouts and auto-placement algorithms.

Quick answers

Frequently asked questions

Can you use grid-template-areas without grid-area?

No, the container will not know where to place elements unless the children have assigned grid-area names. If you omit grid-area on child elements, browsers place them automatically based on the standard auto-placement algorithm instead of your template map. Both properties must work together.

Can grid-template-areas make an L-shaped area?

No, every named area must form an unbroken rectangle or square across rows and columns. Creating an 'L' shape, 'T' shape, or horseshoe layout with identical names produces an invalid CSS declaration. When this happens, browsers ignore the entire template definition.

How do you leave an empty cell in grid-template-areas?

You place a period character (.) or multiple consecutive periods (...) in the cell location where you want whitespace. The parser treats these full stops as null cell tokens, preventing elements from landing there unless explicitly forced. Ensure each period has whitespace separating it from neighboring area names.

Does grid-template-areas affect HTML source order and accessibility?

Yes, rearranging visual order with grid areas disconnects visual flow from DOM order. Keyboard navigation and screen readers continue following the underlying HTML structure, not your CSS visual placement. Always align key reading order in HTML to maintain accessible tab indexes.

Why is my grid-template-areas property not working?

The most common culprit is an irregular matrix where rows contain an unequal number of columns. Non-rectangular area shapes and missing quotation marks around individual row strings also invalidate the entire property instantly. Check your dev tools console to spot invalid property flags.

Can you overlap items when using grid-template-areas?

No, named area strings assign mutually exclusive spatial cells, meaning two names cannot occupy the exact same slot in the template map. If you need layered or overlapping components, target explicit line numbers with grid-column and grid-row instead.

How do media queries work with grid-template-areas?

You only redefine the grid-template-areas map on the parent container inside your media query rule. The child elements retain their existing grid-area tags, so no changes to individual component styles are needed. This keeps responsive breakpoints exceptionally clean and centralized.

Do area names inside grid-template-areas require quotation marks?

The entire row string requires quotation marks, but the area names inside that string must not be quoted individually. For child elements, assign the name directly to grid-area as an unquoted custom identifier (such as grid-area: sidebar). Adding quotes to individual token names breaks layout matching.

Something wrong or out of date?

Tools change fast. Tell the editor what no longer works.