IntermediateLesson 15 of 306 min read

CSS Grid in practice: auto-fit, minmax and named areas

Build responsive grids in one line and draw whole page layouts with named grid areas.

In this lesson you will

  • Create a self-adjusting responsive grid
  • Draw a page layout with named areas
  • Place and align individual items
  • Build a mosaic layout

About 16 minutes

Grids that adapt by themselves

Here is a puzzle. You have a gallery of twelve cards. On a wide screen you want four across, on a tablet two or three, and on a phone one. The old way means writing several media queries. The modern way is one line of grid that works out the number of columns by itself. This lesson shows the patterns professionals use daily.

auto-fit and minmax: the responsive grid in one line

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 20px;
}

Read it slowly. minmax(240px, 1fr) says: each column is at least 240 pixels, and may grow to share the free space. repeat(auto-fit, ...) says: fit as many such columns as there is room for. On a 1000-pixel container you get four columns. Shrink to 500 and you get two. At 300 you get one. The browser does the counting every time the window changes.

Create the self-adjusting gallery with repeat(auto-fit, minmax(120px, 1fr)). Then drag your browser narrower, or just look at the result, to see how the columns settle.

<style>
 .cards { /* responsive grid */ font-family: Arial; }
 .cards div { background: #EFEAFD; padding: 24px; border-radius: 10px; text-align: center; }
</style>
<div class="cards"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>

display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px;

<style>
 .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; font-family: Arial; }
 .cards div { background: #EFEAFD; padding: 24px; border-radius: 10px; text-align: center; }
</style>
<div class="cards"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>

auto-fit versus auto-fill

They look the same until there are fewer items than columns. auto-fill keeps empty columns, so items stay at their minimum size. auto-fit collapses the empty columns, so the items stretch to fill the row. For cards, auto-fit is usually what you want. If you ever see a lonely card that stays narrow beside a blank space, try switching one for the other.

Named grid areas: drawing your layout

For a full page layout, you can literally draw it in your CSS. Give each part of the page a name, then arrange the names in a picture.

.page {
  display: grid;
  grid-template-columns: 220px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  min-height: 100vh;
  gap: 16px;
}
header  { grid-area: header; }
aside   { grid-area: sidebar; }
main    { grid-area: main; }
footer  { grid-area: footer; }

Each quoted string is a row, and each word is a cell. Repeating a name makes that area span across cells. The beauty is how easy it is to rearrange for a phone: inside a media query, redraw the picture as one column.

@media (max-width: 700px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
  }
}
Good to know

Media queries are the subject of the next lesson. Here you can already see the idea: the HTML stays exactly the same, and only the picture changes.

Build the page layout with named areas: header across the top, a sidebar on the left, main on the right, and a footer across the bottom.

<style>
 .page { display: grid; gap: 8px; font-family: Arial; /* add columns and areas */ }
 .page > * { padding: 14px; border-radius: 8px; }
 header { background: #D9CCFB; } aside { background: #DDEBFF; } main { background: #EFEAFD; } footer { background: #D9CCFB; }
</style>
<div class="page"><header>Header</header><aside>Sidebar</aside><main>Main</main><footer>Footer</footer></div>

grid-template-columns: 120px 1fr; grid-template-areas: “h h” “s m” “f f”; then grid-area for each element.

<style>
 .page { display: grid; gap: 8px; font-family: Arial; grid-template-columns: 120px 1fr; grid-template-areas: "h h" "s m" "f f"; }
 .page > * { padding: 14px; border-radius: 8px; }
 header { grid-area: h; background: #D9CCFB; } aside { grid-area: s; background: #DDEBFF; } main { grid-area: m; background: #EFEAFD; } footer { grid-area: f; background: #D9CCFB; }
</style>
<div class="page"><header>Header</header><aside>Sidebar</aside><main>Main</main><footer>Footer</footer></div>

Placing items by line numbers and names

.feature {
  grid-column: 2 / span 2;   /* start at line 2, span two columns */
  grid-row: 1 / 3;           /* from row line 1 to row line 3 */
}

You can also put an item exactly where you want, using line numbers. Items placed this way may overlap, which is a neat way to layer a caption over a picture inside one grid cell.

Alignment inside the grid

.grid { place-items: center; }          /* centre everything in its cell */
.cell { justify-self: end; align-self: start; }   /* one item only */

A magazine-style layout

.mag {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 160px;
  gap: 12px;
}
.mag .big  { grid-column: span 2; grid-row: span 2; }
.mag .wide { grid-column: span 2; }
.mag img   { width: 100%; height: 100%; object-fit: cover; border-radius: 12px; }

A few spans and the fixed row height create an editorial look that would be painful with floats or flexbox. This is the kind of layout that is now an afternoon’s work.

Subgrid and masonry: a peek ahead

Two newer features are worth knowing about. Subgrid lets a nested grid share the tracks of its parent, which is a great fix for cards whose titles, text and buttons should line up across a row. It is now supported by all major browsers. Masonry, the Pinterest-style layout with columns of uneven heights, is still arriving in browsers, so check support before using it for real.

Accessibility reminder

Like flexbox’s order, grid can place items visually anywhere, but keyboard and screen reader users follow the HTML order. Write your HTML in a logical reading order, and use grid to place it, not to rearrange it radically.

Common mistake

Defining fixed pixel columns for the whole page, such as grid-template-columns: 300px 300px 300px. It breaks on any screen narrower than 900 pixels. Prefer fr, minmax() and auto-fit.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does minmax(240px, 1fr) mean?

  1. Exactly 240px
  2. At least 240px, growing to share space
  3. At most 240px
  4. A gap of 240px

minmax sets a minimum and a flexible maximum.

What does repeat(auto-fit, ...) do?

  1. Repeats once
  2. Fits as many columns as there is room for
  3. Hides items
  4. Fixes the number to 12

The browser calculates the number of columns.

In grid-template-areas, what does a repeated name do?

  1. Causes an error
  2. Makes that area span across those cells
  3. Hides the area
  4. Sets a gap

Repeating a name spans multiple cells.

What is the difference between auto-fit and auto-fill?

  1. None ever
  2. auto-fit collapses empty columns so items stretch
  3. auto-fill is faster
  4. auto-fit only works on rows

auto-fit stretches items when there are few of them.

What should the HTML order follow?

  1. The visual order only
  2. A logical reading order
  3. Alphabetical order
  4. Random order

Assistive technology follows the HTML order.

Summary

  • repeat(auto-fit, minmax(240px, 1fr)) makes responsive grids with no media queries.
  • grid-template-areas draws the layout in text.
  • Spans create magazine layouts.
  • Keep the HTML in logical order.

Next lesson

Next: responsive design and media queries, the tools that make a layout adapt to any screen.

Questions

Is auto-fit supported everywhere?

Yes, in all current browsers.

Why is my last row stretched oddly?

auto-fit stretches items to fill each row. Use auto-fill or a max width if you want fixed card widths.

Can I use grid for a whole site layout?

Yes. Many modern sites define the page with grid and the components with flexbox.

Progress is saved in this browser.