Reading history

CSS Grid Template Areas and auto-fit

6 min read 0 views

Learn CSS grid-template-areas to draw page layouts with names, and repeat(auto-fit, minmax()) for responsive grids without media queries.

CSS Grid can describe a whole page layout as a picture made of words. With grid-template-areas, you name the regions of the page and draw where each one goes. To change the layout on a phone, you just draw a new picture. Grid can also create as many columns as will fit, with no media queries at all, using repeat(auto-fit, minmax()). These two techniques cover most real layouts.

In this lesson you will learn

  • Naming grid areas and placing elements with grid-area
  • Rearranging a layout for small screens
  • Responsive card grids with repeat(auto-fit, minmax())
  • The difference between auto-fit and auto-fill
  • Making one item span several columns

You should know the basics of CSS Grid and media queries.

Drawing a Layout With Words

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>grid-template-areas</title>
  <style>
    body { font-family: sans-serif; margin: 10px; font-size: 13px; }
    .page {
      display: grid;
      grid-template-columns: 140px 1fr;
      grid-template-rows: auto 1fr auto;
      grid-template-areas:
        "header header"
        "side   main"
        "footer footer";
      gap: 8px; min-height: 220px;
    }
    .page > * { padding: 10px; border-radius: 6px; }
    header { grid-area: header; background: #90caf9; }
    nav    { grid-area: side;   background: #a5d6a7; }
    main   { grid-area: main;   background: #fff59d; }
    footer { grid-area: footer; background: #ce93d8; }

    /* On narrow screens, redraw the map: everything in one column */
    @media (max-width: 400px) {
      .page {
        grid-template-columns: 1fr;
        grid-template-areas: "header" "main" "side" "footer";
      }
    }
  </style>
</head>
<body>
  <div class="page">
    <header>header</header>
    <nav>side (nav)</nav>
    <main>main content</main>
    <footer>footer</footer>
  </div>
</body>
</html>

Result

  • grid-template-areas takes one quoted string per row. Each word is a cell, and repeating a name across cells makes that area span them: "header header" stretches the header over both columns.
  • Each child picks its area with grid-area: header, grid-area: main and so on. The order of the elements in the HTML no longer decides where they appear.
  • A dot (.) marks an empty cell.
  • The media query redraws the map as one column for narrow screens, and moves the navigation below the main content. Only the CSS changes; the HTML stays the same.
  • Every area must be a rectangle. An L-shaped area is not allowed.

Keep the reading order sensible

Screen readers and the Tab key follow the HTML order, not the visual order. If you move things around a lot with grid areas, make sure the HTML order still makes sense on its own.

Responsive Grids Without Media Queries

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>auto-fit and minmax</title>
  <style>
    body { font-family: sans-serif; margin: 10px; font-size: 13px; }
    h4 { margin: 8px 0 4px; }
    .cards {
      display: grid;
      /* As many columns as fit, each at least 110px, sharing spare space */
      grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
      gap: 8px;
    }
    .fill { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
    .card { background: #ffe0b2; padding: 14px 8px; border-radius: 6px; text-align: center; }
    .wide { grid-column: span 2; background: #ffcc80; }
  </style>
</head>
<body>
  <h4>auto-fit with 7 cards (one spans 2 columns)</h4>
  <div class="cards">
    <div class="card wide">Featured</div><div class="card">2</div><div class="card">3</div>
    <div class="card">4</div><div class="card">5</div><div class="card">6</div><div class="card">7</div>
  </div>
  <h4>Only 2 cards: auto-fit stretches them</h4>
  <div class="cards"><div class="card">A</div><div class="card">B</div></div>
  <h4>Only 2 cards: auto-fill keeps empty columns</h4>
  <div class="cards fill"><div class="card">A</div><div class="card">B</div></div>
</body>
</html>

Result

  • minmax(110px, 1fr) means each column is at least 110px wide, and at most an equal share of the spare space.
  • repeat(auto-fit, …) creates as many such columns as fit the container. As the window narrows, cards wrap onto new rows automatically.
  • grid-column: span 2 makes the “Featured” card twice as wide.
  • The difference between the two keywords shows when there are only a few items. auto-fit collapses the empty columns, so A and B stretch across the row. auto-fill keeps the empty columns, so A and B stay card-sized.
PatternResult
repeat(3, 1fr)Always 3 equal columns
repeat(auto-fit, minmax(200px, 1fr))As many columns as fit; items stretch to fill the row
repeat(auto-fill, minmax(200px, 1fr))As many columns as fit; empty columns are kept
repeat(auto-fit, minmax(min(200px, 100%), 1fr))Same as auto-fit, but never wider than a very small screen

Common Mistakes

  1. Rows of different lengths in grid-template-areas. Every string needs the same number of cells.
  2. Non-rectangular areas, which make the whole declaration invalid.
  3. A minimum wider than a phone screen, such as minmax(400px, 1fr), which causes sideways scrolling. Wrap it in min(400px, 100%).
  4. Quoting the area name in grid-area: write grid-area: main, not grid-area: "main".

Practice Exercises

Exercise 1. Write grid-template-areas for a layout with a header on top, then three columns (left ad, main, right ad), then a footer.

Show answer
.page {
  display: grid;
  grid-template-columns: 120px 1fr 120px;
  grid-template-areas:
    "header header header"
    "ad1    main   ad2"
    "footer footer footer";
}

Exercise 2. Make a photo gallery where each photo is at least 150px wide and the photos fill each row.

Show answer
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}

Frequently Asked Questions

Can two elements share the same grid area?

Yes. Both are placed in the same cells and overlap. This is useful for laying text on top of an image; use z-index to choose which one is in front.

Should I use Grid or Flexbox for a card list?

Use Grid when you want neat columns that line up in both directions. Use Flexbox when items should keep their own widths and simply wrap, like a row of tags.

Summary

  • grid-template-areas draws a layout with named regions; grid-area places each element.
  • Redraw the areas in a media query to rearrange the page for small screens.
  • repeat(auto-fit, minmax(…)) makes responsive columns without media queries.
  • auto-fit stretches items into empty space; auto-fill keeps empty columns.

Next: CSS Cascade Layers.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *