Sub topic 8
auto-fit versus auto-fill
Prerequisites
This chapter assumes you can write repeat() and minmax() and know what fr units and gap do. It also assumes you have seen repeat(auto-fit, minmax(200px, 1fr)) in the previous chapter.
The difference in one sentence
auto-fit and auto-fill both let the browser decide how many tracks fit in the container. They differ only in what happens to tracks that end up with no items in them. auto-fill keeps those empty tracks. auto-fit collapses them to zero width.
How the browser decides
- It takes the container width and works out how many tracks of the minimum size, plus gaps, fit on one row.
- It creates that many tracks. Both keywords give the same count.
- It places the items into the tracks in order.
- It handles any track that has no items.
auto-fillleaves it at its size.auto-fitcollapses it to zero, along with the gap beside it. - It shares the leftover space among the tracks that remain. This only matters when the maximum is flexible, as with
1fr.
If the items fill every track, there are no empty tracks and the two keywords look identical.
Side by side
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>auto-fit versus auto-fill</title>
<style>
.fill { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 1rem; }
.fit { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; }
.fill div, .fit div { background: #cde; padding: 1rem; }
</style>
</head>
<body>
<h2>auto-fill</h2>
<div class="fill"><div>One</div><div>Two</div></div>
<h2>auto-fit</h2>
<div class="fit"><div>One</div><div>Two</div></div>
</body>
</html>Assume the container is 1000px wide and 1rem is 16px. Four 200px tracks and three gaps need 848px, and a fifth track would need 1064px. So both grids create four tracks.
| Keyword | Tracks after step 4 | Width of each of the two items |
|---|---|---|
| `auto-fill` | 4 (two empty) | about 238px |
| `auto-fit` | 2 (two collapsed) | about 492px |
With auto-fill, the two items stay near the minimum size and empty space sits on the right. With auto-fit, the two items stretch across the whole row.
Choosing between them
- Use
auto-fitwhen items should always fill the row, such as a short list of feature blurbs. A single item will take the full width. - Use
auto-fillwhen you want a consistent column width no matter how many items there are, such as a gallery where a lone card should not become huge.
Common errors
- Seeing no difference. You will not see one when the row is full of items. Test with fewer items than the container has tracks.
- Expecting a difference with fixed tracks. In
repeat(auto-fit, 200px)the tracks never stretch, so collapsing changes nothing visible. The only effect is on alignment, for example withjustify-content: center. Withauto-fill, the empty tracks still take space and push the items off-center. Withauto-fit, the items are centered. - Blaming the keyword for overflow. Overflow in narrow containers comes from the
200pxminimum, not from either keyword. The next chapter covers the fix. - Thinking collapsed tracks are removed. They still exist as tracks with zero width. A later item can still be placed into one if you position it there explicitly.
Exercise
A grid container is 700px wide with gap: 1rem (16px) and grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)). It holds one item. How wide is that item, and how wide would it be with auto-fill?
Show answer
Three 200px tracks and two gaps need 632px, and a fourth track would need 848px, so three tracks are created. With auto-fit, the two empty tracks collapse and the single item takes the whole 700px. With auto-fill, all three tracks stay, so the item is one track wide: (700 - 32) / 3 = about 223px.