HomeGuideLabGlossary

Shrinking flex items

Shrinking happens completely separately in each flex line.

I know nothing about the size of I know nothing about the size of 1221

By default, you’ll only have one flex line anyway because flex containers don’t allow their flex items to wrap.

The point of shrinking is to prevent flex items from overflowing the flex container.

By default, flex items agree to shrink because the initial value of flex-shrink is 1.

Prevent excessive shrinking if you want a scrollable flex container

If you want your flex items to be scrollable within the flex container, that means some flex items will have to overflow the flex container. You can achieve that by preventing the flex items from shrinking altogether by setting flex-shrink: 0.

123flex-shrink: 0

Alternatively, you could let the flex items shrink down to their minimum size. By default, a content-based minimum size will be used for the flex items. If that’s not what you want, you can set an explicit minimum size beyond which the flex items shouldn’t shrink.

123flex-shrink: 1min-inline-size: 80px

Shrinking happens in rounds.

Imagine you have two flex items, and together they overflow the flex container.

12130 pixelsof overflow

The flex container asks them to shrink, and they agree. They both have the same flex base size and flex-shrink value, so they’re asked to shrink by the same amount.

12130 pixelsof overflowBoth of you, please shrink by 65 pixelsflex-shrink: 1flex-shrink: 1

But because the first flex item has a large minimum size, it only agrees to shrink down to that size.

1240 pixelsof overflowBoth of you, please shrink by 65 pixelsI reached my min size, I’ll only shrink by 25 pixelsDone, shrunk by 65 pixels!flex-shrink: 1min-inline-size: 150pxflex-shrink: 1

This means the flex container is still overflowing. The first flex item is done shrinking, so the flex container will have to do another round of shrinking and ask the second flex item to shrink even more.

12Hey , please shrink again, this time by 40 pixelsDone, shrunk by 40 pixels!flex-shrink: 1min-inline-size: 150pxflex-shrink: 12

Shrinking isn’t simply proportional to flex-shrink.

Flexbox layout will try to preserve the ratio between the flex base sizes the flex items asked for.

For example, you could have a sidebar box that wants to be 300 pixels wide, and a content area box that wants to be 2000 pixels wide. They both have flex-shrink: 1.

SidebarContentflex-shrink: 1flex-shrink: 1

If the flex container is overflowing by 400 pixels, it will have to ask the flex container to shrink by that amount.

400 pixelsof overflowSidebarContentflex-shrink: 1flex-shrink: 1

If the amount of shrinking was simply proportionaly to flex-shrink, then both boxes would be asked to shrink by the same amount of 200 pixels.

That would make the sidebar way too tiny, and not what we intended.

SidebarContentBoth of you, please shrink by 200 pixels

So instead, Flexbox layout will take into consideration those flex base sizes of 300 and 2000 pixels, too. That way, the size of the sidebar and the content area after shrinking will stay proportional to those original intended sizes.

SidebarContentSidebar, please shrink by 52 pixelsContent, please shrink by 348 pixels