Sub topic 6
Repeating and Bounding Tracks with repeat() and minmax()
This chapter assumes you know CSS selectors and the box model, and that you can write a basic display: grid container with fr units and gap. You will learn two functions that make track lists shorter and more flexible: repeat() and minmax().
Why these functions exist
A track is one column or row in a grid. Writing a long list of tracks by hand is tedious and easy to get wrong. Fixed sizes also break when the container is narrower or wider than you expected. repeat() removes the repetition. minmax() lets a track grow and shrink within limits.
Repeating tracks with repeat()
repeat(count, track-size) repeats a track definition a set number of times. These two declarations are identical:
.a { grid-template-columns: 1fr 1fr 1fr 1fr; }
.b { grid-template-columns: repeat(4, 1fr); }You can repeat a pattern of several tracks. The pattern is written once and then repeated as a whole:
.pattern {
display: grid;
grid-template-columns: repeat(2, 100px 1fr);
}This creates four columns: 100px 1fr 100px 1fr. You can also mix repeat() with other tracks, such as 200px repeat(3, 1fr).
In this chapter the count is always a positive whole number. The next chapter introduces the keywords auto-fit and auto-fill in the count position.
Bounding tracks with minmax()
minmax(min, max) sets the smallest and largest size a track may have. The browser picks a size between them, depending on the space available.
.layout {
display: grid;
grid-template-columns: minmax(150px, 300px) 1fr;
gap: 1rem;
}Here the first column is never narrower than 150px and never wider than 300px. The second column takes the remaining space.
The two arguments can use different units. Common values are:
- A length such as
200pxor12rem. - A fraction such as
1fr, allowed only as the maximum. - Keywords such as
auto,min-content, andmax-content.
Why 1fr as a minimum does not work
A plain 1fr track behaves like minmax(auto, 1fr). The auto minimum means the track will not shrink below the size of its content. A long word or a wide image can therefore push a column wider than you intended.
Setting the minimum to 0 fixes this:
.safe {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}Now three columns share the width equally, whatever their content. Wide content may overflow its own cell, but it no longer changes the column sizes.
Combining the two
minmax() can appear inside repeat(). This is the building block for the responsive card grid later in the tutorial:
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(200px, 1fr));
gap: 1rem;
}This creates exactly three columns. Each is at least 200px wide and shares any extra space equally. If the container is narrower than about 600px plus the gaps, the columns do not shrink further, so the grid overflows.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `minmax(1fr, 300px)` | Invalid. `fr` is not allowed as a minimum, so the declaration is ignored. | Put `fr` in the maximum position. |
| `minmax(300px, 100px)` | The maximum is smaller than the minimum, so the browser treats the maximum as equal to the minimum. | Make the maximum larger. |
| `repeat(0, 1fr)` | Invalid. The count must be at least 1. | Use a positive count. |
| Content stretches an `1fr` column | The implicit `auto` minimum grows the track. | Use `minmax(0, 1fr)`. |
Exercise
Write a grid-template-columns value that creates a 250px sidebar column followed by three equal columns that cannot be stretched by their content.
Show answer
grid-template-columns: 250px repeat(3, minmax(0, 1fr));