How it works
§9.7 "Resolving Flexible Lengths" runs once per flex line:
- Pick the factor. Sum the outer hypothetical main sizes. Below the container's inner size, use
flex-grow; otherwise flex-shrink.
- Freeze inflexible items at their hypothetical size: any item whose factor is
0, and, when growing, any item already clamped by its max size (base > hypothetical), or, when shrinking, any clamped by its min size.
- Initial free space = container inner size − (frozen items' outer target sizes + other items' outer base sizes).
- Loop while unfrozen items remain:
- Remaining free space is computed the same way. If the unfrozen items' factors sum to less than 1, use initial free space × that sum when that is smaller in magnitude.
- Distribute it: by
flex-grow ratio, or by scaled flex-shrink ratio, starting each item from its base size.
- Clamp each unfrozen item by its min and max sizes (and floor its content box at zero). Record whether each clamp was a min or a max violation.
- Sum the violations (clamped − unclamped). Zero: freeze everything. Positive: freeze the min violators. Negative: freeze the max violators. Then repeat.
- Each item's used size is its target size.
A shrink case with a min violation:
<div class="row" style="display: flex; width: 300px">
<div style="flex: 1 1 200px; min-width: 180px"></div>
<div style="flex: 1 1 200px"></div>
</div>
<script>
const [a, b] = document.querySelectorAll(".row > div");
console.log(a.getBoundingClientRect().width, b.getBoundingClientRect().width);
// → 180 120
</script>
Pass 1: 100px too much, equal scaled factors, so both target 150. A is clamped up to 180, a positive violation, so A freezes. Pass 2: 300 − 180 − 200 = −80 goes entirely to B: 120. Chrome 155 and Firefox 158 agreed.
Edge cases
- Partial fill. With
flex-grow: 0.25 on both items of a 400px row and basis 0, each got 100px; the spec intends this so that growth fades smoothly to zero as the factor approaches 0.
- Base size ignores min and max. Clamping happens for the hypothetical size and after distribution, never to the base size. That's why a max-clamped item is frozen in step 2.
box-sizing: border-box with padding. flex-basis then sizes the border box, so an item with basis 0 and 20px of padding has a negative inner base size; the spec allows this and floors it later.
- Percentage bases in an auto-height column container fall back to
content, because the container's height is indefinite. With overflow: hidden (so no automatic minimum), a flex-basis: 50% item with 30px of content was 30px tall; a 0px basis item was 0.
Real-world scenario
A fixed sidebar and a fluid main area:
.layout { display: flex; }
.sidebar { flex: 0 0 240px; } /* rigid */
.main { flex: 1; min-width: 0; } /* takes the rest, may shrink below its content */
In an 800px container the main area was 560px in both browsers. With a 1000px-wide child and no min-width: 0, .main stayed 1000px wide and the row overflowed the container; with it, .main kept 560px and the child overflowed inside it, where overflow: auto can scroll it.
Trade-offs
flex: 1 vs flex: auto. flex: 1 ignores content when sharing space: good for equal tabs or columns. flex: auto keeps content proportions and only shares the extra: good for buttons whose labels differ.
flex-basis vs width. Prefer flex-basis (through the shorthand) on items; the spec recommends the shorthand because it resets unspecified parts to useful values. Use width when the item may stop being a flex item.
- Grow vs alignment. If items shouldn't stretch, leave
flex-grow: 0 and use justify-content or auto margins to place the spare space instead.