flex-grow, flex-shrink, flex-basis?

These three properties decide how a flex item is sized along the main axis. flex-basis is the item's starting size before free space is distributed; flex-grow is its share of any extra space; flex-shrink is how much it gives up when there is not enough space. They are usually set together with the flex shorthand.

The properties

  • flex-basis: auto (default; use the item's width/height or content size), a length (200px, 30%) or 0.
  • flex-grow: a unitless ratio, default 0 (do not grow). Items with grow: 2 get twice as much of the extra space as items with grow: 1.
  • flex-shrink: a ratio, default 1. 0 prevents shrinking (useful for icons and avatars). Shrinking is weighted by basis, so larger items shrink more.

The flex shorthand

ShorthandMeansUse
flex: initial0 1 autoDefault
flex: 11 1 0%Equal-width columns, ignoring content size
flex: auto1 1 autoGrow, but start from content size
flex: none0 0 autoRigid item
flex: 1 1 200pxExplicitResponsive cards that wrap

Worked example

Container 600px; three items with basis 100px = 300px; 300px free. With grow 1, 1, 1 → each 200px. With grow 2, 1, 0 → 300, 200, 100.

Gotchas

  • flex: 1 uses a basis of 0, so items are equal regardless of content; flex: auto makes longer content get wider items.
  • Items will not shrink below their minimum content width (min-width: auto); set min-width: 0 for text truncation.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 .row { display: flex; width: 100%; max-width: 600px; gap: 4px; margin: 6px 0 14px; }
4 .row > div { background: #dbeafe; border: 2px solid #0b1020; padding: 6px; white-space: nowrap; }
5 .ratio div:nth-child(1) { flex: 2 1 100px; }
6 .ratio div:nth-child(2) { flex: 1 1 100px; }
7 .ratio div:nth-child(3) { flex: 0 1 100px; }
8 .layout aside { flex: 0 0 140px; background: #fef9c3; } /* rigid sidebar */
9 .layout main { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
10</style>
11<code>flex: 2 / 1 / 0 (basis 100px)</code>
12<div class="row ratio"><div>grow 2</div><div>grow 1</div><div>grow 0</div></div>
13<code>flex: 1 (equal) vs flex: auto (content-based)</code>
14<div class="row"><div style="flex:1">short</div><div style="flex:1">much longer content</div></div>
15<div class="row"><div style="flex:auto">short</div><div style="flex:auto">much longer content</div></div>
16<code>flex: none sidebar + flex: 1 main with min-width: 0</code>
17<div class="row layout"><aside>Sidebar 140px</aside><main>A very long title that truncates instead of overflowing the layout container</main></div>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app