🎨 Fix Text Inside a Flex Item Overflowing Past a Fixed-Width Sibling Because Flex Items Default to a Minimum Width of Their Content
Every flex item’s computed min-width defaults to auto, which resolves to the size of its content’s longest unbreakable run – not zero – so a flex-basis or flex-grow value is only ever a target the item shrinks toward, never a hard ceiling it’s forced below. A long unbroken word keeps the item wider than its flex-basis no matter how much flex-shrink you give it, pushing it past a fixed-width sibling instead of wrapping.
🔎 The Problem
.row { display: flex; }
.sidebar { width: 240px; flex-shrink: 0; }
.main { flex: 1 1 auto; } /* expected to shrink to fit */
Computed widths at a narrow viewport:
.sidebar: 240px (correct, flex-shrink: 0)
.main: content's intrinsic min-width (a long URL or
unbroken string), NOT the flex-basis - overflows
past .sidebar's right edge instead of wrapping
Root cause: .main never received an explicit min-width,
so its default of min-width: auto resolves to 'as wide as
my longest unbreakable content needs,' overriding flex-
shrink entirely for that one dimension.
✅ Fix: Override the Default min-width Explicitly
- Set min-width: 0 on any flex item that should be allowed to shrink below its content’s intrinsic size – this is the single line that actually unlocks flex-shrink for text and block content.
- For a flex item containing a long unbreakable string specifically, pair min-width: 0 with overflow-wrap: anywhere (or word-break: break-word) on the inner text so it wraps instead of just clipping once it’s allowed to shrink.
- Apply the same min-width: 0 fix to nested flex or grid containers inside the item too – a child flex container inherits this same default independently and can reintroduce the overflow one level deeper.
🚨 Why This Is Dangerous
- Setting min-width: 0 globally on every flex item can hide a genuine layout bug elsewhere by letting content shrink to unreadable widths silently – apply it to the specific item that needs to shrink, not as a blanket reset.
flex-shrink says how eagerly an item gives up space – min-width: auto is the floor that decides whether it’s even allowed to.
