Sub topic 16
Why items refuse to shrink: min-width: 0
This chapter assumes you know how flex-shrink and flex-basis work, and that you are comfortable with the box model and with overflow. It explains why a flex item sometimes overflows its container even though shrinking is allowed, and how to fix it.
The symptom
You build a row with a flexible title and a few fixed buttons. You give the title flex: 1 and expect long text to be cut off with an ellipsis. Instead, the row grows wider than its container, the buttons are pushed off screen, or a horizontal scrollbar appears.
The shrink rules look correct. The default flex-shrink is 1, so items are allowed to get smaller. The cause is a second rule that sits underneath: the automatic minimum size.
The automatic minimum size
In CSS, min-width has the initial value auto. For most boxes, auto computes to 0. For a flex item in a row, auto means something different. The item's minimum width on the main axis becomes its content-based minimum size. This is the smallest size the item can take without its content overflowing.
For an item that holds text, that size is roughly the width of its longest unbreakable piece. This is the width of the whole line if white-space: nowrap is set, or the longest single word if wrapping is allowed. For an image, it is the image's own width. For a nested element, it is that element's own minimum content width.
The flex algorithm never shrinks an item below this minimum. The item is frozen at that size, and the leftover overflow stays in the container.
The same rule applies on the column axis. In a flex-direction: column container, the automatic minimum applies to min-height instead. The rule is about the main axis, not about width in particular.
When the automatic minimum applies
The content-based minimum only applies when both of these are true:
- The item is a flex item whose
overflowisvisibleon the main axis. - The item's computed
min-width(ormin-heightfor columns) isauto.
If either condition is false, the automatic minimum goes away. That gives you two ways to fix the problem.
Reproducing the problem
The following page has a row with a title that should truncate. As written, the title refuses to shrink.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shrink problem</title>
<style>
.row {
display: flex;
gap: 0.5rem;
width: 300px;
border: 1px solid #888;
}
.row .title {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.row button {
flex: none;
}
</style>
</head>
<body>
<div class="row">
<div class="title">A very long document title that does not fit</div>
<button>Edit</button>
<button>Delete</button>
</div>
</body>
</html>Here the title already has overflow: hidden, so its automatic minimum is 0 and the ellipsis works. The problem usually appears when the overflow is on a descendant instead. Look at this variant.
<div class="row">
<div class="title">
<span class="text">A very long document title that does not fit</span>
</div>
<button>Edit</button>
<button>Delete</button>
</div>.row {
display: flex;
gap: 0.5rem;
width: 300px;
}
.row .title {
flex: 1;
}
.row .text {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}The .title item has overflow: visible, so its minimum is its content's minimum width. That is the full width of the unbroken line inside .text. The .title box cannot shrink below it, so the row overflows. The truncation on .text never triggers, because .text is never given a width smaller than its content.
Fix 1: set min-width: 0
The most direct fix is to replace the automatic minimum with an explicit one.
.row .title {
flex: 1;
min-width: 0;
}With min-width: 0, the item may shrink all the way down. The flex algorithm now takes space from .title, .text receives a narrower box, and the ellipsis appears. This is the pattern used in the topic's earlier example, where the title carries min-width: 0 and its own overflow rules.
In a column container, use min-height: 0 for the same reason. This matters for scrolling panels, such as a column with a header, a scrollable body and a footer.
.panel {
display: flex;
flex-direction: column;
height: 100vh;
}
.panel .body {
flex: 1;
min-height: 0;
overflow-y: auto;
}Without min-height: 0, the body would grow to fit all its content and the page itself would scroll, not the panel.
Fix 2: set overflow to something other than visible
Any overflow value other than visible (and clip is treated as visible here) turns off the automatic minimum. So the following also works.
.row .title {
flex: 1;
overflow: hidden;
}This is why many truncation examples work without an explicit min-width: 0: the overflow: hidden on the item does the job. The two fixes differ in side effects. overflow: hidden creates a scroll container and clips anything that extends outside the box, such as focus outlines or box shadows. min-width: 0 changes only the sizing and clips nothing. Prefer min-width: 0 when you only want to permit shrinking.
Other things that resist shrinking
The content-based minimum is not the only reason an item stays wide. Check these as well.
| Cause | What to look for | Remedy |
|---|---|---|
| Automatic minimum size | Long unbroken text, wide images, `nowrap` | `min-width: 0` or `overflow` not `visible` |
| `flex-shrink: 0` | `flex: none` or `flex: 0 0 auto` on the item | Use a value of `flex-shrink` above `0` |
| Explicit `min-width` | A pixel or rem minimum on the item | Lower or remove it |
| Nested flex container | A flex item that is itself a flex container whose children cannot shrink | Apply `min-width: 0` at each level |
| Long words | A URL or a long identifier with no break points | `overflow-wrap: anywhere` |
The nested case catches many people. The minimum size passes up through the tree. If a grandchild has an unbreakable line, the child's content-based minimum includes it, and so does the item above it. Setting min-width: 0 on only one level may not be enough.
Wrapping long words instead of truncating
If you want text to wrap instead of being cut with an ellipsis, overflow-wrap can lower the minimum.
.row .title {
flex: 1;
overflow-wrap: anywhere;
}The value anywhere allows a break at any character and, unlike break-word, it also counts those break points when computing the content-based minimum size. The item can then shrink below the length of the longest word. With overflow-wrap: break-word, the minimum still reflects the full word, so the item does not shrink and the word spills out.
How the algorithm reaches the minimum
It helps to see where the minimum enters the sizing steps.
Because diagrams must use the required fence, here is the sequence in that form.
The loop explains a common surprise. When one item freezes at its minimum, the other items give up more space to compensate. A row may therefore look as though some unrelated items shrank more than you expected.
Debugging checklist
- Open the browser developer tools and select the item that overflows.
- Check its computed
min-width(ormin-height). If it showsauto, the content-based minimum is active. - Temporarily add
min-width: 0in the styles panel. If the overflow disappears, you have confirmed the cause. - If it does not, check ancestors and nested flex items for the same rule.
- Check for
flex-shrink: 0and for fixedmin-widthvalues.
Exercise
A sidebar layout uses .layout { display: flex; } with .sidebar { width: 200px; flex: none; } and .content { flex: 1; }. Inside .content sits a <pre> element with a very long line of code, and the page gets a horizontal scrollbar. Why does this happen, and what two changes to .content would fix it so that the <pre> scrolls inside its own box?
Show answer
.content has min-width: auto, so its minimum size is the width of the long line inside the <pre>. It cannot shrink below that, so the row overflows. Add min-width: 0 to .content so it can shrink, and give the <pre> overflow-x: auto so it scrolls within the shrunken box. Setting overflow: auto on .content instead of min-width: 0 would also remove the automatic minimum.
Exercise
In a column flex container with a fixed height, the middle item has flex: 1 and overflow-y: auto, yet the whole page scrolls instead of the middle item. The middle item is wrapped in another div that has flex: 1 and no other rules. What is the fix?
Show answer
The wrapper div is the flex item, and its min-height is auto. Its minimum height equals its content's height, so it grows to fit everything and the inner scroll area never gets a limit. Add min-height: 0 to the wrapper. If the wrapper is also a flex container, give it display: flex and flex-direction: column so the scroll area can fill it.