Sub topic 4
Repeating tracks with repeat()
This chapter assumes you can write a basic grid container with display: grid. It also assumes you know grid-template-columns, grid-template-rows, the fr unit, and gap. The chapter covers the repeat() function: its syntax, how it expands, how it combines with other track sizes, and the errors people hit most often.
What repeat() does
repeat() is a CSS function that writes a track list for you. These two declarations produce the same grid:
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-columns: repeat(4, 1fr);The function takes two arguments separated by a comma:
- A repeat count. This is either a positive integer or one of the keywords
auto-fillandauto-fit. - A track list. This is one or more track sizes, optionally with line names.
The browser expands the function before it lays out the grid. A grid with repeat(4, 1fr) has four ordinary tracks, with no special repeating behavior left over.
Repeating a pattern
The second argument can hold several track sizes, and the whole group repeats. This is useful for alternating widths.
.pattern {
display: grid;
grid-template-columns: repeat(2, 100px 1fr);
}This expands to 100px 1fr 100px 1fr. The result is four columns, where columns 1 and 3 are fixed and columns 2 and 4 share the remaining space.
You can also mix repeat() with ordinary track sizes in the same declaration:
.page {
display: grid;
grid-template-columns: 240px repeat(3, 1fr) 80px;
}This gives five columns: a 240px column, three equal flexible columns, and an 80px column. Because the repeated tracks are real tracks, they count when you place items by line number. Here the grid has six column lines, numbered 1 to 6.
A complete example
The page below runs as shown. Save it as an .html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>repeat() demo</title>
<style>
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(2, 80px);
gap: 1rem;
}
.grid div {
background: #cfe3ff;
border: 1px solid #5b8bd9;
padding: 0.5rem;
}
</style>
</head>
<body>
<div class="grid">
<div>1</div><div>2</div><div>3</div><div>4</div>
<div>5</div><div>6</div><div>7</div><div>8</div>
</div>
</body>
</html>repeat() works for rows as well as columns. Here it makes two explicit rows of 80px each. Eight items fill the eight cells.
Repeating with named lines
You can put line names inside the repeated track list. The names repeat with the tracks.
.named {
display: grid;
grid-template-columns: repeat(2, [start] 1fr [end]);
}This expands to [start] 1fr [end] [start] 1fr [end]. A line name can be used more than once in a grid. When it is, you refer to a particular occurrence by number, such as start 2. Naming lines is covered in its own chapter, so treat this as a note on how the pieces interact.
The keyword counts: auto-fill and auto-fit
Instead of an integer, the first argument can be auto-fill or auto-fit. The browser then works out how many repetitions fit in the container.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, 200px);
}If the container is 700px wide and there is no gap, the browser creates three 200px columns, because a fourth would need 800px. The count changes when the container resizes. The difference between the two keywords, and their use with minmax(), is covered in the responsive grids chapter. For now, remember that they are the only way repeat() can produce a count you did not write yourself.
Rules and restrictions
repeat() has a few firm limits. Breaking them makes the whole declaration invalid, and the browser ignores it.
| Rule | Invalid example | Fix |
|---|---|---|
| The count must be a positive integer or a keyword. | `repeat(0, 1fr)` | Use `repeat(1, 1fr)` or leave `repeat()` out. |
| `repeat()` cannot be nested. | `repeat(2, repeat(2, 1fr))` | Write `repeat(4, 1fr)`. |
| `auto-fill` and `auto-fit` need a definite track size. | `repeat(auto-fill, auto)` | Use a length or `minmax(200px, 1fr)`. |
| Only one auto-repeat is allowed per track list. | `repeat(auto-fill, 100px) repeat(auto-fill, 50px)` | Keep one, and use fixed tracks for the rest. |
| The count cannot be a variable that is not an integer. | `repeat(2.5, 1fr)` | Use a whole number. |
A var() that resolves to an integer is fine, as in repeat(var(--cols), 1fr). This is a common way to change the column count from a media query or a component.
.grid {
--cols: 2;
display: grid;
grid-template-columns: repeat(var(--cols), 1fr);
gap: 1rem;
}
@media (min-width: 700px) {
.grid { --cols: 4; }
}Common errors
Equal columns that are not equal
repeat(3, 1fr) can produce unequal columns. An fr track has an automatic minimum size, which is the minimum content size of its items. A long unbreakable word, a wide image, or a pre block can force one column wider than the others. To make the columns truly equal, set the minimum to zero:
.equal {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
}The minmax() function is the subject of a later chapter. Here it only removes the content-based minimum.
Forgetting that repeat() is not a loop over items
repeat() defines tracks, not items. If you write repeat(3, 1fr) and place ten children in the grid, you get three columns and four rows. The extra rows are created in the implicit grid, using grid-auto-rows, which is auto by default. If you want those rows to have a set height, set grid-auto-rows yourself.
Off-by-one line numbers
A repeated list of N tracks has N + 1 lines. With repeat(4, 1fr), an item that should cover the last column uses grid-column: 4 / 5, not 4 / 4. The shorthand grid-column: 4 is equivalent, because the end line defaults to a one-track span.
Mixing units and overflow
repeat(4, 300px) in a 900px container overflows by 300px plus gaps. Fixed sizes do not shrink. Use fr, minmax(), or auto-fill when the container width is not under your control.
When to use repeat()
Use repeat() when the same size appears more than twice in a row, or when the count should come from a variable. For two or three different sizes, writing them out is often clearer. repeat(2, 1fr) is no shorter or easier to read than 1fr 1fr.
Exercise
Write a grid-template-columns declaration that gives a grid with a 150px first column, then four equal flexible columns that never grow wider than their share because of long content, then a 60px last column. How many column lines does the grid have?
Show answer
grid-template-columns: 150px repeat(4, minmax(0, 1fr)) 60px; The grid has six columns, so it has seven column lines.
Exercise
A grid uses grid-template-columns: repeat(3, 1fr) and contains nine children. You add a tenth child. What happens to the layout, and which property controls the height of the new row?
Show answer
The browser adds a fourth row in the implicit grid, and the tenth child goes in its first cell. The height of that row comes from grid-auto-rows, which defaults to auto.
Exercise
Why is grid-template-columns: repeat(2, repeat(2, 1fr)) invalid, and what is the correct way to get four equal columns?
Show answer
repeat() cannot be nested, so the browser drops the declaration. Use repeat(4, 1fr).