Skip to content

auto-fit vs auto-fill in CSS Grid

The pattern both keywords enable

The canonical responsive grid is one line: `grid-template-columns: repeat(auto-fit, minmax(200px, 1fr))`. It creates as many columns as fit at a minimum of 200px each, lets them share the leftover space, and changes the column count as the container resizes — with no media queries at all.

Swap auto-fit for auto-fill and, in a container with enough items to fill every column, absolutely nothing changes. That is precisely why the distinction is so hard to internalise: in the common case the two are indistinguishable.

Where they diverge

The difference appears only when there are fewer items than would fit. Picture a container wide enough for five 200px columns, holding three cards.

auto-fill creates all five tracks and keeps the two empty ones. The three cards stay at 200px and the right-hand side of the row is empty space.

auto-fit creates the same five tracks, then collapses the empty ones to zero width. The remaining three tracks absorb that space through their 1fr maximum, so the cards stretch to fill the whole row.

Note that the collapse is what does the work, not the fr unit on its own — with minmax(200px, 200px) the cards would not stretch under either keyword, because there is no flexible maximum for the reclaimed space to flow into.

Which one to use

Use auto-fit when items should expand to use the available space — a card grid, a gallery, a set of feature tiles. A row with two items looks deliberate rather than truncated.

Use auto-fill when a consistent item size matters more than filling the row: a calendar, a grid of fixed-size thumbnails, or any layout that items get added to over time and should not visibly resize as the count changes.

If you find yourself unsure, auto-fit is the more common choice in interface work, because a half-empty row usually reads as a bug even when it was intentional.

Frequently asked questions

Why do my grid items overflow instead of shrinking?
A track sized 1fr will not shrink below the intrinsic minimum width of its content, so a long unbroken string or a wide image pushes it past its share. Writing minmax(0, 1fr) removes that floor and is the standard fix.
Does minmax(200px, 1fr) mean columns are never narrower than 200px?
Not quite. If the container itself is narrower than 200px, a single column still has to fit it and will be narrower. To guarantee the minimum never applies below the container width, use minmax(min(200px, 100%), 1fr).
Should I use Grid or Flexbox for a card layout?
Grid, if you want the cards aligned in both directions with equal track widths. Flexbox with wrap is better when the last row should size its items to their content rather than to the columns established above them.

Try the CSS Grid Generator

Set columns, rows, gaps and alignment, preview the layout live, and copy the grid CSS with the matching HTML.

Open tool

More guides