Sub topic 9
Preventing overflow in narrow containers
This chapter covers one failure of repeat(auto-fit, minmax(200px, 1fr)) and its one-line fix. It assumes you can write that declaration, as covered in the earlier chapters on repeat(), minmax() and auto-fit. It also assumes you know the CSS box model and what a percentage resolves against. After this chapter you should be able to explain why the plain pattern overflows, what min(100%, 200px) changes, and which overflow problems the fix does not cover.
The problem: a minimum that cannot shrink
Here is the card grid pattern from the previous chapters:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}The 200px in minmax(200px, 1fr) is a hard floor. No track can be narrower than 200px. The browser calculates how many tracks fit, and if even one does not fit, it still creates one. The grid always has at least one column. That column is 200px wide whatever the container offers.
Suppose the container is 150px wide. The grid makes a single 200px track, which sticks out 50px past the container. The result is a horizontal scrollbar, clipped content, or cards that overlap a neighbouring element.
On a wide desktop this never shows up. It appears in real situations such as:
- A phone in portrait mode with a narrow viewport and padding on the container.
- A sidebar or dialog that holds a card grid.
- A grid inside a multi-column layout, where each column is already narrow.
- A page zoomed to 200% or more, where the layout viewport shrinks in CSS pixels.
Because the grid has no media queries, no breakpoint reacts to these cases. The pattern is responsive across a range of widths but has a hard lower limit.
How many columns, exactly
The column count is a calculation, so you can predict it. With a gap, the browser fits as many tracks as satisfy this inequality:
count × minimum + (count − 1) × gap ≤ container widthSolving for the count gives floor((width + gap) / (minimum + gap)). If that result is below 1, the browser uses 1.
The table below uses a 200px minimum and a 16px gap (1rem at the default font size).
| Container width | Columns | Width the tracks need | Result |
|---|---|---|---|
| 150px | 1 (forced) | 200px | Overflows by 50px |
| 200px | 1 | 200px | Fits exactly |
| 300px | 1 | 200px | Fits, `1fr` stretches the track to 300px |
| 432px | 2 | 416px | Fits |
| 648px | 3 | 632px | Fits |
| 900px | 4 | 848px | Fits |
Only the first row breaks. The grid is correct at every width where at least one track fits, and fails below the minimum itself.
The fix: cap the minimum at the container
Replace the fixed minimum with the smaller of the container width and the preferred minimum:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
gap: 1rem;
}min() is a CSS math function that returns the smallest of its arguments. Here it compares 100% with 200px.
100%inside a grid track size resolves against the width of the grid container's content box. Padding and borders are already excluded.- When the container is 600px wide,
min(100%, 200px)is200px. The layout is identical to the unsafe version. - When the container is 150px wide,
min(100%, 200px)is150px. The single column is 150px, and nothing overflows.
The floor is now "200px, unless the container is smaller, in which case the whole container". The change affects only containers narrower than 200px, so wide layouts stay as they were.
Seeing it work
This page puts both versions inside a 150px wrapper so you can compare them. Save it as an .html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>minmax overflow demo</title>
<style>
.narrow {
width: 150px;
outline: 2px solid crimson;
margin-bottom: 2rem;
}
.cards {
display: grid;
gap: 1rem;
}
.unsafe { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.safe { grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); }
.cards > div {
background: #dbeafe;
padding: 1rem;
box-sizing: border-box;
}
</style>
</head>
<body>
<p>Unsafe: the card is 200px inside a 150px box.</p>
<div class="narrow">
<div class="cards unsafe"><div>Card A</div><div>Card B</div></div>
</div>
<p>Safe: the card is 150px and stays inside the box.</p>
<div class="narrow">
<div class="cards safe"><div>Card A</div><div>Card B</div></div>
</div>
</body>
</html>In the first box the blue cards run past the red outline. In the second they stay inside it, and the two cards stack in one column. Open the browser's developer tools and inspect the grid. The track sizes show 200px in the first case and 150px in the second.
Choosing the numbers
The two numbers in the pattern do different jobs. In minmax(min(100%, 200px), 1fr):
200pxis the width you would like a card to have at least, so it determines how many columns appear.100%is the safety limit and almost never needs changing.
You can use relative units for the preferred minimum, for example min(100%, 14rem). Then the card width scales with the user's font size, which suits text-heavy cards. Pixel values suit cards built around fixed-size media.
Two details are easy to get wrong:
- The
min()goes insideminmax(), as its first argument. Writingmin(100%, minmax(...))is invalid, becausemin()takes lengths and percentages, not track sizes. - You can write
minmax(min(200px, 100%), 1fr)with the arguments in either order.min()is symmetric.
The fix applies equally to auto-fill. If you use repeat(auto-fill, minmax(min(100%, 200px), 1fr)), the one-column fallback has the same problem and the same cure.
Where the fix stops working
Capping the track minimum does not cover every overflow. These are the cases to check.
Content wider than the track
The track is now the right size, but an item can still be wider than its track. A long unbroken word, a URL, a <pre> block or an image with a fixed width has a content-based minimum size. By default a grid item will not shrink below that minimum, so it spills out of the track.
.cards > * {
min-width: 0; /* allow the item to shrink below its content size */
overflow-wrap: anywhere; /* break long words and URLs */
}
.cards img {
max-width: 100%;
height: auto;
}min-width: 0 removes the automatic minimum. overflow-wrap: anywhere lets the browser break a long string at any point, which is acceptable for URLs and identifiers but not always for prose. For a <pre> block, give it overflow-x: auto so the code scrolls inside its card instead of stretching the page.
A container whose width depends on its content
A percentage needs a known width to resolve against. If the grid container sizes itself from its content, the browser cannot use 100% without a circular dependency. This happens when the container is an inline-grid, a float, or a flex item with no width set. In these cases the percentage may not behave as you expect. Give the container a definite width, or set min-width: 0 on a flex item so it can be narrower than its content.
Padding on the grid container
You rarely need to account for padding, because 100% already refers to the content box. A container with padding: 1rem that is 150px wide has a 118px content box. The safe column is therefore 118px wide.
Plain `1fr` columns
The same kind of overflow appears with fixed column counts. grid-template-columns: repeat(3, 1fr) is shorthand for minmax(auto, 1fr) columns, which means a wide item can push a column wider than its share. If you need columns that stay equal whatever their contents, write repeat(3, minmax(0, 1fr)).
When to use the safe form
Use the safe form every time you write the auto-fit or auto-fill pattern. The extra characters cost nothing, and a card grid is often reused in places you did not design for, such as a sidebar, a modal or an embedded widget.
A plain minmax(200px, 1fr) is acceptable only when you control the container and know it can never be narrower than the minimum. That condition is hard to guarantee once users can zoom or resize text, so treat the safe form as the default.
Summary of the pattern
| Declaration | Narrow container (150px) | Wide container (900px) |
|---|---|---|
| `minmax(200px, 1fr)` | One 200px track, overflows | Four columns |
| `minmax(min(100%, 200px), 1fr)` | One 150px track, fits | Four columns |
| `minmax(0, 1fr)` with `repeat(3, ...)` | Three 50px tracks, fits but cramped | Three equal columns |
The first two rows differ only on narrow containers, which is what the fix is for. The third row shows the other tool in this chapter. It protects a fixed column count from content, but it does not decide how many columns to create.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; and sits in a container that is 200px wide with no padding. How wide is the single column, by how much does it overflow, and what declaration fixes it?
Show answer
The column is 240px wide, because 240px is the minimum track size and one track is always created. It overflows the 200px container by 40px. The fix is grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));, which makes the minimum 200px, the smaller of 100% of the container and 240px.
Exercise
After applying minmax(min(100%, 200px), 1fr), a card in a 320px container still pokes out of its track because it contains a 600-character URL with no spaces. Why does this happen, and which two declarations on the card address it?
Show answer
The track is correctly sized, but a grid item has an automatic minimum size based on its content, and an unbreakable string sets that minimum to the string's full width. The item will not shrink below it, so it overflows the track. Setting min-width: 0 on the item removes the automatic minimum, and overflow-wrap: anywhere lets the URL break across lines.
Exercise
With repeat(auto-fit, minmax(min(100%, 200px), 1fr)) and gap: 1rem (16px), how many columns appear in a 450px container? Show the calculation.
Show answer
Two columns. The count is floor((450 + 16) / (200 + 16)) = floor(466 / 216) = floor(2.16) = 2. Two 200px tracks plus one 16px gap need 416px, which fits in 450px. Three columns would need 632px.