Sub topic 12
Responsive grids with auto-fill and auto-fit
This chapter assumes you can define columns with grid-template-columns, use fr units, and write repeat() and minmax(). You should also know how gap works.
The goal: columns that adapt on their own
A fixed repeat(4, 1fr) always gives four columns. On a narrow screen each column becomes too small to read. Instead of writing media queries, you can let the browser decide how many columns fit.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.5rem;
}Each track is at least 240px wide. The browser creates as many tracks as fit in the container. Then 1fr stretches them to use the leftover space.
How the browser counts columns
With auto-fill or auto-fit, the repeat count is computed from the container's width. The browser uses the minimum size from minmax() plus the gaps. For example, a 1000px container with 240px minimums and 24px gaps fits three columns (3 × 240 + 2 × 24 = 768). Four would need 4 × 240 + 3 × 24 = 1032px, which is too wide.
auto-fill versus auto-fit
The two keywords differ only when there are fewer items than the row could hold.
auto-fillkeeps the empty tracks. They take up space, so items stay at their minimum-to-1frshare and leave blank room at the end of the row.auto-fitcollapses empty tracks to zero width. The remaining tracks then grow with1frand fill the whole row.
.fill { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.fit { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }With two items in an 800px container, .fill shows two 150px-plus columns next to empty tracks, while .fit stretches the two items across the full width. When the row is full of items, both behave the same.
| Keyword | Empty tracks | Few items look like |
|---|---|---|
| `auto-fill` | Kept | Items stay narrow, blank space on the right |
| `auto-fit` | Collapsed | Items stretch to fill the row |
Common errors
- Using a fixed size instead of
minmax().repeat(auto-fit, 1fr)is invalid, because the repeat count cannot be computed from a flexible track. Give the track a definite minimum, such asminmax(240px, 1fr). - Minimum wider than the container. On a 320px screen,
minmax(400px, 1fr)overflows horizontally. Useminmax(min(100%, 400px), 1fr)so the minimum never exceeds the container. - Expecting a single item to stay small with
auto-fit. A lone item stretches across the row. Useauto-fillor set amax-widthon the item if you want that item to stay narrow. - Mixing in
auto-fitwith other flexible tracks. Collapsing only affects empty repeated tracks, so a sidebar defined outside therepeat()is unaffected.
A safe, complete pattern
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
gap: 1.5rem;
}Exercise
A container is 700px wide, has gap: 20px, and uses repeat(auto-fill, minmax(200px, 1fr)). It holds only two items. How many columns are created, and how does the result change with auto-fit?
Show answer
Three columns fit, because 3 × 200 + 2 × 20 = 640px, while four need 860px. With auto-fill, the two items take the first two columns and the third stays as an empty track, so the items are about 220px wide with blank space on the right. With auto-fit, the empty third track collapses, so the two items stretch to fill the whole 700px row.
Exercise
Why is repeat(auto-fit, 1fr) invalid, and how do you fix it?
Show answer
The browser needs a definite size to compute how many tracks fit, and 1fr has no minimum it can use. Replace it with minmax(<minimum>, 1fr), for example repeat(auto-fit, minmax(200px, 1fr)).