repeat()
Example
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, 1fr);
}.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}Auto-repeat needs a track with a definite minimum, so minmax(200px, 1fr) makes it valid.
Common mistakes
- Writing
repeat(auto-fit, 1fr), which is invalid. - Expecting
auto-fillto stretch items when there are few of them.
Key points
- Responsive grids use
auto-fillorauto-fitwithminmax()insiderepeat(). auto-fillandauto-fitare not combined with flexible-only tracks likerepeat(auto-fill, 1fr).
Interview questions
auto-fill vs auto-fit in repeat(): what is the difference?
Both fit as many tracks as possible. auto-fit collapses empty tracks so items stretch, while auto-fill keeps them.
Can repeat() hold more than one track?
Yes. repeat(3, 100px 1fr) repeats the two-track pattern three times.