Create flexible tracks with fr, repeat, minmax, and auto sizing.
A flexible grid adjusts its column widths and column count to the available space. Use fr to share space, minmax() to set limits, and repeat() to create columns.
fr.ratio {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
gap: 1rem;
}At a 616-pixel content width with a 16-pixel gap, 600 pixels remain for the tracks. They receive 200 and 400 pixels: one share and two shares.
minmax(0, 1fr) sets a zero track minimum instead of the automatic minimum of a bare 1fr. This allows the track to shrink, but the content inside it still needs to wrap or handle overflow.
.equal {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}This always defines three columns. They become narrower together as the container narrows; repetition with a numeric count does not change that count.
minmax() takes a minimum followed by a maximum. A flexible fr value is valid in the maximum position, but not the minimum position.
.catalog {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
gap: 1rem;
}
.catalog > article {
min-width: 0;
overflow-wrap: anywhere;
}The browser creates as many 14rem minimum columns as fit, accounting for gaps, then distributes extra width through the 1fr maximum. Empty repeated tracks collapse with auto-fit.
With a 16-pixel root font size and enough items:
| Container width | Columns that fit |
|---|---|
| 1000px | Four: the minimum widths plus three gaps take 944px. |
| 700px | Two: three columns and their gaps would take 704px. |
| 300px | One. |
The count depends on container space, not the number of items on just the final row.
At 616px with a 16px gap, the earlier 1fr 2fr example has 600px to share. What happens if the second track changes to 1fr while both minimums remain zero?
Each track gets one of two equal shares: 300px. The gap is subtracted before the shares are divided.
Now consider repeat(3, minmax(0, 1fr)) at a narrower width. Does it change the number of columns?
No. The numeric repeat count stays at three; the tracks shrink. Automatic repetition is needed for the count to change.
auto-fit and auto-fill.reserved {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr));
gap: 1rem;
}Both initially create tracks that fit. The difference appears when whole repeated tracks are empty:
auto-fill: Keeps those empty tracks, reserving space for them.auto-fit: Collapses them so occupied flexible tracks can expand.auto-fit collapses a column only if the whole column is empty. An empty cell in the last row is not enough.
A strict 14rem minimum overflows a container narrower than that value. To allow one track to become as narrow as its container, replace the catalog's template with:
.catalog {
grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
}The min() function lowers the minimum when the container is narrower than 14rem. Keep the content wrapping rules as well.
fr: Shares leftover track space after gaps.minmax(): Sets track limits; fr belongs in the maximum position.auto-fit collapses them; auto-fill retains them.Try it yourself
HTML + CSS