Flex items default to min-width: auto, so they refuse to shrink below their content. Learn why long text or wide children overflow and how min-width: 0 fixes it.
The short answer
#Flex items have a default min-width of auto, which means they will not shrink smaller than their content. A long URL, a wide table or a pre block inside the item pushes it past the edge of the container. Set min-width: 0 on the flex item that should be allowed to shrink.
_10.card-body {_10 flex: 1;_10 min-width: 0; /* let the item shrink below its content width */_10}
Why it happens
#In normal block layout, a box takes the width its parent gives it and the content wraps or overflows inside it. Flexbox works differently. The flexbox spec gives flex items an automatic minimum size: for an item whose overflow is visible, min-width: auto resolves to the smallest width the content can have without overflowing. That is usually the width of the longest word, image or unbreakable string.
So when the container gets narrow, the browser tries to shrink the item, hits that content-based minimum and stops. The item stays wide and spills out of its parent, often taking a horizontal scroll bar with it.
The same thing happens vertically with min-height: auto in a column flex container, which is why a scrolling panel inside a full-height flex layout sometimes refuses to scroll and grows the page instead.
Common triggers are:
- Long URLs, email addresses or hashes with no spaces.
- Code blocks with
white-space: pre. - Text that uses
white-space: nowrapwithtext-overflow: ellipsis. - Nested flex or grid children, where the inner item is the one with the wide content.
- Images or tables with a fixed width.
How to fix it
#Set min-width: 0 on the flex item
This is the standard fix. It replaces the content-based minimum with zero, so the item can shrink to whatever space is available. The content then wraps, truncates or overflows inside the item, where you can control it.
_13.layout {_13 display: flex;_13 gap: 1rem;_13}_13_13.sidebar {_13 flex: 0 0 16rem;_13}_13_13.main {_13 flex: 1;_13 min-width: 0;_13}
If the wide content is several levels deep, every flex item between the container and that content needs min-width: 0. Check each level in DevTools until the overflow stops.
Set an overflow value other than visible
An item with overflow: hidden, auto or scroll also gets a minimum size of zero. Use this when you want the item to clip or scroll its own content, for example a code block that should scroll sideways.
_10.main {_10 flex: 1;_10 overflow-x: auto;_10}
Be careful with overflow: hidden, because it can also clip focus rings, shadows and dropdowns.
Let long words break
If the culprit is a long unbroken string, allow it to wrap as well as letting the item shrink.
_10.main {_10 min-width: 0;_10 overflow-wrap: anywhere;_10}
Use minmax(0, 1fr) in grid
CSS Grid has the same automatic minimum. A 1fr track has a minimum of auto, so wide content can stretch it. Use minmax(0, 1fr) to allow the track to shrink.
_10.layout {_10 display: grid;_10 grid-template-columns: 16rem minmax(0, 1fr);_10}
Common pitfalls
#- Adding
min-width: 0to the container instead of the item. The rule belongs on the flex child. - Fixing only the outer item when the wide content sits inside a nested flex item. Each level needs it.
- Reaching for
width: 100%ormax-width: 100%. These do not override the automatic minimum on their own. - Using
flex-shrink: 1and expecting it to help. Items already shrink by default; the minimum size is what stops them. - Forgetting the vertical version. In a column layout with a scrolling child, use
min-height: 0.
Related reading
#Was this resource helpful?