liveBook · Manning

Shrink

As explained in CSS in Depth, Second Edition

The CSS flex-shrink property specifies a flex item's shrink factor when the flex items' initial sizes exceed the available space in the flex container.

The CSS flex-shrink property controls how much a flex item contracts when the items’ initial main sizes exceed the available space. A value of 0 prevents shrinking; positive values allow it. Larger values shrink more, while the item’s initial size also affects the result. section 4.2.3

Fitting two columns around a gap

Suppose a main column targets 66.67% and a sidebar targets 33.33%, with a 1.5em gap between them. Their percentages already consume the container’s full width, so the gap causes overflow. Giving both columns flex-shrink: 1 lets the browser subtract space from each column until the layout fits. This provides an alternative to sizing the columns with flex-grow, though padding can produce slightly different final widths. section 4.2.3

Use flex-shrink with these practical distinctions:

  • Set flex-shrink: 0 when an item must retain its initial size, accepting possible overflow. section 4.2.3
  • Use higher shrink factors to make selected items surrender proportionally more space. section 4.2.3
  • Remember that flex-shrink is one part of flex: flex-grow, flex-shrink, and flex-basis jointly determine item sizing. section 4.2
  • In a vertical flexbox, shrinking affects height only when another constraint gives the container a specific height. section 4.3.1
Figure 4.12 Flex items can have an initial size exceeding that of the flex container.
figure

Inspect the overflow scenario that triggers flex-item shrinking before space is redistributed. section 4.2.3

How CSS in Depth, Second Edition covers it

How deep it goes
Explains zero and positive factors, proportional contraction, initial-size effects, gaps, padding, and column-direction constraints.
Examples
Demonstrates two percentage-based columns separated by a gap and nested tiles in a vertical flexbox.
What it assumes
Assumes flex containers, flex items, main axes, gaps, and flex shorthand while explaining the three sizing components.
What you can do after
Build it from scratch by choosing shrink factors for columns and predicting when constrained flex items will contract.
How it connects
Connects flex-shrink with flex-grow, flex-basis, flex shorthand, flexbox, nested flexboxes, and CSS Grid.

Also explained in

Compare how 4 books explain shrink →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.