auto-fit and minmax

Topic 2 of 4 in Advanced grid6 min readBeginnerFree course

Overview

If you learn only one grid recipe, make it this one:

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(15rem, 100%), 1fr));
  gap: 1rem;
}

It creates a responsive grid — four columns on a desktop, two on a tablet, one on a phone — with no media queries. This lesson takes it apart piece by piece.

Piece 1: minmax(15rem, 1fr)

minmax(15rem, 1fr) means each column is at least 15rem, and at most one share of the free space. On its own, inside a fixed repeat(3, …), that would give three columns that never go below 15rem — but on a phone they’d overflow. We need the number of columns to change.

Piece 2: repeat(auto-fit, …)

Instead of a number, repeat() accepts auto-fill or auto-fit: “create as many columns as will fit”. The browser divides the container’s width by the minimum size (accounting for gaps) and creates that many tracks:

Container width (gap 1rem) Columns of at least 15rem (240px)
1100px 4
800px 3
520px 2
400px 1

Then the 1fr maximum makes the columns grow to share any leftover width equally, so the row is always full. As the screen narrows, columns shrink towards 15rem; when they’d go below it, the grid drops to one fewer column and the items reflow.

auto-fill vs auto-fit

The two keywords differ only when there are fewer items than columns — say, three cards in a container wide enough for four:

Container 1000px wide with three items and grid-template-columns: repeat(auto-fill | auto-fit, minmax(200px, 1fr)) and gap: 16px. auto-fill keeps an empty fourth track, so the three items are 238px wide. auto-fit collapses the empty track, so the three items stretch to 322.67px. Both reflow with no media queries.

  • auto-fill creates all the tracks that fit, even empty ones. The three items stay at the size of four columns, leaving space at the end.
  • auto-fit creates the tracks, then collapses the empty ones to zero. The 1fr maximum then lets the three items stretch across the whole row.

The numbers in the diagram were measured in Chrome. Use auto-fit when items should fill the row (a short list of features), and auto-fill when items should keep a consistent size however many there are (a product grid where one search result shouldn’t become a giant card).

Piece 3: min(15rem, 100%)

There’s a final edge case: a container narrower than 15rem — a very small phone, a narrow sidebar, or a zoomed-in page. minmax(15rem, 1fr) would still demand a 15rem column, and it would overflow.

Wrapping the minimum in min() fixes it:

CSS
minmax(min(15rem, 100%), 1fr)

min(15rem, 100%) is 15rem normally, but never more than the container’s full width. On a 200px-wide container, the single column becomes 200px instead of 240px. It’s a small addition that makes the grid truly bulletproof — and matters for users who zoom to 400% (see Layouts that work for everyone).

Why this beats breakpoints

A media-query version needs a rule for each breakpoint, and it responds to the viewport:

CSS
.cards { display: grid; gap: 1rem; }
@media (width >= 40rem) { .cards { grid-template-columns: repeat(2, 1fr); } }
@media (width >= 60rem) { .cards { grid-template-columns: repeat(3, 1fr); } }
@media (width >= 80rem) { .cards { grid-template-columns: repeat(4, 1fr); } }

Put that grid in a 30rem sidebar on a wide screen and it still tries to show four columns. The auto-fit version responds to its own width, so it does the right thing wherever you put it. (Container queries extend this idea to any property — see Container queries.)

Media queries are still the right tool when you need specific column counts at specific sizes, or different layouts that can’t be expressed as “as many as fit”.

Variations

CSS
/* Wider cards, bigger gaps */
grid-template-columns: repeat(auto-fit, minmax(min(22rem, 100%), 1fr));

/* Never more than 3 columns: make the minimum at least a third of the space */
grid-template-columns: repeat(auto-fit, minmax(max(15rem, calc((100% - 2rem) / 3)), 1fr));

/* Fixed-size tiles that don't stretch */
grid-template-columns: repeat(auto-fill, 10rem);
justify-content: center;

The second variation is a neat trick: by making the minimum at least a third of the container (minus the two gaps), there can never be room for a fourth column.

Pair it with a good card: a grid only lines up the boxes. To line up the insides of cards — titles, prices, buttons — combine this recipe with subgrid (later in this chapter).

Exercise

The bulletproof card grid

Make .cards a grid that fits as many columns as possible, each at least 15rem wide, sharing extra space equally — with no media queries. It must never overflow a screen narrower than 15rem (test at 220px wide). When there are only two cards on a wide screen, they should stretch to fill the row. Then make a second version, .cards--fixed, where cards keep their size and leave empty space instead of stretching.

HTML
<div class="cards">
  <article>Flexbox</article><article>Grid</article><article>Responsive</article>
  <article>Containers</article><article>Anchors</article>
</div>

<div class="cards--fixed">
  <article>Flexbox</article><article>Grid</article>
</div>

<style>
  * { box-sizing: border-box; }
  article { background: #eaf0fc; padding: 1.5rem; border-radius: 8px; }
  /* your code here */
</style>

Summary

  • repeat(auto-fit, minmax(15rem, 1fr)) creates as many columns as fit, each at least 15rem, sharing extra space.
  • The grid reflows to fewer columns as the container shrinks — no media queries needed.
  • auto-fill keeps empty tracks when there are few items; auto-fit collapses them so items stretch.
  • Wrap the minimum in min(15rem, 100%) so the grid never overflows containers narrower than 15rem.
  • Because it responds to its container, the same grid works in a sidebar, a modal or a full page.