Skip to main content

CSS Grid

Grid Areas

Written by Updated Grid Areas exercises

Describe page regions with named areas and understand how the auto-placement algorithm fills a grid. The chapter combines named page regions with repeat(), auto-fill, auto-fit, implicit tracks, dense packing, and the auto-placement algorithm.

Grid areas let you draw your layout as words: "header header", "nav main", "footer footer". Each item then just says which area it belongs in. For repeated things like product cards, you do the opposite and let the grid place them automatically.

Placing items by line numbers is covered in Grid Placement, and a full worked layout in the Grid Layout Example.

Draw the page with named areas

Example

css

.page {
  display: grid;
  grid-template:
    "header header" auto
    "nav    main"   1fr
    "footer footer" auto
    / 14rem minmax(0, 1fr);
}

header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
footer { grid-area: footer; }

Each string is one row, and each word is one cell. Repeating a name, like header header, makes that area span both columns. After the slash come the column widths: a 14rem sidebar and the rest for the main content. Then each element is assigned to its area with grid-area.

To change the layout on a phone, you only redraw the strings in a media query, for example one column with "header" "main" "nav" "footer". The HTML stays the same.

Every area must be a rectangle, and a . marks an empty cell.

Let repeated content place itself

Example

css

.catalog {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
  grid-auto-flow: row;
  grid-auto-rows: minmax(10rem, auto);
}

The cards are not placed at all; the grid fills them in row by row. auto-fill makes as many 15rem-or-wider columns as fit, and grid-auto-rows sets the height of the rows the grid creates by itself.

auto-fill keeps empty columns when there are only a few items, while auto-fit stretches the items to fill the row. grid-auto-flow: dense fills gaps left by bigger items, but it can show items out of their HTML order, which is confusing for keyboard users.

Grid Area Properties

PropertyPurposeExample
grid-template-areasNames cells in the explicit grid."head head" "nav main"
grid-areaPlaces an item in a named area.grid-area: main
grid-auto-flowControls automatic item placement.grid-auto-flow: dense
grid-auto-columnsSizes implicit columns.grid-auto-columns: minmax(12rem, 1fr)

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Grid Areas example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.page {
  display: grid;
  grid-template:
    "header header" auto
    "nav    main"   1fr
    "footer footer" auto
    / 14rem minmax(0, 1fr);
}

header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
footer { grid-area: footer; }

.catalog {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
  grid-auto-flow: row;
  grid-auto-rows: minmax(10rem, auto);
}
  </style>
</head>
<body>
  <div class="page"><header>Header</header><nav>Navigation</nav><main><section class="catalog"><article>One</article><article>Two</article><article>Three</article></section></main><footer>Footer</footer></div>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
repeat() in Grid tracks57167610.1

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Notes

  • Implicit tracks are created when content lands outside the explicit grid. Size them deliberately with grid-auto-columns and grid-auto-rows.

Grid Areas exercises

One row of areas

Make the page a grid with two equal columns and one row of areas named "head head".

The HTML your code works on
<style>
  .page { width: 600px; }
</style>
<div class="page">
  <header class="head">Head</header>
  <aside class="side">Side</aside>
  <main class="main">Main</main>
  <footer class="foot">Foot</footer>
</div>

Result

Check your answer to see it here.