Sub topic 11
Sizing tracks with minmax()
This chapter assumes you can define columns with grid-template-columns, you know what fr units and repeat() do, and you are comfortable with px, %, and rem. By the end you will be able to give a track a size range instead of a single size, and you will know why 1fr columns sometimes overflow.
What minmax() does
A fixed track such as 200px never changes. A 1fr track grows and shrinks, but its lower limit is set by the browser. minmax(min, max) lets you set both limits yourself.
The function takes two arguments and can be used anywhere a track size is allowed:
.container {
display: grid;
grid-template-columns: minmax(200px, 300px) 1fr;
}The first column is never narrower than 200px and never wider than 300px. Within that range, the browser picks a size based on the space available and the content. The second column takes what is left.
How the browser resolves the range
Grid sizing happens in two broad steps. The browser first gives every track its minimum size. It then distributes any leftover space to tracks that can still grow, up to their maximum.
This explains why minmax(200px, 300px) looks fixed at 300px in a wide container and shrinks toward 200px in a narrow one. If the container is narrower than all the minimums combined, the tracks keep their minimums and the grid overflows.
Values you can use as arguments
Both arguments accept more than lengths.
| Value | Meaning |
|---|---|
| Length or percentage | A fixed size, such as `200px` or `20rem` |
| `fr` | A share of free space. Allowed only as the maximum |
| `auto` | As a minimum, the smallest size the content allows. As a maximum, the content's natural size, with the track also able to stretch |
| `min-content` | The narrowest the content can get without overflowing, for example the longest word |
| `max-content` | The width of the content with no wrapping |
Two rules to remember:
fris valid only as the maximum.minmax(1fr, 300px)is invalid, and the browser discards the whole declaration.- If the minimum is larger than the maximum, the maximum is ignored and the track is treated as the minimum.
minmax(300px, 200px)behaves like300px.
The most useful pattern: minmax(0, 1fr)
A plain 1fr is shorthand for minmax(auto, 1fr). The auto minimum means a track will not shrink below the size of its content. For most text this is fine. Long unbreakable content such as a URL, a wide <pre> block, or an image with a fixed width can push the track wider than expected.
<div class="two-col">
<div>Short text</div>
<pre>https://example.com/a/very/long/path/that/cannot/wrap/anywhere/in/the/line</pre>
</div>.two-col {
display: grid;
grid-template-columns: 1fr 1fr;
width: 400px;
}Here the columns are not equal. The second column grows to fit the long line, and the grid becomes wider than 400px. Changing the columns fixes it:
.two-col {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
width: 400px;
}
.two-col pre {
overflow-x: auto;
}With a minimum of 0, the tracks can shrink below their content width and stay equal. Add overflow-x: auto to the item so the long line scrolls inside its own cell instead of spilling out.
Sidebars and readable text
A common use is a column that is flexible but bounded. A main column with a readable maximum width looks like this:
.page {
display: grid;
grid-template-columns: minmax(1rem, 1fr) minmax(0, 65ch) minmax(1rem, 1fr);
}
.page > * {
grid-column: 2;
}The outer columns act as flexible margins that never drop below 1rem. The middle column holds content up to about 65 characters wide. On a narrow screen, the middle column shrinks, and the margins stay at 1rem. On a wide screen, the margins absorb the extra space and the text remains centered.
Content-based sizing: auto rows and fit-content()
minmax() is not only for columns. Rows often use it to set a minimum height that grows with content:
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(120px, auto);
gap: 1rem;
}Each implicit row is at least 120px tall. If a card holds more text, auto lets that row grow to fit it. Compare this with grid-auto-rows: 120px, which would make long content overflow its card.
A related function is fit-content(limit). It sizes a track to its content but never beyond the limit. It behaves like minmax(auto, max-content) capped at limit:
.toolbar {
display: grid;
grid-template-columns: fit-content(300px) 1fr;
}The first column is as wide as its content, up to 300px.
Using minmax() with repeat()
minmax() fits inside repeat(). This is the foundation of the responsive layouts in the next chapter:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.5rem;
}For now, read this as "each column is at least 240px and shares free space equally." The next chapter explains how auto-fit decides how many columns to create.
Common errors
- Using
fras a minimum.minmax(1fr, 2fr)is invalid. Move the flexible value to the second argument. - Expecting
1frcolumns to be equal. They are equal only if no content forces a larger minimum. Useminmax(0, 1fr)to guarantee it. - Setting a fixed minimum that is too wide.
minmax(300px, 1fr)on a 280px phone screen causes horizontal scrolling. Either lower the minimum or wrap it, for exampleminmax(min(300px, 100%), 1fr). - Swapping the arguments. The minimum comes first. When the minimum exceeds the maximum, the maximum is silently ignored.
- Mixing percentages in unpredictable ways. A percentage minimum is relative to the container's size, but if the container's size depends on its content, browsers may treat the percentage as
autowhile sizing. Prefer lengths orfrwhen the container's width is not fixed.
Choosing a track size
| You want | Use |
|---|---|
| A track that never changes size | `200px` |
| Equal columns that ignore content width | `minmax(0, 1fr)` |
| Columns that share space but respect content | `1fr` |
| A row at least a certain height that grows with content | `minmax(120px, auto)` |
| A column sized to its content with a cap | `fit-content(300px)` |
| A flexible column with a floor | `minmax(200px, 1fr)` |
Exercise
A grid uses grid-template-columns: 1fr 1fr 1fr; inside a 600px container. One item contains an image with width: 400px. The columns are no longer equal. Why does this happen, and what single change to the declaration makes the columns equal again?
Show answer
Each 1fr is really minmax(auto, 1fr). The auto minimum stops the column from shrinking below the 400px image, so that column grows and the others get less space. Replace each 1fr with minmax(0, 1fr), so grid-template-columns: repeat(3, minmax(0, 1fr));. The columns can then shrink below their content width. You should also constrain the image, for example with max-width: 100%, so it does not overflow its cell.
Exercise
Write a grid-auto-rows declaration so that implicit rows are at least 100px tall but grow when their content needs more room.
Show answer
grid-auto-rows: minmax(100px, auto);