flex-grow, flex-shrink and flex-basis

Topic 2 of 5 in Flexbox8 min readBeginnerFree course

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

  1. Each item starts at its flex-basis.
  2. Add up the bases (plus gaps) and compare with the container’s size.
  3. If there’s space left over, distribute it according to flex-grow. If they overflow, take the overflow away according to flex-shrink.

flex-basis

CSS
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.

Growing: a 600px container with three items, each with flex-basis 100px and flex-grow 1, 2 and 1. Free space is 600 − 300 = 300px, shared 75, 150 and 75, so the items end up 175px, 250px and 175px. Shrinking: a 400px container with three items, each with flex-basis 200px and flex-shrink 1, 2 and 1. The overflow is 600 − 400 = 200px, removed 50, 100 and 50 because shrinking is weighted by shrink × basis, so the items end up 150px, 100px and 150px. Shorthands: flex: 1 = 1 1 0%, flex: auto = 1 1 auto, flex: none = 0 0 auto.

The numbers in this diagram were measured in Chrome:

CSS
.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:

CSS
.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 (basis 0) gives equal widths regardless of content — use it for equal columns.
  • flex: auto (basis auto) 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:

CSS
.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:

CSS
.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

CSS
/* 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.

Exercise

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.

HTML
<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>

Summary

  • flex-basis is an item’s starting size on the main axis; auto uses its width or content size.
  • flex-grow shares out free space in proportion to each item’s grow factor.
  • flex-shrink removes overflow in proportion to shrink factor × basis, so bigger items shrink more.
  • Use the flex shorthand: 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: 0 to allow it.