Sub topic 9
Preventing overflow in narrow containers
This chapter assumes you know how repeat(), minmax(), and auto-fit work, and that you can read a basic grid declaration. You also need to be comfortable with px, rem, and percentages. The chapter covers one failure of the auto-fit pattern and the one-function fix for it.
The problem: a fixed minimum can't shrink
The responsive card grid from the previous chapters looks like this:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}The browser fits as many 200px columns as it can. Each column is at least 200px wide, and leftover space is shared out by the 1fr maximum.
This works while the container is at least 200px wide. If the container is narrower, for example 160px inside a small sidebar or on a very small phone, the browser still creates one column. That column's minimum is 200px, so it is wider than its container. The grid overflows. Depending on the parent, you get a horizontal scrollbar or clipped content.
The reason is that minmax(200px, 1fr) is a hard floor. A track never gets narrower than its minimum, even when the container has less room.
The fix: cap the minimum with min()
The CSS min() function returns the smaller of its arguments. Put it inside minmax() to cap the minimum at the container's width:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
gap: 1rem;
}Now the minimum is min(100%, 200px):
- In a container wider than 200px,
100%is larger than200px, so the minimum is200px. The layout behaves as before. - In a container narrower than 200px,
100%is smaller, so the minimum becomes the container's width. One column fills the container and nothing overflows.
Percentages in track sizes refer to the grid container's content box, so 100% means "the full width available to the grid."
How the decision flows
Common mistakes
- Using
min()in the wrong place. The cap belongs in the first argument ofminmax(). Writingminmax(200px, min(100%, 1fr))is invalid, becausefris not a length and can't be used insidemin(). - Using a large minimum with
auto-fill. The overflow problem is the same forauto-fill. Apply the same fix there. - Fixing the symptom with
overflow-x: hidden. This hides the clipped content instead of letting it fit. Fix the track size. - Capping with a viewport unit.
min(100vw, 200px)refers to the viewport, not the container. In a narrow sidebar on a wide screen it would not help. Use100%.
A complete example
This page runs as shown. The narrow wrapper forces the edge case.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Safe minmax</title>
<style>
.narrow { width: 120px; border: 2px solid #444; }
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
gap: 1rem;
}
.cards > div { background: #cde; padding: 0.5rem; }
</style>
</head>
<body>
<div class="narrow">
<div class="cards">
<div>One</div>
<div>Two</div>
</div>
</div>
</body>
</html>The cards stack in one column inside the 120px wrapper. If you change min(100%, 200px) back to 200px, they extend past the wrapper's border.
Summary
A plain minmax(200px, 1fr) can't go below 200px, so it overflows containers narrower than that. Replacing the minimum with min(100%, 200px) keeps the wide-container behavior and lets a single column shrink to fit.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) and overflows inside a 180px-wide widget. Rewrite the declaration so it no longer overflows, and explain why the new minimum works in both a 180px and a 600px container.
Show answer
Use grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));. In the 180px container, 100% is 180px, which is smaller than 250px, so the minimum is 180px and one column fits exactly. In the 600px container, 100% is 600px, which is larger than 250px, so the minimum stays 250px and two columns fit (500px plus the gap is under 600px), sharing the extra space through 1fr.
Exercise
Why is minmax(200px, min(100%, 1fr)) not a valid fix?
Show answer
The min() function only accepts lengths, percentages, and similar values. 1fr is a flexible track unit, not a length, so it can't be an argument to min(). The cap has to go on the minimum, which is the first argument of minmax().