[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-css-grid-auto-fit-vs-auto-fill":3},{"content":4,"lastModified":5},"\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>The grid was one declaration: \u003Ccode>repeat(auto-fit, minmax(180px, 1fr))\u003C\u002Fcode>. 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.\u003C\u002Fp>\n\n\u003Ch2>188 vs 392, measured\u003C\u002Fh2>\n\n\u003Cp>800px container, 16px gap, \u003Ccode>minmax(180px, 1fr)\u003C\u002Fcode>. Four columns fit, since 4 × 180 + 3 × 16 = 768. I ran both in Chrome and read the computed values back:\u003C\u002Fp>\n\n\u003Cul>\n\u003Cli>auto-fill, 2 cards: tracks \u003Ccode>188px 188px 188px 188px\u003C\u002Fcode>, every card 188px.\u003C\u002Fli>\n\u003Cli>auto-fit, 2 cards: tracks \u003Ccode>392px 392px 0px 0px\u003C\u002Fcode>, every card 392px.\u003C\u002Fli>\n\u003Cli>either keyword, 7 cards: 188px across the board.\u003C\u002Fli>\n\u003C\u002Ful>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F35691103\u002Fpexels-photo-35691103.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1600\" alt=\"Aerial view of a mostly empty parking lot with painted parking bays\" loading=\"lazy\" \u002F>\n\n\u003Ch2>The last row was never what auto-fit fixes\u003C\u002Fh2>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Ch2>Where the two keywords render the same\u003C\u002Fh2>\n\n\u003Cp>Give the track a fixed size instead of a flexible one and there is nothing to stretch into. \u003Ccode>repeat(auto-fit, 200px)\u003C\u002Fcode> and \u003Ccode>repeat(auto-fill, 200px)\u003C\u002Fcode> place cards at the same size in the same spots. The only observable difference is \u003Ccode>justify-content\u003C\u002Fcode>, because a folded track takes its gutters with it.\u003C\u002Fp>\n\n\u003Cp>I put \u003Ccode>justify-content: center\u003C\u002Fcode> 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.\u003C\u002Fp>\n\n\u003Ch2>Two overflow bugs that catch either keyword\u003C\u002Fh2>\n\n\u003Cp>Neither keyword saves you on a phone. First bug:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">\u002F* 320px container, 360px minimum track *\u002F\n.results {\n  display: grid;\n  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));\n}\n\u002F* measured: track 360px, container scrollWidth 360, horizontal scroll appears *\u002F\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>An auto-repeated track can't shrink below its minimum, so the container overflows instead of wrapping. Wrapping the minimum in \u003Ccode>min()\u003C\u002Fcode> fixes it:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">.results {\n  grid-template-columns: repeat(auto-fill, minmax(min(360px, 100%), 1fr));\n}\n\u002F* 320px container: track 320px, no overflow *\u002F\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>The second bug isn't about \u003Ccode>repeat()\u003C\u002Fcode> at all. A \u003Ccode>1fr\u003C\u002Fcode> track has an automatic minimum of min-content, so one long unbreakable string inflates its whole column. I dropped an unbroken identifier into \u003Ccode>repeat(3, 1fr)\u003C\u002Fcode> and measured tracks of 487px, 141px, 141px. With \u003Ccode>minmax(0, 1fr)\u003C\u002Fcode> 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 \u003Ccode>overflow-wrap: anywhere\u003C\u002Fcode> on the one element that needs it.\u003C\u002Fp>\n\n\u003Ch2>What I use now\u003C\u002Fh2>\n\n\u003Cp>One question decides it. Should the cards change size when the data changes?\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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.\u003C\u002Fp>\n\n\u003Cp>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:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-css\">.results {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 16px;\n}\n.results > * {\n  flex: 1 1 180px;\n  max-width: 400px;\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Cards take whatever space is left but stop before they look silly, which is usually what someone wanted when they reached for auto-fit.\u003C\u002Fp>\n\n\u003Cp>DevTools makes the diagnosis fast. Select the grid and read computed \u003Ccode>grid-template-columns\u003C\u002Fcode>: 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.\u003C\u002Fp>\n\n\u003Cp>I keep this rule and the numbers next to it in \u003Ca href=\"\u002Fsnippetark\u002F\">Snippet Ark\u003C\u002Fa>, 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 \u003Ca href=\"\u002Fposts\u002Fcss-container-queries-guide\u002F\">container queries\u003C\u002Fa> and when they beat viewport breakpoints.\u003C\u002Fp>\n","2026-11-17",1794910419987]