Sub topic 10
Handling overflow with flex-shrink
This chapter assumes you know how to create a flex container, how flex-basis sets a starting size, and how flex-grow shares extra space. Here you will see what happens in the opposite case, when items are too wide for the container.
What flex-shrink does
flex-shrink is a number on each flex item. It sets how much that item gives up when the items' starting sizes add up to more than the container's main-axis size. The default is 1, so items shrink by default. A value of 0 stops an item from shrinking.
The browser does not simply divide the shortage equally. It weights each item by its flex-shrink value multiplied by its flex-basis. A large item loses more pixels than a small one, and both lose the same proportion of their size.
.row {
display: flex;
width: 600px;
}
.a { flex: 0 1 400px; }
.b { flex: 0 1 200px; }
.c { flex: 0 1 200px; }The bases add up to 800px, so the row is 200px short. Item .a makes up half of the total weight, so it gives up 100px and ends at 300px. Items .b and .c each give up 50px and end at 150px. Every item lost one quarter of its base size.
Changing the weights
A higher flex-shrink makes an item give up more. A value of 0 protects an item completely, which suits things like logos and icons.
.row {
display: flex;
width: 600px;
}
.logo {
flex: 0 0 120px;
}
.content {
flex: 1 1 300px;
}
.sidebar {
flex: 0 3 300px;
}The logo keeps 120px. The bases total 720px, so the shortage is 120px. The sidebar's weight is 3 × 300 = 900, and the content's weight is 1 × 300 = 300. The sidebar therefore takes three quarters of the shortage (90px) and the content takes one quarter (30px). The final widths are 120px, 270px and 210px.
Limits on shrinking
Shrinking stops when an item reaches its minimum size. Three things set that floor:
- An explicit
min-width(ormin-heighton a column). - The item's automatic minimum, which is the size of its content, such as the longest unbreakable word or an image's width.
- A
flex-shrinkof0.
When every item is at its floor and the content still does not fit, the items overflow the container. The next chapters cover the automatic minimum and how min-width: 0 changes it.
Common mistakes
- Setting
widthon an item and expecting it to be fixed. Under the defaultflex-shrink: 1, the width acts as a flexible starting size. Useflex: noneorflex-shrink: 0to fix it. - Assuming equal shrink for equal
flex-shrink. The weights also depend on each item'sflex-basis. - Using
flex-shrinkon a non-flex child. The property is ignored unless the parent hasdisplay: flexordisplay: inline-flex.
Exercise
A 500px wide flex row contains two items. Item A has flex: 0 1 400px and item B has flex: 0 1 200px. What is the final width of each item?
Show answer
The bases total 600px, so the shortage is 100px. The weights are 400 and 200, so A loses 400/600 × 100 ≈ 66.7px and B loses 200/600 × 100 ≈ 33.3px. A ends at about 333.3px and B at about 166.7px.
Exercise
A logo in a navigation bar gets squeezed when the window narrows. Which one-line declaration on the logo stops this, and what is the trade-off?
Show answer
Use flex-shrink: 0 (or flex: none). The logo keeps its size, but if the other items cannot shrink enough, the row may overflow the container.