liveBook · Manning

Auto-fit

As explained in Math for Web Design

auto-fit is a CSS Grid repeat() keyword that creates fitting tracks and collapses empty tracks so available space can be redistributed.

auto-fit is a repeat() keyword for responsive CSS Grid layouts. It calculates how many tracks fit in the container, then collapses unused tracks to 0px so occupied tracks can expand into the released space. section 4.4.4 section 4.6

The browser first estimates repetitions with floor((container width − total gaps) / track width). With flexible tracks such as minmax(200px, 1fr), the remaining space is distributed among the tracks that contain items. section 4.4.4

Two cards in an 850px grid

Use grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));. The layout can calculate four 200px-capable columns, but only two contain cards. auto-fit collapses the two empty columns, allowing the two cards to expand to 425px each. Replacing auto-fit with auto-fill preserves the empty columns and keeps the card widths at 212.5px.

Choose the keyword according to how empty space should behave:

  • Use auto-fit when occupied tracks should grow after unused tracks collapse. section 4.4.4
  • Use auto-fill when calculated tracks should remain even without content. section 4.6

How Math for Web Design covers it

How deep it goes
Explains repetition math, gap subtraction, empty-track collapse, and flexible redistribution through runnable CSS examples.
Examples
Demonstrates two cards in an 850px container, contrasting 212.5px auto-fill tracks with 425px auto-fit tracks.
What it assumes
Assumes basic CSS Grid syntax while explaining repetition counts, gaps, flexible tracks, and empty-track behavior.
What you can do after
Build responsive card grids with repeat(auto-fit, minmax(...)) and choose whether empty tracks collapse or remain.
How it connects
Connects auto-fit with CSS Grid, repeat(), auto-fill, minmax(), fractional units, and responsive design.

Also explained in

Compare how 2 books explain auto-fit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.