CSS Grid auto-fit vs auto-fill: Why Two Cards Ate the Whole Row
Search page I shipped last year. A CSS grid, six results, four columns, all lined up. Then a half-empty query came back with two hits and both cards rendered 392px wide, thumbnails the size of a laptop screen. Three hits gave me 256px cards. Six hits gave me 188px cards. Identical CSS the whole time.
The grid was one declaration: repeat(auto-fit, minmax(180px, 1fr)). I lifted it off a post that called auto-fit "almost always the correct choice" over auto-fill for card grids. It isn't, and the reason isn't the one those posts explain.
188 vs 392, measured
800px container, 16px gap, minmax(180px, 1fr). Four columns fit, since 4 × 180 + 3 × 16 = 768. I ran both in Chrome and read the computed values back:
- auto-fill, 2 cards: tracks
188px 188px 188px 188px, every card 188px. - auto-fit, 2 cards: tracks
392px 392px 0px 0px, every card 392px. - either keyword, 7 cards: 188px across the board.
Read that second line again. The collapsed tracks didn't disappear. They sit there at 0px. So the mechanism is: auto-fit counts the same number of columns auto-fill would, then folds away any track that no item landed in and hands the freed space to the survivors.
Which means card width becomes a function of result count. A two-result page and a six-result page don't look like the same product. If the card holds a fixed-aspect image, every query renders the page at a different scale, and users read that as broken even though the browser did exactly what you asked.
The last row was never what auto-fit fixes
Here is the part I got wrong for about a year. I assumed auto-fit would stretch the leftovers on a ragged final row. It doesn't. Seven cards in a four-column grid give the same 188px cards under both keywords, and row two just sits there with three cards and a hole on the right.
MDN is exact about why: an empty track is "one with no in-flow grid items placed into or spanning across it." Sizing is done for the grid as a whole, and a track holding a card in row one counts as occupied forever. auto-fit only folds columns that no item touches anywhere, so in a multi-row grid nothing folds at all.
Put another way: auto-fit only changes the layout when there are fewer items than columns. That is also why the tutorials make it look like it stretches a ragged last row. Their demos have three cards in a four-column grid, the one case where it does.
Where the two keywords render the same
Give the track a fixed size instead of a flexible one and there is nothing to stretch into. repeat(auto-fit, 200px) and repeat(auto-fill, 200px) place cards at the same size in the same spots. The only observable difference is justify-content, because a folded track takes its gutters with it.
I put justify-content: center on both, two cards in an 800px row. The first card under auto-fill started at 16px, because it was centering four reserved 180px slots. Under auto-fit it started at 212px, because it was centering two real cards and their single gap. That second one is a good trick for a logo strip that should look centered however many items it holds. Fixed track, auto-fit, centered, no media query, no child counting.
Two overflow bugs that catch either keyword
Neither keyword saves you on a phone. First bug:
/* 320px container, 360px minimum track */
.results {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
}
/* measured: track 360px, container scrollWidth 360, horizontal scroll appears */
An auto-repeated track can't shrink below its minimum, so the container overflows instead of wrapping. Wrapping the minimum in min() fixes it:
.results {
grid-template-columns: repeat(auto-fill, minmax(min(360px, 100%), 1fr));
}
/* 320px container: track 320px, no overflow */
The second bug isn't about repeat() at all. A 1fr track has an automatic minimum of min-content, so one long unbreakable string inflates its whole column. I dropped an unbroken identifier into repeat(3, 1fr) and measured tracks of 487px, 141px, 141px. With minmax(0, 1fr) I got 256px each. The text still spilled out of its card (scrollWidth stayed 487), but the grid stopped collapsing. That's the order you want: constrain the track first, then handle the string with overflow-wrap: anywhere on the one element that needs it.
What I use now
One question decides it. Should the cards change size when the data changes?
If no (listing pages, search results, photo grids, dashboards), use auto-fill. Cards stay the size the design specified, the unused slots sit at the end, and the page doesn't reshuffle between queries.
If yes (two feature boxes sharing a row, a pricing table with two plans and later four), use auto-fit. That is the case it was built for.
And if you genuinely want a ragged last row to fill the width, neither keyword will do it. Flexbox with a capped grow is the honest answer:
.results {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.results > * {
flex: 1 1 180px;
max-width: 400px;
}
Cards take whatever space is left but stop before they look silly, which is usually what someone wanted when they reached for auto-fit.
DevTools makes the diagnosis fast. Select the grid and read computed grid-template-columns: four values while you have two cards means auto-fit is stretching, four real values with two cards inside means auto-fill is holding the line.
I keep this rule and the numbers next to it in Snippet Ark, mostly because I don't trust myself to remember which keyword does what eight months from now. If you want the related side of intrinsic sizing, I wrote about container queries and when they beat viewport breakpoints.