CSS3 Intermediate Modern Layout · 9 min read

CSS Grid: minmax, auto-fit, and grid-template-areas

Lesson 10 of 41 in the CSS3 learning path.

How do I make a responsive card grid without media queries?

This is the rest of Grid after a fixed two-column layout. repeat(auto-fit, minmax(14rem, 1fr)) tells the browser to create as many columns as will fit, each at least 14rem and sharing extra space equally. When the container gets narrower, columns drop away and the cards reflow, so a card grid becomes responsive without a media query. auto-fit collapses empty tracks, which is what you want when there are fewer cards than columns. gap is the only gutter you need between those tracks.

Named areas describe a whole page in a picture. grid-template-areas lists strings of cell names, and each child claims a name with grid-area. The sample builds a course home: a header across the top, a nav, a main region that holds the auto-fit cards, and a footer. Every area is a rectangle. The card grid inside main is a second grid, because nesting grids is how a page shell and a card gallery stay independent.

Syntax & Parameters

Syntax
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1rem; }
.page { grid-template-areas: "header header" "nav main" "footer footer"; }
.main { grid-area: main; }

Example

Interactive preview available
styles.css
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.45;
  color: #1c2430;
  background: #f4f1ea;
}
.home {
  display: grid;
  grid-template-columns: 11rem minmax(0, 1fr);
  grid-template-areas:
    "header header"
    "nav main"
    "footer footer";
  gap: 0.75rem;
  max-width: 62rem;
  margin-inline: auto;
  padding: 0.85rem;
}
.home-nav { grid-area: nav; }
.home-main { grid-area: main; min-width: 0; }
.panel {
  padding: 0.75rem 0.85rem;
  background: #fff;
  border: 1px solid #e3ddd2;
  border-radius: 12px;
}
.home-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}
.home-header h1 { margin: 0; font-size: 1.25rem; }
.home-header a { color: #0f766e; font-weight: 600; }
.home-nav h2 {
  margin: 0 0 0.45rem;
  color: #0f766e;
  font-size: 0.75rem;
  text-transform: uppercase;
}
.home-nav ul {
  display: grid;
  gap: 0.35rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.home-nav a { color: #1c2430; font-weight: 600; text-decoration: none; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: 0.75rem;
}
.card h2 { margin: 0 0 0.25rem; font-size: 1rem; }
.card p { margin: 0; color: #5c6b7a; }
.card span {
  display: block;
  margin-top: 0.45rem;
  color: #0f766e;
  font-size: 0.8rem;
  font-weight: 600;
}
.home-footer {
  grid-area: footer;
  color: #5c6b7a;
  font-size: 0.85rem;
}

Expected Output

The page is a grid picture: a header bar across the top, an 11rem nav on the left, a main region on the right, and a footer across the bottom. A 0.75rem gap separates those regions. The header title and the New lesson link sit on opposite ends of the bar.

Inside main, the lesson cards form as many columns as will fit at 14rem each. In a wide main you see two or more cards on a row, each sharing the leftover space, with the same 0.75rem gap. In a narrow main the cards stack to one column. Empty columns do not remain. The nav does not stretch under the footer, and the cards do not escape the main track.

Line-by-Line Walkthrough

  1. .home is the page grid. grid-template-columns makes a narrow nav track and a main track that can shrink, using minmax(0, 1fr).
  2. grid-template-areas draws the page in three strings. Each string is one row, and each quoted name is one cell. Repeated names merge cells.
  3. header header means the header spans both columns. footer footer does the same on the last row. nav and main each take one cell.
  4. grid-area: header on the header, and the matching names on nav, main, and footer, pull those elements into the picture. Source order no longer decides the cell.
  5. Every area in this picture is a rectangle. A name that makes an L shape is invalid and the whole template is dropped.
  6. .card-grid is a second grid, nested in main. Its columns are repeat(auto-fit, minmax(14rem, 1fr)), so each card is at least 14rem and grows to share leftover space.
  7. auto-fit collapses empty tracks. Three cards on a wide row grow to fill the main area instead of leaving blank columns.
  8. auto-fill would keep those empty tracks, so the cards would stay near 14rem and the row would show unused columns. This gallery wants auto-fit.
  9. gap: 0.75rem on both grids is the gutter. The page gap separates header, nav, main, and footer. The card gap separates cards only.
  10. min-width: 0 on main lets the inner auto-fit grid shrink with the track. Without it, the cards can force the page wider than the viewport.
  11. The header uses flex only for its own row, the title and the link. The page shell stays grid. One dimension inside a component, two dimensions for the page.

Narrow the frame and the card columns drop away one at a time. The named areas stay a header, a nav, a main, and a footer until a media query stacks the shell.

Common Pitfalls & Security

auto-fit looks broken when the minimum in minmax is larger than the container, and named areas fail silently when the picture is not a rectangle or the strings have different lengths.

  • minmax(14rem, 1fr) cannot create a column if the container is under 14rem. The grid then overflows instead of wrapping to a smaller minimum. Pick a minimum that fits the smallest pane you care about.
  • auto-fill keeps empty tracks. If the cards stay narrow and a blank region sits beside them, you asked for fill, not fit.
  • Mismatched area strings, or a name that does not form a rectangle, invalidate the template. The items fall back to auto placement and the picture disappears.
  • grid-area names must match the strings exactly. A typo in main leaves that element in auto placement, often under the footer.
  • A nested auto-fit grid whose parent has min-width: auto can widen the page shell. Set min-width: 0 on the grid item that contains the cards.

Best Practices & Modern Standards

Use named areas for the page shell and auto-fit for the repeating cards inside one of those areas. Keep the shell tracks few and rectangular, and let the card grid own its own columns so a new card does not force you to redraw the page. gap is the gutter on both grids. Prefer auto-fit when leftover space should go to the cards you actually have.

A media query is still the right tool when the shell itself must change, for example when the nav should move above the cards on a phone. Container queries are the right tool when one card should switch layout because its cell got wider, not because the viewport did. Custom properties can hold the 14rem minimum so a dense catalog can lower it in one place.

Real-World Use

A course home is a header, a studio nav, a responsive set of lesson cards, and a footer. The areas name that shell. The cards use auto-fit so a library of three lessons and a library of twelve lessons share one rule, and the columns appear and disappear with the width of main.

The same split covers a documentation landing page, a store category, and an admin list of drafts. The shell is areas. The repeating tiles are minmax and auto-fit.

Frequently Asked Questions

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

Both repeat tracks that are at least the minmax minimum. auto-fill keeps empty tracks, so unused columns still take space. auto-fit collapses those empty tracks, so the cards you have grow into the row. A gallery of real cards wants auto-fit, while a rigid calendar that must show every slot wants auto-fill.

Why does minmax need two values?

The first value is the smallest track size, and the second is the largest. minmax(14rem, 1fr) means at least 14rem, and otherwise an equal share of free space. If the minimum is larger than the container, the track overflows instead of shrinking below that minimum. That is why the minimum has to fit the narrowest pane you support.

Do grid-template-areas replace grid-template-columns?

Areas name the cells, and columns and rows still set the sizes. In this sample the columns are 11rem and minmax(0, 1fr), while the areas say which element sits in which cell. If you omit the columns, the browser creates flexible columns from the area strings, which is rarely the size you drew.

Can I put a card grid inside a named area?

Yes. The element with grid-area: main can itself be a grid, or it can contain a grid. This sample puts .card-grid inside main so the page shell and the cards have separate column lists. Placing each card as a page-level area would force you to redraw the template every time a lesson is added.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…