π What is repeat()?
repeat() is a CSS Grid function that lets you create multiple grid tracks (rows or columns) using a compact, clean, and scalable syntax. Instead of writing the same value over and over, you can generate tracks automatically.
π― Syntax
repeat() Syntax
repeat(<count>, <track-size>)count β How many tracks to generatetrack-size β Width or height of each track
π Where can repeat() be used?
- grid-template-columns
- grid-template-rows
- repeat(auto-fit, ...)
- repeat(auto-fill, ...)
π§ͺ Basic Example
3 Equal Columns
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}This generates 3 equal-width columns β 1fr 1fr 1fr.
π Without repeat()
Manual Equivalent
grid-template-columns: 1fr 1fr 1fr;repeat() makes your CSS cleaner and easier to maintain.
π₯ Repeat With Fixed Values
Fixed Column Repetition
grid-template-columns: repeat(4, 200px);Produces four 200px-wide columns.
π§ Special Feature: auto-fit & auto-fill
repeat() becomes extremely powerful when used withauto-fit and auto-fill.
1οΈβ£ auto-fit β collapses empty columns
auto-fit + minmax
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));Fills available space and removes unused columns β responsive & fluid.
2οΈβ£ auto-fill β keeps empty columns
auto-fill + minmax
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));Behaves similarly but preserves implicit empty columns.
Note
Use auto-fit when you want content to stretch.
Use auto-fill when you want consistent grid structure.
π Practical Examples
Responsive Card Grid
Card Grid Example
.cards {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}The grid automatically adapts to screen size, with each card at least 300px wide.
Uniform Row Heights
Repeat for Rows
grid-template-rows: repeat(5, 100px);Creates 5 rows, each 100px tall.
π§© Combining repeat() With Other Functions
repeat() + minmax()
grid-template-columns: repeat(3, minmax(150px, 1fr));Each column has safe minimum size but can grow flexibly.
repeat() + auto unit
grid-template-columns: repeat(2, auto) 1fr;Useful for sidebar + content layouts.
π± Advanced Pattern: Holy Grail Layout
Holy Grail Using repeat()
.layout {
display: grid;
grid-template-columns: 200px repeat(2, 1fr) 200px;
}repeat(2, 1fr) generates two equal flexible center columns.
β οΈ Common Mistakes
- Using repeat() without considering minimum widths
- Confusing auto-fit vs auto-fill
- Using large fixed px values inside repeat() on small screens
- Expecting repeat() to auto-create rows or columns without template definitions
Note
π₯ Summary
repeat() is one of the most important CSS Grid tools for writing clean, scalable, and responsive grid definitions. From simple equal columns to complex responsive galleries, repeat() makes layouts easier.
Learn more βMDN Docs π