Sub topic 8
auto-fit versus auto-fill
This chapter assumes you can write grid-template-columns with fr units and gap. It also assumes you have seen repeat() and minmax() and know that repeat(auto-fit, minmax(200px, 1fr)) produces a responsive column count. Here you learn the one place where auto-fit and auto-fill differ, why that difference only shows up in some situations, and how to choose between them.
The shared behavior
Both keywords tell the browser to work out the number of columns for you. The browser:
- Takes the grid container's width.
- Finds the largest number of tracks, plus the gaps between them, that fits in that width.
- Creates that many tracks.
Both keywords use the same counting algorithm, so for the same container and the same track size they produce the same number of columns. If you have more items than columns, the items wrap onto new rows, and the two keywords look identical.
The difference starts after the tracks exist. Some tracks may have no item in them, because there are fewer items than columns. Then:
auto-fillkeeps those empty tracks at their full size.auto-fitcollapses those empty tracks to zero width. The gaps on either side of a collapsed track collapse too.
Collapsing matters when the track's maximum is a flexible 1fr. The space the empty tracks occupied is handed to the remaining tracks, so the items stretch.
A side-by-side example
This page is complete and runnable. It shows two grids with the same two items, one per keyword.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>auto-fit vs auto-fill</title>
<style>
body { font-family: sans-serif; }
.demo {
width: 1000px;
margin-bottom: 2rem;
outline: 1px dashed gray;
display: grid;
gap: 16px;
}
.fill { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.fit { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.demo > div { background: #cde; padding: 1rem; }
</style>
</head>
<body>
<h2>auto-fill</h2>
<div class="demo fill"><div>One</div><div>Two</div></div>
<h2>auto-fit</h2>
<div class="demo fit"><div>One</div><div>Two</div></div>
</body>
</html>The dashed outline shows the container. Open the page and turn on your browser's grid overlay in the developer tools to see the tracks.
Here is the arithmetic for a 1000px container with a 16px gap:
- Five columns would need 5 × 200 + 4 × 16 = 1064px, which does not fit.
- Four columns need 4 × 200 + 3 × 16 = 848px, which fits. Both keywords create 4 tracks.
- With
auto-fill, the 4 tracks share the space equally. Each is (1000 − 48) / 4 = 238px wide. The two items sit in the first two tracks. The last two tracks stay empty and keep their width, so the items end about 492px from the left edge, with blank space to their right. - With
auto-fit, the two empty tracks collapse, along with their gaps. Two tracks and one gap remain, so each item is (1000 − 16) / 2 = 492px wide, and the row is filled.
| Situation (1000px, 2 items) | `auto-fill` | `auto-fit` |
|---|---|---|
| Tracks created | 4 | 4, then 2 collapse |
| Width of each item | 238px | 492px |
| Row filled | No, blank space on the right | Yes |
When the keywords look identical
Many layouts never show a difference. Be aware of these cases so you do not chase a bug that is not there.
Enough items to fill the row
If there are at least as many items as columns, no track is empty and nothing collapses. Both keywords give the same result.
Items that wrap onto later rows
A track collapses only if no item sits in it in any row. Suppose a 4-column grid holds 5 items. Row 1 fills all four columns and row 2 has one item in column 1. Columns 2 to 4 each contain an item in row 1, so none of them collapse. The fifth item is as wide as the others and sits alone on the second row.
This is why you cannot use auto-fit to make a lone last-row item stretch across the row. It stays one column wide.
Tracks with a fixed maximum
Look at minmax(200px, 300px). The maximum is a fixed length, not 1fr, so the tracks cannot grow past 300px. Collapsing empty tracks does not make the items any wider.
The counting rule changes in this case. For repeat() with auto-fit or auto-fill, the browser counts using the track's maximum when that is a definite length. It uses the minimum only when the maximum is flexible, as with 1fr. In a 1000px container with a 16px gap:
minmax(200px, 1fr)counts with 200px, which gives 4 columns.minmax(200px, 300px)counts with 300px, which gives 3 columns. Four would need 4 × 300 + 48 = 1248px.
Those 3 tracks grow to 300px each and leave 68px unused.
Where a visible difference remains with fixed tracks
When tracks have a fixed size, collapsing still changes where the filled tracks sit. Consider alignment of the whole set of tracks with justify-content:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, 200px);
gap: 16px;
justify-content: center;
}With auto-fit, the empty tracks collapse, so only the filled tracks are centered. If you change auto-fit to auto-fill, the empty tracks still take up space, so the whole set of tracks is centered. Items then appear shifted to the left of center whenever there are fewer items than columns.
This is the clearest case where the choice has a visible effect even though the tracks cannot stretch. For a centered gallery of fixed-size thumbnails, use auto-fit.
How to choose
Ask what you want a half-empty row to look like.
- Use
auto-fitwhen items should stretch to fill the row. This suits cards, product tiles, and dashboard panels, where one card should not sit in a small corner of a wide container. It is also the right choice when you center fixed-size tracks. - Use
auto-fillwhen the column size should stay consistent regardless of item count. This suits a list where the number of items changes over time, such as search results or filtered tags. Withauto-fit, a filter that leaves one result makes a single card jump to full width. Withauto-fill, the card keeps the same width it had before filtering, so the page does not shift as the list changes. - Use
auto-fillwhen this grid must line up with another grid. If two stacked containers should share the same column lines, empty tracks keep the lines in place.
Common errors
Using `fr` as the only track size
/* Invalid: the repeated track size must be definite */
.bad { grid-template-columns: repeat(auto-fit, 1fr); }auto-fit and auto-fill need to know how much space one repeated track needs, so they can count how many fit. A bare 1fr has no fixed size to count with, and the declaration is dropped. Put a fixed length in the minimum, as in minmax(200px, 1fr).
Expecting the keywords to react to item size
The count depends only on the container width and the track sizes you wrote. A wide card does not reduce the column count. It overflows its track or is stretched by it, depending on content.
Mixing with other repeated tracks
A grid-template-columns value can contain only one auto-fit or auto-fill repeat. You can put fixed tracks before and after it, for example 100px repeat(auto-fit, minmax(200px, 1fr)) 100px. The fixed tracks are subtracted from the available width before the count is made. Those tracks are never collapsed, because only the tracks produced by the auto repeat can collapse.
Expecting a container with no width to produce columns
The count needs a width to work with. In a container with an indefinite width, such as a shrink-to-fit inline-grid, the browser cannot pick a column count by fitting tracks to the container. Give the container a definite width, or let it be a block-level grid.
Narrow screens
With minmax(200px, 1fr), a container narrower than 200px overflows. The next chapter shows how min(100%, 200px) inside minmax() fixes this. Both keywords share the same problem.
Summary of the model
- Both keywords create the same number of tracks.
auto-fillkeeps tracks without items.auto-fitcollapses them.- The difference is visible only when some track has no item at all. With flexible tracks, the visible effect is stretching. With fixed tracks, it is the alignment of the remaining tracks.
- When in doubt for a card grid, start with
auto-fitand switch toauto-fillif jumpy item widths bother you.
Exercise
A grid container is 900px wide with gap: 20px and grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)). It holds 2 items. How many tracks are created, how many collapse, and how wide is each item? What would the item width be with auto-fill?
Show answer
Three columns need 3 × 250 + 2 × 20 = 790px, which fits. Four need 4 × 250 + 3 × 20 = 1060px, which does not. So 3 tracks are created. With auto-fit, 1 empty track collapses along with its gap, leaving 2 tracks and 1 gap. Each item is (900 − 20) / 2 = 440px wide. With auto-fill, the 3 tracks all stay, and each is (900 − 40) / 3 ≈ 286.7px wide, so each item is about 287px.
Exercise
You have a grid with grid-template-columns: repeat(auto-fill, 180px) and justify-content: center. It holds 2 items in a container that fits 5 tracks. The items look shifted left of center. Why, and what one-word change fixes it?
Show answer
With auto-fill, the 3 empty tracks keep their 180px width, so the browser centers all 5 tracks, and the 2 items occupy the left part of that group. Changing auto-fill to auto-fit collapses the empty tracks, so only the 2 filled tracks are centered.
Exercise
A grid uses repeat(auto-fit, minmax(200px, 1fr)) and holds 5 items. It shows 4 columns. Does auto-fit stretch the fifth item across the second row? Explain.
Show answer
No. Columns 2 to 4 each contain an item in the first row, so none of those tracks is empty and none collapse. The fifth item stays one column wide in the first column of row 2. Collapsing applies only to tracks that hold no item in any row.