← Back to HTML & CSS, page 2

html-css-0035 · Medium mid

What is the difference between auto-fit and auto-fill in CSS Grid, and how does minmax() help?

repeat(auto-fill, minmax(150px, 1fr)) and repeat(auto-fit, ...) both create as many columns as fit. With few items, auto-fill keeps the empty columns, so items stay at the column width, while auto-fit collapses the empty columns and lets items stretch. minmax sets a minimum size and lets columns share the rest.

Full answer

repeat(auto-fill, ...) and repeat(auto-fit, ...) let the browser decide how many columns fit in the container. You give each column a minimum width with minmax(), and the browser creates as many columns as can fit.

The difference only appears when there are fewer items than columns that could fit.

  • auto-fill keeps all the columns it created, even the empty ones. Items stay at their column width and empty space remains on the right.
  • auto-fit creates the same columns, then collapses the empty ones to zero width. The remaining 1fr columns grow, so items stretch to fill the row.

In an 800px container, auto-fill creates five 160px columns (800 divided by 150 is 5.33, so five fit), and the three items each stay 160px wide. With auto-fit, the two empty columns collapse, and the three items stretch to about 267px each. When there are enough items to fill the row, both behave the same.

minmax(150px, 1fr) means "never narrower than 150px, otherwise share the space". On a screen narrower than 150px, a fixed 150px minimum would overflow. Writing minmax(min(100%, 150px), 1fr) caps the minimum at the container width.

No media queries are needed. The layout reflows from one column on a phone to many on a desktop. Rows are created automatically as needed and sized by grid-auto-rows, which is why they are called implicit tracks.

html
<style>
  body { margin: 0; width: 800px; font-family: sans-serif; }
  .grid { display: grid; gap: 0; margin-bottom: 16px; }
  .fill { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .fit  { grid-template-columns: repeat(auto-fit,  minmax(150px, 1fr)); }
  .safe { grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); }
  .grid div { background: #cde; border: 1px solid #fff; padding: 8px; }
</style>
<div class="grid fill" id="fill"><div>1</div><div>2</div><div>3</div></div>
<div class="grid fit"  id="fit"><div>1</div><div>2</div><div>3</div></div>
<div class="grid safe" id="safe"><div>1</div><div>2</div><div>3</div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Both keywords create as many tracks as fit the container.
  • auto-fill keeps empty tracks; auto-fit collapses them.
  • The difference appears only when there are fewer items than columns that fit.
  • minmax(min(100%, 150px), 1fr) avoids overflow on narrow screens.
  • Extra rows are implicit tracks sized by grid-auto-rows.

Common mistakes

  • using auto-fit and auto-fill with fixed values only, such as repeat(auto-fit, 150px). Without 1fr in a minmax, the columns never grow.
  • forgetting that the minimum in minmax must fit on the smallest screen you support.

Follow-ups

  • what is the difference between explicit and implicit grid tracks?
  • What are implicit grid tracks?
  • How do you stop a grid item from overflowing with long text?

Tips

  • Use auto-fit for card grids where you want items to fill the row. Use auto-fill when you want a steady column width even if the last row is short.
  • Use numbers: three items in 800px with a 150px minimum give 160px tracks for fill and about 267px for fit.
  • Mention the min() trick for very small screens.