repeat()
Repeats a pattern of grid columns or rows. With auto-fill or auto-fit and minmax() it creates a responsive grid without media queries.
Example
#.courses {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(140px, 100%), 1fr));
gap: 10px;
}<style>
.course { padding: 12px; border: 1px solid #27272a; border-radius: 12px; background: #18181b; }
.course span { display: block; color: #a1a1aa; font-size: 13px; }
</style>
<div class="courses">
<div class="course"><strong>HTML and CSS</strong><span>Egypt</span></div>
<div class="course"><strong>React</strong><span>Space</span></div>
<div class="course"><strong>JavaScript</strong><span>Jurassic Park</span></div>
<div class="course"><strong>Next.js</strong><span>The Future</span></div>
<div class="course"><strong>Python</strong><span>Safari</span></div>
</div>The grid decides the number of columns itself: more fit on a computer, fewer on a phone. There is not a single media query here.
Definition and usage
#The repeat() function shortens the notation of tracks in a CSS Grid. Instead of 1fr 1fr 1fr 1fr you write repeat(4, 1fr), and instead of 100px 1fr 100px 1fr simply repeat(2, 100px 1fr). It works in grid-template-columns and grid-template-rows, and can be combined with regular values, for example 200px repeat(3, 1fr).
Instead of a number you can write auto-fill or auto-fit. The browser then calculates by itself how many columns fit in the container. Paired with minmax() this gives a responsive grid without media queries: repeat(auto-fill, minmax(160px, 1fr)) creates as many columns at least 160px wide as will fit, and divides the excess space equally between them.
The minmax(MIN, MAX) function sets the size range of a single track: the column won’t be narrower than MIN or wider than MAX. 1fr, an equal share of the free space, is often used as the maximum. On a very narrow screen the minimum can be wider than the whole container, so it is safer to write minmax(min(160px, 100%), 1fr).
The difference between auto-fill and auto-fit shows when there are few items. auto-fill creates spare columns, even empty ones, so the items keep their size. auto-fit collapses empty columns to zero and lets the existing items stretch across the full width. With many items both versions look the same.
Syntax
#repeat(<number> | auto-fill | auto-fit, <track size>+)
minmax(<minimum>, <maximum>)Common patterns
#three equal columns
Three columns, each an equal share of the width. Six tiles arrange themselves into two rows.a repeated pattern
You can repeat a whole pattern. Here a pair twice: a narrow 60 px column and a wide1frone, four columns in total.columns from 50 to 90 px
Each column is between 50 and 90 px wide. In a wide frame they stop at 90 px, and when you narrow it (drag the bottom-right corner) they shrink, but not below 50 px.repeat(auto-fill, minmax(70px, 1fr))
spare columns
The browser creates as many columns as fit, even empty ones. Two tiles take only part of the width and the rest stays free. Narrow the frame to see the columns disappear one by one.repeat(auto-fit, minmax(70px, 1fr))
empty columns disappear
Empty columns collapse to zero, so the same two tiles stretch across the whole width. Compare withauto-fillnext to it.
Common patterns in practice
#repeat(3, 1fr)
.demo {
grid-template-columns: repeat(3, 1fr);
}repeat(2, 60px 1fr)
1fr one, four columns in total..demo {
grid-template-columns: repeat(2, 60px 1fr);
}repeat(3, minmax(50px, 90px))
.demo {
grid-template-columns:
repeat(3, minmax(50px, 90px));
}repeat(auto-fill, minmax(70px, 1fr))
.demo {
grid-template-columns:
repeat(auto-fill, minmax(70px, 1fr));
}repeat(auto-fit, minmax(70px, 1fr))
auto-fill next to it..demo {
grid-template-columns:
repeat(auto-fit, minmax(70px, 1fr));
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- With
auto-fillandauto-fitthe minimum inminmax()must be fixed, for example160px, notautoor1fr. Otherwise the browser can’t calculate how many columns fit. - When there are few items and they should stretch across the full width, choose
auto-fit. When they should keep their size, chooseauto-fill. - The expression
minmax(min(160px, 100%), 1fr)protects against horizontal scrolling on screens narrower than 160 px.
Related entries
#- grid-template-columnsDefines the grid’s columns: their number and widths.
- grid-template-rowsDefines the grid’s rows: their number and heights.
- min()Picks the smallest of the given values. It works as an upper limit, for example a width of 100% of the parent, but no more than 600px.
- gapSets the spacing between items in a flex, grid, or multi-column container.
- grid-auto-flowDecides how items without an explicit position fill the grid.
- UnitsAn overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.