π Introduction
The flex-shrink property controls how much a flex item will shrink relative to other items when there isnβt enough space in the flex container. Itβs one of the 3 core item-sizing properties in Flexbox:flex-grow, flex-shrink, and flex-basis. Understanding flex-shrink helps you prevent unwanted element squishing in responsive layouts. π§ β¨
π― What Does flex-shrink Do?
- Controls how much a flex item shrinks when space is limited
- Higher value β shrinks more
- 0 means "never shrink"
- Used for maintaining stable UI layouts
β¨ Syntax
Syntax
selector {
flex-shrink: <number>; /* default = 1 */
}π§© Accepted Values
1οΈβ£ flex-shrink: 1 (default)
The item can shrink when necessary.
Default
flex-shrink: 1;2οΈβ£ flex-shrink: 0
The item will NOT shrink β keeps its width at all costs.
No Shrinking
flex-shrink: 0;Note
3οΈβ£ Higher Values (2,3,...)
Items with larger flex-shrink values shrink more compared to items with smaller values.
Shrink More
flex-shrink: 3;π₯ Practical Examples
1. Prevent a Logo From Shrinking
Logo Protection
.logo {
flex-shrink: 0;
}2. Make One Item Shrink More Than Others
Unequal Shrinking
.item1 { flex-shrink: 1; }
.item2 { flex-shrink: 3; }3. Prevent Buttons From Squishing
Code Snippet
.btn {
flex-shrink: 0;
}π How flex-shrink Works With Multiple Items
When items overflow their container, the browser subtracts width from items. It distributes shrinking proportional to the flex-shrink values.
| Item | flex-shrink | Shrink Priority |
|---|---|---|
| Item A | 1 | Low |
| Item B | 2 | Medium |
| Item C | 3 | High |
π§ͺ Visual Example
Full Example
.container {
display: flex;
width: 300px;
gap: 10px;
}
.item {
flex-basis: 150px;
padding: 10px;
background: #ddd;
}
.item:nth-child(1) { flex-shrink: 1; }
.item:nth-child(2) { flex-shrink: 0; }
.item:nth-child(3) { flex-shrink: 3; }Further details can be found at: https://dummyimage.com/900x250/eeeeee/000000&text=flex-shrink+demo
π§ Tips & Best Practices
- Use flex-shrink: 0 for elements that must stay readable
- Avoid mixing large
flex-basiswith high shrink values - Useful for responsive navigation bars
- Combine with
flex-growandflex-basisfor complete layout control
π Conclusion
The flex-shrink property determines how flex items react when there isnβt enough space. With proper use, you can prevent important UI elements from collapsing and control which items should give up space first. Mastering flex-grow, flex-shrink, andflex-basis gives you complete command over Flexbox sizing. π