CSS flex-shrink Property
Subject: css
CSS flex-shrink Property
The flex-shrink property defines how much a flex item should shrink compared to other items when there isn’t enough space in the flex container.
It is part of the Flexbox model and works alongside flex-grow and flex-basis.
Why Use flex-shrink?
- Prevent important elements from shrinking
- Control how different elements shrink proportionally
- Preserve content readability in small viewports
- Avoid layout breaking on smaller screens
Syntax
<number>is a non-negative number (default is1)0prevents the item from shrinking- Higher values allow items to shrink more
Example: Controlling Shrink Behavior
Explanation
- The container width is
400px - Each item has a
flex-basisof300px(total600px) .item1shrinks due toflex-shrink: 1.item2does not shrink due toflex-shrink: 0
Best Practices
- Use
flex-shrink: 0on logos, icons, or important text - Use higher values for less important or decorative elements
- Always test layout shrinkage across various screen widths
- Combine with
flex-basisorwidthfor expected results
Common Use Cases
- Locking image/logo widths in navbars
- Preventing buttons or labels from shrinking
- Allowing only text boxes to adjust on smaller screens
- Maintaining fixed-width sidebars in responsive layouts
Key Takeaways
flex-shrinkcontrols shrink ratio when space is limited0disables shrinking for that item- Higher numbers shrink more than lower ones
- Use with
flex-basisorwidthfor controlled layouts - Ideal for adaptive, responsive, and content-safe designs
Advertisement Slot 1
Advertisement Slot 2