Sub topic 10
Handling overflow with flex-shrink
This chapter assumes you know how to create a flex container, how the main axis and cross axis work, and how flex-basis and flex-grow set an item's starting size and its share of free space. You should also be comfortable with the box model and with px, rem and percentage units.
What flex-shrink does
flex-grow handles the case where the items are smaller than the container. flex-shrink handles the opposite case: the items' combined hypothetical sizes are larger than the container's main size, so they would overflow.
flex-shrink is a number on each flex item. It says how much of the overflow that item absorbs, relative to the other items. The initial value is 1, so by default every item can shrink. A value of 0 means the item refuses to shrink below its flex-basis. The value cannot be negative.
Because the default is 1, you often get shrinking without writing any flex properties. A row of items with width: 300px each in a 600px container with three items does not overflow. The items shrink to about 200px each.
.row {
display: flex;
width: 600px;
}
.row > div {
width: 300px; /* flex-basis is auto, so it falls back to width */
}How the shrinking is calculated
The browser does not shrink items by an equal amount. It weights each item's shrink by its flex-shrink value multiplied by its flex-basis. This is called the scaled shrink factor. Larger items give up more pixels than smaller ones, so every item loses roughly the same proportion of its size.
The steps are:
- Add up the flex-basis sizes of all items (plus margins, borders and padding, and any gaps).
- Subtract the container's main size. The result is the negative free space, the amount of overflow.
- For each item, compute
flex-shrink × flex-basis. This is its scaled shrink factor. - Distribute the overflow across the items in proportion to their scaled shrink factors.
Here is a worked example. The container is 500px wide, and the three items have bases of 200px, 200px and 300px. All use the default flex-shrink: 1.
- Total basis: 700px. Overflow: 200px.
- Scaled shrink factors: 200, 200 and 300. Their sum is 700.
- Item 1 shrinks by 200 × (200 / 700) ≈ 57px, to about 143px.
- Item 2 also shrinks to about 143px.
- Item 3 shrinks by 200 × (300 / 700) ≈ 86px, to about 214px.
Each item ends up at about 71% of its original size. The final sizes add up to exactly 500px.
Changing the shrink weights
Setting different flex-shrink values changes the weights. In this example, the sidebar should hold its width and the content should absorb all the squeeze.
.layout {
display: flex;
width: 600px;
}
.sidebar {
flex: 0 0 250px; /* grow 0, shrink 0, basis 250px */
}
.content {
flex: 1 1 500px; /* basis 500px, may shrink */
}The total basis is 750px and the container is 600px. The sidebar has a shrink factor of 0, so it stays at 250px. The content absorbs the full 150px of overflow and ends at 350px.
A larger value makes an item shrink faster. With bases of 200px each and flex-shrink values of 1 and 3, the second item has three times the scaled shrink factor, so it gives up three times as many pixels as the first.
.a { flex: 0 1 200px; }
.b { flex: 0 3 200px; }
/* In a 300px container: overflow is 100px.
.a loses 25px (ends at 175px). .b loses 75px (ends at 125px). */Shrinking stops at the minimum size
Shrinking has a floor. An item cannot shrink below its min-width (or min-height in a column). With min-width: auto, which is the default for flex items, the floor is the item's min-content size. For text, that is roughly the width of the longest unbreakable word. For an image, it is its intrinsic width.
When an item reaches its floor, the browser freezes it at that size and redistributes the remaining overflow among the items that can still shrink. If all items are frozen and the content still does not fit, the row overflows the container.
This is why long words, URLs, <pre> blocks and wide images sometimes refuse to shrink. The fix, setting min-width: 0 on the item, is covered in its own chapter later in this tutorial.
.item {
flex: 1 1 0;
min-width: 0; /* allow shrinking below min-content */
}Common shrink patterns
Preventing an item from shrinking
Fixed-size elements such as icons, avatars and buttons look squashed when a row gets tight. Set flex-shrink: 0 on them.
.avatar {
flex-shrink: 0;
width: 48px;
height: 48px;
}Without it, a long name next to the avatar can squeeze the avatar into an oval.
Letting wrapping replace shrinking
If the container uses flex-wrap: wrap, items move to a new line when they do not fit at their flex-basis. Shrinking only happens within a line, after the browser has decided which items belong on it. A wrapping row therefore rarely needs to shrink items, except when a single item is wider than the container.
Shrinking in a column
In flex-direction: column, the main axis is vertical. flex-shrink then affects heights, but only if the container has a constrained height. A column container with height: auto grows to fit its content, so there is no overflow to absorb.
Common mistakes
- Expecting equal shrinking. Shrinking is proportional to basis, so a large item loses more pixels than a small one.
- Using
widthand expecting it to be a hard value. In a flex row,widthacts as the basis (whenflex-basisisauto). It can still shrink. Useflex-shrink: 0, ormin-width, to hold a size. - Setting
flex-shrink: 0on everything. The row then overflows on small screens and produces horizontal scrolling. - Forgetting that
flex: 1sets the basis to 0. Withflex: 1, the basis is0%, so there is no overflow to shrink in the first place. Items share the space throughflex-growinstead. - Ignoring gaps and margins. These count toward the total size, but they do not shrink. Items must absorb them.
Reading shrinking in DevTools
Browser developer tools show computed sizes, which makes shrinking visible. In Chrome, Firefox and Safari, select the flex container and open the layout inspector. Compare an item's computed width with its declared width or flex-basis. If they differ, the item shrank. If the computed width equals min-content, the item hit its floor.
Summary of the rules
| Situation | Result |
|---|---|
| Items fit in the container | `flex-shrink` has no effect |
| Items overflow, default `flex-shrink: 1` | Each item shrinks in proportion to its basis |
| Item has `flex-shrink: 0` | Item keeps its basis size, others absorb the overflow |
| Item reaches its min-content floor | Item is frozen, others keep shrinking |
| All items frozen and still too wide | The container overflows |
Exercise
A flex row is 400px wide and has two items. Item A has flex: 0 1 300px and item B has flex: 0 1 100px. Then you change B to flex: 0 0 100px. What are the final widths of A and B? Ignore padding, borders, margins and gaps.
Show answer
Item B can no longer shrink, so it stays at 100px. The total basis is 400px, which equals the container, so there is no overflow. Item A stays at 300px. If B had kept flex-shrink: 1, there would also have been no overflow, so both items would have kept their bases. To see shrinking, the container would need to be narrower, for example 300px. Then the overflow of 100px falls entirely on A, which ends at 200px, and B stays at 100px.
Exercise
A profile card shows a 48px avatar next to a long username. On narrow screens the avatar turns into an oval. Which property fixes this, and on which element?
Show answer
Set flex-shrink: 0 on the avatar. The username would otherwise force the row to overflow, and the avatar, with the default flex-shrink: 1, would give up width. With flex-shrink: 0, it keeps its 48px width. Add min-width: 0 to the username if it must truncate.