flex-grow, flex-shrink and flex-basis
Overview
Flexbox’s real power is how it divides space. Three properties on each item control it:
flex-basis— the size to start from,flex-grow— how much of any free space the item takes,flex-shrink— how much it gives up when there isn’t enough space.
Together they’re usually written with the flex shorthand. Knowing the arithmetic behind them turns flexbox from “fiddle until it looks right” into something predictable.
The algorithm in three steps
- Each item starts at its flex-basis.
- Add up the bases (plus gaps) and compare with the container’s size.
- If there’s space left over, distribute it according to
flex-grow. If they overflow, take the overflow away according toflex-shrink.
flex-basis
flex-basis: auto; /* default: use the item's width (or its content size if width is auto) */
flex-basis: 200px; /* start at 200px */
flex-basis: 0; /* start from nothing — all space is "free" */
flex-basis: 30%; /* a percentage of the container */flex-basis replaces width (or height in a column) as the starting size along the main axis. If both are set, flex-basis wins (unless it’s auto).
flex-grow
flex-grow is a ratio. Free space is divided between items in proportion to their grow factors. The default is 0 — items don’t grow.
The numbers in this diagram were measured in Chrome:
.container { display: flex; width: 600px; }
.a { flex: 1 1 100px; } /* → 175px */
.b { flex: 2 1 100px; } /* → 250px */
.c { flex: 1 1 100px; } /* → 175px */Free space = 600 − (100 + 100 + 100) = 300px. Total grow = 1 + 2 + 1 = 4, so each grow unit is 75px. Item b gets 2 × 75 = 150px extra.
Notice that b is not twice as wide as a — it got twice the extra space. With a basis of 0, the final sizes come out in a 1:2:1 ratio — as long as the items have no padding or borders, which a 0 basis can’t remove. (Grid’s fr unit gives exact ratios even with padding.)
flex-shrink
When items overflow, flex-shrink decides who gives up space. The default is 1 — everyone shrinks. Shrinking is weighted by shrink factor × flex-basis, so larger items shrink more in absolute terms:
.container { display: flex; width: 400px; }
.a { flex: 0 1 200px; } /* → 150px */
.b { flex: 0 2 200px; } /* → 100px */
.c { flex: 0 1 200px; } /* → 150px */Overflow = 600 − 400 = 200px. Weighted factors: 1×200, 2×200, 1×200 = 800. Item b loses 400/800 × 200 = 100px.
flex-shrink: 0 means “never shrink” — use it for things that must keep their size: icons, avatars, fixed-width selects.
The flex shorthand
Always prefer the flex shorthand — it sets all three at once and its keywords are well defined:
| Shorthand | Expands to | Meaning |
|---|---|---|
flex: initial |
0 1 auto |
the default: content-sized, can shrink, won’t grow |
flex: auto |
1 1 auto |
content-sized, then grow and shrink |
flex: none |
0 0 auto |
rigid: exactly its content/width size |
flex: 1 |
1 1 0% |
ignore content size; share all space equally |
flex: 2 |
2 1 0% |
twice the share of a flex: 1 sibling |
flex: 0 0 15rem |
fixed at 15rem |
The important difference is flex: 1 vs flex: auto:
flex: 1(basis0) gives equal widths regardless of content — use it for equal columns.flex: auto(basisauto) starts from each item’s content size and shares only the leftover space — items with more content end up wider.
The minimum size trap
By default, a flex item won’t shrink below its minimum content size — the width of its longest word, or its widest image or input. This is min-width: auto, a special default for flex items:
.search input { flex: 1; }
/* on a narrow screen, the input refuses to shrink below its default width and overflows */The fix is to allow shrinking below the content size:
.search input { flex: 1; min-width: 0; }You’ll need min-width: 0 (or overflow: hidden, which has the same effect) whenever a flex item contains something wide that should be able to shrink or truncate: long words, white-space: nowrap text with an ellipsis, <pre> code blocks, nested flex containers, or form inputs. It’s the most common cause of mysterious horizontal scrolling in flex layouts. In a column, the same applies to min-height.
Common recipes
/* Equal columns */
.col { flex: 1; }
/* Sidebar + flexible content */
.sidebar { flex: 0 0 16rem; }
.content { flex: 1; min-width: 0; }
/* Push one item to take remaining space */
.search input { flex: 1; min-width: 0; }
/* Icon that must never shrink */
.icon { flex: none; }
/* Items that grow from their natural sizes */
.tag { flex: auto; }Debugging tip: turn on the flex overlay in DevTools. It shows each item’s basis, the free space and how much each item grew or shrank — the arithmetic above, drawn for you.
A search bar that fills the row
In a 40rem-wide .search row there’s a select (fixed at 8rem, never shrinking), an input that should take all the remaining space, and a button sized to its text. Make it work — and make sure the input can shrink below its default minimum width on very narrow screens instead of overflowing. Then build a .split row of three panels where the middle panel gets twice the share of space of the others, regardless of their content. Measure the panels in DevTools: is the middle one exactly twice as wide? Explain why not.
<form class="search">
<select><option>All courses</option></select>
<input type="search" placeholder="Search…" />
<button>Search</button>
</form>
<div class="split">
<div>Short</div>
<div>A panel with quite a lot more content inside it</div>
<div>Short</div>
</div>
<style>
* { box-sizing: border-box; }
.search { max-width: 40rem; }
.split > div { background: #eaf0fc; padding: 1rem; }
/* your code here */
</style><form class="search">
<select><option>All courses</option></select>
<input type="search" placeholder="Search…" />
<button>Search</button>
</form>
<div class="split">
<div>Short</div>
<div>A panel with quite a lot more content inside it</div>
<div>Short</div>
</div>
<style>
* { box-sizing: border-box; }
.search { max-width: 40rem; }
.split > div { background: #eaf0fc; padding: 1rem; }
.search {
display: flex;
gap: 0.5rem;
}
.search select { flex: 0 0 8rem; } /* fixed: no grow, no shrink */
.search input { flex: 1; min-width: 0; } /* takes the rest; may shrink fully */
.search button { flex: none; } /* exactly its content size */
.split {
display: flex;
gap: 1rem;
}
.split > div { flex: 1; } /* basis 0: content size is ignored */
.split > div:nth-child(2) { flex: 2; } /* twice the share of the space */
/*
At 900px wide the panels measure 225px, 418px and 225px — not exactly 1:2:1.
A 0 basis can't be smaller than the panel's padding, so each panel starts
at 32px (2 × 1rem) and only the remaining space is shared 1:2:1.
For exact ratios, use grid: grid-template-columns: 1fr 2fr 1fr gives 217, 434, 217.
*/
</style>Summary
flex-basisis an item’s starting size on the main axis;autouses its width or content size.flex-growshares out free space in proportion to each item’s grow factor.flex-shrinkremoves overflow in proportion to shrink factor × basis, so bigger items shrink more.- Use the
flexshorthand:flex: 1(1 1 0%),flex: auto(1 1 auto),flex: none(0 0 auto). - Flex items won’t shrink below their content’s minimum width by default; set
min-width: 0to allow it.