Sub topic 6
Repeating and bounding tracks with repeat() and minmax()
This chapter covers two CSS Grid functions that make column definitions shorter and more flexible: repeat() and minmax(). You will use them to define columns that have a floor, a ceiling, and a repeating pattern. The next chapters build responsive layouts on top of them.
Prerequisites
This chapter assumes you already know the following:
- Basic CSS selectors and the box model.
- How to create a grid container with
display: grid. - How
frunits andgapsize tracks. A track is a single row or column in a grid. - Length units such as
px,rem, and percentages.
The problem: long track lists
You can define columns by listing a size for each one. For a four-column grid of equal columns, that looks like this:
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
gap: 1rem;
}This works, but it gets tedious. A twelve-column grid needs twelve 1fr values. Changing the count means editing the list by hand. The repeat() function removes this repetition.
repeat(): saying "this, N times"
repeat() takes two arguments, separated by a comma: how many times to repeat, and the track list to repeat.
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}This is identical to 1fr 1fr 1fr 1fr. The first argument is the repeat count. It must be a positive integer, or one of the keywords auto-fit or auto-fill, which later chapters cover.
Repeating a pattern
The second argument can contain several track sizes. The whole group repeats.
.grid {
display: grid;
grid-template-columns: repeat(2, 100px 1fr);
}This expands to 100px 1fr 100px 1fr. The grid has four columns that alternate between a fixed width and a flexible width.
Mixing repeat() with other tracks
You can place repeat() next to ordinary track sizes in the same declaration.
.grid {
display: grid;
grid-template-columns: 200px repeat(3, 1fr) 200px;
}This creates five columns. The outer two are 200px wide. The middle three share the remaining space equally.
Where repeat() works
repeat() also works in grid-template-rows. This chapter focuses on columns because that is where responsive behavior matters most.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 120px);
gap: 1rem;
}This defines a three-by-two grid of explicit cells. Items beyond the sixth create extra rows in the implicit grid. Those rows are sized automatically, not at 120px, unless you set grid-auto-rows.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 120px;
gap: 1rem;
}Here every row is 120px, however many items you add.
Why plain fr is not always enough
With repeat(4, 1fr), the columns share the available width equally. That sounds ideal, but 1fr has a hidden property. It is shorthand for minmax(auto, 1fr). The minimum is auto, which means the content's minimum size.
This has a visible consequence. If one item contains a long unbreakable string, such as a URL or a wide image, its column can grow beyond its fair share. The columns are no longer equal.
<div class="grid">
<div>Short</div>
<div>Short</div>
<div>https://example.com/a/very/long/address/with/no/spaces/or/break/points</div>
</div>.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}In a narrow container, the third column stretches to fit the long string, and it can push the grid wider than its parent. To control this, you need to set the minimum yourself. That is the job of minmax().
minmax(): a floor and a ceiling
minmax(min, max) defines a track size that is never smaller than min and never larger than max. The browser picks a value within that range, depending on the space available.
.grid {
display: grid;
grid-template-columns: minmax(150px, 300px) 1fr;
}The first column is at least 150px and at most 300px. If the container is wide, it grows toward 300px. If space is tight, it shrinks toward 150px but not below. The second column takes the rest.
Accepted values
Each argument can be a length, a percentage, an fr value (only as the maximum), or a keyword.
| Value | Where allowed | Meaning |
|---|---|---|
| `200px`, `10rem`, `30%` | min or max | A fixed or relative length |
| `1fr` | max only | A share of the leftover space |
| `auto` | min or max | As a min, the content's minimum size. As a max, the content's size or stretched to fill |
| `min-content` | min or max | The narrowest the content can be without overflowing |
| `max-content` | min or max | The width the content wants if it never wraps |
An fr value is not valid as the minimum. For example, minmax(1fr, 300px) is invalid, and the browser discards the whole declaration.
The most common pattern: minmax(Npx, 1fr)
Setting the max to 1fr makes the track flexible. Setting the min to a length gives it a firm floor.
.grid {
display: grid;
grid-template-columns: repeat(3, minmax(200px, 1fr));
gap: 1rem;
}Each column is at least 200px. When the container is wide enough, the three columns share the extra space equally. When the container is narrower than three columns plus gaps (about 632px here), the columns stay at 200px and the grid overflows its container. The fixed count of 3 does not adapt to the width.
Fixing the long-string problem with minmax(0, 1fr)
A minimum of 0 stops content from forcing a column wider. Combined with 1fr, you get strictly equal columns.
.grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}Now the long URL cannot widen its column. The text overflows its own cell instead, so you may want overflow-wrap: anywhere on the items.
.grid > * {
overflow-wrap: anywhere;
}The two forms behave differently, so choose deliberately:
| Declaration | Minimum track size | Effect of long content |
|---|---|---|
| `repeat(3, 1fr)` | The content's minimum size | Column grows, columns become unequal |
| `repeat(3, minmax(0, 1fr))` | 0 | Columns stay equal, content may overflow its cell |
| `repeat(3, minmax(200px, 1fr))` | 200px | Columns never narrower than 200px, grid may overflow |
How the browser resolves a minmax() track
Knowing the sizing order helps you predict results. For a simple case with minmax(Npx, 1fr) tracks, the browser works like this:
- It gives each track its minimum size.
- It subtracts the minimums and the gaps from the container's width to find the leftover space.
- It distributes the leftover space among the
frtracks in proportion to theirfrvalues.
The "No" branch is the important edge case. A fixed repeat count with a pixel minimum cannot shrink below the sum of its minimums. The next chapter solves this by letting the browser choose the count.
Using different fr proportions
Because the max is an fr value, you can weight tracks unevenly.
.grid {
display: grid;
grid-template-columns: minmax(180px, 1fr) minmax(180px, 2fr);
gap: 1rem;
}With a 700px container and a 16px gap, the minimums take 360px and the gap takes 16px. That leaves 324px. The leftover is split one part to two, which adds 108px to the first track and 216px to the second. The final widths are 288px and 396px.
Notice that the minimum is added first, then the extra is divided. The tracks are not in a strict 1:2 ratio. Their totals are 288:396.
A common mistake: flexible tracks with percent minimums
A percentage minimum refers to the grid container's width, not the remaining space.
.grid {
display: grid;
grid-template-columns: repeat(3, minmax(40%, 1fr));
gap: 1rem;
}Three tracks at 40% need 120% of the container width, so the grid always overflows. When you combine repeat() with a percentage minimum, make sure the count times the percentage fits within 100% after gaps.
Edge cases and gotchas
- Invalid repeat counts.
repeat(0, 1fr)andrepeat(-2, 1fr)are invalid. The count must be at least 1. - No nesting of repeat(). A
repeat()cannot contain anotherrepeat()ingrid-template-columns. Write the pattern out, or restructure the grid. - min greater than max. In
minmax(300px, 200px), the max is treated as equal to the min. The track is 300px. - Gaps count.
gapis subtracted from the available width beforefris distributed. If your columns look slightly narrower than expected, check the gap. - fr as a minimum is invalid. Only lengths, percentages, and the content keywords work as the minimum.
A worked example
This example builds a three-column feature row where the first column holds a label with a fixed range, and the remaining columns share space equally without being pushed wider by long text.
<div class="features">
<div>Label</div>
<div>First description with a long-address-like-string-that-does-not-break</div>
<div>Second description</div>
</div>.features {
display: grid;
grid-template-columns: minmax(80px, 160px) repeat(2, minmax(0, 1fr));
gap: 1rem;
}
.features > * {
overflow-wrap: anywhere;
}Reading the declaration from left to right:
minmax(80px, 160px)gives the label column a width between 80px and 160px. It is not flexible because its max is a length, so it stops at 160px.repeat(2, minmax(0, 1fr))creates two columns that share all remaining space equally.overflow-wrap: anywherelets the long string break so it stays inside its cell.
Remember that a track with a length maximum does not grow into leftover space beyond that maximum. In a very wide container, the browser may still stretch tracks depending on justify-content, which defaults to normal and behaves like stretch for auto tracks only. Since minmax(80px, 160px) has a length max, it stays at 160px, and the two fr tracks absorb the rest.
Summary
repeat(N, tracks)writes a repeated track list in a short form. The group of tracks can contain more than one size.minmax(min, max)bounds a track. The max may be1frbut the min may not.1frmeansminmax(auto, 1fr). Useminmax(0, 1fr)for strictly equal columns.- A fixed repeat count with a pixel minimum overflows when the container is too narrow. Letting the browser choose the count is the next step.
Exercise
A grid uses grid-template-columns: repeat(3, minmax(220px, 1fr)); gap: 20px;. The container is 600px wide. Does the grid overflow, and by how much?
Show answer
Yes. The three minimums take 3 × 220px = 660px and the two gaps take 2 × 20px = 40px, so the grid needs 700px. The container is 600px wide, so the grid overflows by 100px. The count is fixed at 3, so the columns cannot shrink below 220px.
Exercise
Two columns are defined as repeat(2, 1fr). One cell contains a very long unbroken URL, and the two columns end up with different widths. Rewrite the declaration so the columns stay equal, and name one additional property that keeps the URL inside its cell.
Show answer
Use grid-template-columns: repeat(2, minmax(0, 1fr));. The 1fr shorthand has an automatic minimum equal to the content's minimum size, which lets the long URL widen its column. Setting the minimum to 0 removes that. Adding overflow-wrap: anywhere to the items lets the URL break so it stays inside its cell.
Exercise
Is grid-template-columns: minmax(1fr, 400px) 1fr; valid? Explain why or why not.
Show answer
No. An fr value is allowed only as the maximum of minmax(), never as the minimum. The browser discards the declaration, so the grid falls back to its default single-column behavior. A valid version would be minmax(100px, 400px) 1fr or minmax(0, 1fr) 1fr.