flex-grow, flex-shrink and flex-basis

Intermediate · 16 min

How a flex container shares out extra space or takes it back, what the flex shorthand sets, and why items refuse to shrink until min-width is zero.

Before thisFlexbox Fundamentals

Kevin Powell · 16 min

flex-grow, flex-shrink and flex-basis

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Flex sizing is a two-step process. Each item starts from its `flex-basis` (its hypothetical size). If the items together are smaller than the container, leftover space is shared by `flex-grow`. If larger, the overflow is taken back by `flex-shrink`.
  • `flex-grow` shares leftover space in proportion to its value: in a 600px row with three 100px-basis items and grow values 1, 2 and 3, the items end up 150px, 200px and 250px wide.
  • `flex-shrink` is weighted by basis. With a 200px container and items of 300px and 100px, the items shrink to 150px and 50px, because the bigger item gives up more.
  • In the `flex` shorthand, `flex: 1` means `1 1 0%`, `flex: auto` means `1 1 auto`, `flex: none` means `0 0 auto`, and the default is `0 1 auto`. Note that `flex: 1` makes the basis `0%`, not `auto`.
  • A flex item's `min-width` defaults to `auto`, which is its content's minimum size. A long unbreakable word or wide image stops it shrinking, and it overflows. Set `min-width: 0` on the item to allow shrinking.

Two phases of sizing

Imagine a shelf of fixed length, and books of varying thickness. First you set every book on the shelf at its natural thickness. If there is empty shelf left, you decide who gets it. If the books overflow, you decide who gets squeezed. Flexbox sizing works in those two phases, and each of the three properties belongs to one of them.

  • flex-basis is the starting size, the book’s natural thickness along the main axis.
  • flex-grow decides who gets leftover space.
  • flex-shrink decides who gives up space when there is a deficit.

flex-basis: where each item starts

flex-basis takes a length, a percentage, or auto. With auto, the item uses its width (in a row) or height (in a column), and if that is also auto, its content size. Any other value overrides width:

.item { width: 100px; flex-basis: 300px; }   /* starts at 300px */

Because flex-basis works on whatever the main axis is, it works in both row and column containers, which width cannot. In practice width is often the more readable choice for a row, and the first demo in Kevin’s video is about the two being the same until something forces them apart.

flex-grow: sharing the leftover

After the bases are laid out, the leftover space is the container’s size minus the sum of the bases. flex-grow is a share count: each item gets grow / total-grow of that space, added on top of its basis.

.row { display: flex; width: 600px; }
.row > * { flex-basis: 100px; }
.a { flex-grow: 1; }   /* 100 + 300 * 1/6 = 150px */
.b { flex-grow: 2; }   /* 100 + 300 * 2/6 = 200px */
.c { flex-grow: 3; }   /* 100 + 300 * 3/6 = 250px */

These are the widths a browser produced for exactly that setup: 150, 200 and 250. The default flex-grow is 0, so by default items do not grow, and leftover space remains at the end.

Notice that grow shares extra space, not total space. Items with different bases and equal flex-grow end up with different widths. When you want equal widths, you need to zero out the bases, which is what flex: 1 does.

flex-shrink: taking back the overflow

When the bases add up to more than the container, flexbox shrinks items. The default flex-shrink is 1, so everyone shrinks. The detail that surprises people is that shrinking is weighted by basis: an item gives up space in proportion to its flex-shrink times its basis.

.row { display: flex; width: 200px; }
.big   { width: 300px; }   /* ends at 150px */
.small { width: 100px; }   /* ends at 50px */

The overflow is 200px. The split is 300:100, so the big item shrinks by 150px and the small one by 50px. That’s what a browser produced for this case. Set flex-shrink: 0 on an item you never want squeezed, like an avatar or an icon.

The flex shorthand

You will almost always write the shorthand, flex: grow shrink basis. Its common forms are worth knowing exactly:

Shorthand Expands to Meaning
(none) 0 1 auto default: no grow, can shrink, content size
flex: 1 1 1 0% share all space equally, ignore content size
flex: auto 1 1 auto start from content size, then share leftover
flex: none 0 0 auto fixed at its content size, rigid
flex: 2 2 1 0% twice the share of a flex: 1 sibling

The trap is that the single-number form sets the basis to 0% and not auto. A column with flex: 1 and a column with flex: 2 split the full width one third to two thirds, even if one has more text. Those expansions were read out of computed styles in a browser.

Why items refuse to shrink: min-width: auto

If you give an item flex: 1 and put a very long word or a wide table in it, it may push the row past the container. Flex items have min-width: auto, which resolves to the content’s minimum size. So the item stops shrinking at that point and overflows.

.text { flex: 1; min-width: 0; }   /* now it may shrink below its content */

In a test with a 200px row, an item holding one 70-character word reported 525px wide, and with min-width: 0 on it, the two flex: 1 items each came out at 100px. Adding min-width: 0 (or overflow: hidden, which also resets the automatic minimum) is the standard fix, and it’s the first thing to try when a flex layout overflows for no obvious reason.

What the video covers

Kevin Powell’s “How to take control of Flexbox” was published in March 2024 and runs about sixteen minutes. Its timestamps are: what we are starting with; understanding what happens when we use display: flex; what flex-shrink does; adding in flex-grow; and making things more consistent by defining the size. It builds a hotel-card design from a Dribbble shot and has a CodePen. I could not read a transcript, so beyond those chapters I don’t claim specifics. The arithmetic examples, the shorthand table and the min-width: auto discussion above are the prose’s own. The runner-up covers flex-basis against width.

Where this fits

This is the sizing half of flexbox. The previous lesson, flexbox fundamentals, covered the axes, and the next one, alignment and gap, covers where items sit once they have their sizes. The unit and min()/max() rules you may use for bases are in values, units and math functions.

Check yourself

  1. 01What's the difference between `flex: 1` and `flex: auto`?

    `flex: 1` is `1 1 0%`: every item starts from zero, so free space is shared equally and the items come out equal width regardless of content. `flex: auto` is `1 1 auto`: items start from their content or `width`, then share what is left, so a longer item stays wider.

  2. 02Items in a 200px flex row are 300px and 100px wide with default flex-shrink. How wide do they end up?

    150px and 50px. The 200px overflow is removed in proportion to each item's basis times its shrink factor: 300 versus 100 gives a 3:1 split of the 200px, so the large item gives up 150px and the small one 50px.

  3. 03You write `width: 100px; flex-basis: 300px` on an item in a row. How wide is it?

    300px. In a row, `flex-basis` overrides `width` for the item's starting size, unless it is `auto`. `width` is only used when `flex-basis` is `auto`.

  4. 04A text column in a flex row has `flex: 1` but a long URL pushes it wider than the container. How do you fix it?

    Add `min-width: 0` to the item. Flex items default to `min-width: auto`, which won't let them be smaller than their content. With `min-width: 0` the item can shrink, and you can wrap or truncate the text with `overflow-wrap`, `overflow: hidden` or `text-overflow`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark