🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Flex-shrink

AI & DATA SCIENCE // flex-shrink

The flex-shrink property determines how much a flex item should shrink relative to its siblings when the container doesn't have enough space to fit every item at its natural or specified size.

Syntax

flex-shrink: number;

Deep Dive Course

flex-shrink defaults to 1, meaning items shrink proportionally by default when they collectively overflow their container's available space, and setting it to 0 on a specific item prevents that particular item from shrinking at all, forcing its siblings to absorb more of the necessary shrinkage instead, or causing overflow if they can't shrink enough on their own. Like flex-grow, shrinking is distributed proportionally among items with a nonzero flex-shrink value, though the actual calculation also factors in each item's own base size, meaning a larger item with the same flex-shrink value as a smaller one shrinks by a proportionally larger absolute amount.

1Understanding Flex-shrink

flex-shrink defaults to 1, meaning items shrink proportionally by default when they collectively overflow their container's available space, and setting it to 0 on a specific item prevents that particular item from shrinking at all, forcing its siblings to absorb more of the necessary shrinkage instead, or causing overflow if they can't shrink enough on their own. Like flex-grow, shrinking is distributed proportionally among items with a nonzero flex-shrink value, though the actual calculation also factors in each item's own base size, meaning a larger item with the same flex-shrink value as a smaller one shrinks by a proportionally larger absolute amount.

💡

Set flex-shrink: 0 on an item that should never shrink below its natural or specified size, like a fixed-size icon or logo, letting its more flexible siblings absorb any necessary shrinkage instead when space runs short.

editor.html
.logo {
  flex-shrink: 0;
  width: 50px;
}

/* sibling nav links left at default flex-shrink: 1 */
localhost:3000

2Practical Example

Here is a real-world application of Flex-shrink showing how it is used in production CSS code.

editor.html
.item {
  flex-shrink: 1;
  flex-basis: 100px;
}

/* container narrows, forcing items to shrink below their 100px basis */
localhost:3000

3Best Practices

Follow these guidelines when working with Flex-shrink:

1. Set flex-shrink: 0 on items that should maintain a fixed, unshrinking size, like an icon or logo, when their siblings are more free to shrink instead

2. Understand that flex-shrink's default value of 1 means items shrink proportionally by default whenever they collectively overflow the container, which is often the desired behavior without any explicit configuration needed

3. Combine flex-shrink: 0 with an explicit flex-basis or width on an item to fully lock in a fixed, non-shrinking, non-growing size for it

⚠️

Tip: Set flex-shrink: 0 on an item that should never shrink below its natural or specified size, like a fixed-size icon or logo, letting its more flexible siblings absorb any necessary shrinkage instead when space runs short.

editor.html
.logo {
  flex-shrink: 0;
  width: 50px;
}

/* sibling nav links left at default flex-shrink: 1 */
localhost:3000

Examples

Example 01Basic Usage
.logo {
  flex-shrink: 0;
  width: 50px;
}

/* sibling nav links left at default flex-shrink: 1 */
Example 02Advanced Example
.item {
  flex-shrink: 1;
  flex-basis: 100px;
}

/* container narrows, forcing items to shrink below their 100px basis */

Best Practices

  • Set flex-shrink: 0 on items that should maintain a fixed, unshrinking size, like an icon or logo, when their siblings are more free to shrink instead
  • Understand that flex-shrink's default value of 1 means items shrink proportionally by default whenever they collectively overflow the container, which is often the desired behavior without any explicit configuration needed
  • Combine flex-shrink: 0 with an explicit flex-basis or width on an item to fully lock in a fixed, non-shrinking, non-growing size for it

Interview Question

Why does an item with flex-shrink: 0 completely refuse to shrink at all, even if every other item in the container has already shrunk down as much as it possibly can and the container is still overflowing?

Hint: Think about what flex-shrink: 0 specifically communicates to the flex layout algorithm about that particular item's willingness to participate in the shrinking process at all.

flex-shrink's value determines an item's proportional share of the total amount of shrinking that needs to happen when a container's items collectively don't fit, and a value of 0 specifically communicates that this particular item should receive exactly zero share of that shrinking, opting it out of the shrinking process entirely, completely independent of whatever difficulty its siblings might be having accommodating the available space. This means even in a genuinely tight situation where every other item in the container has already shrunk down to its absolute minimum possible size and the container is still overflowing because there simply isn't enough combined space for everything at its current sizes, the flex-shrink: 0 item still holds firmly at its full natural or explicitly specified size, since it was never eligible to shrink at all under any circumstance, and any remaining, unresolvable overflow is left to spill outside the container rather than being resolved by making that particular protected item any smaller.

Exercises

MediumPractice using Flex-shrink in a real scenario.
View Solution
.logo {
  flex-shrink: 0;
  width: 50px;
}

/* sibling nav links left at default flex-shrink: 1 */

Frequently Asked Questions

Why does an item with flex-shrink: 0 completely refuse to shrink at all, even if every other item in the container has already shrunk down as much as it possibly can and the container is still overflowing?

flex-shrink's value determines an item's proportional share of the total amount of shrinking that needs to happen when a container's items collectively don't fit, and a value of 0 specifically communicates that this particular item should receive exactly zero share of that shrinking, opting it out of the shrinking process entirely, completely independent of whatever difficulty its siblings might be having accommodating the available space. This means even in a genuinely tight situation where every other item in the container has already shrunk down to its absolute minimum possible size and the container is still overflowing because there simply isn't enough combined space for everything at its current sizes, the flex-shrink: 0 item still holds firmly at its full natural or explicitly specified size, since it was never eligible to shrink at all under any circumstance, and any remaining, unresolvable overflow is left to spill outside the container rather than being resolved by making that particular protected item any smaller.

Related Functions

Flex-growFlex-basisFlex-wrap