Flex Shrink – Controlling How Items Compress
Quick Answer: The flex-shrink property controls how much a flex item shrinks when the items are too large to fit in the container. The default value is 1, so items shrink. Set flex-shrink: 0 to stop an item from shrinking, or a larger number to make it shrink more than the others.
S1. Why Do We Need Flex Shrink?
Imagine a page with a 250px sidebar and a wide content area. On a narrower screen, the two do not fit side by side. Something must give.
Should the sidebar get squeezed, so that its logo and menu look cramped? Or should the content area give up some width instead?
Flexbox decides this with flex-shrink. Let's first see what happens when we leave it alone.
S2. The Problem: Every Item Compresses by Default
Shrinking = reducing the size of flex items along the main axis when their combined size is larger than the container.
Imagine a container that is 900px wide. The sidebar is set to 250px, and the content area is set to 800px. Together they need 1,050px, which is 150px more than the container has.
Example 1: A sidebar and content area that do not fit
This example renders, but the sidebar is no longer 250px wide.
<!-- HTML -->
<div class="layout">
<aside class="sidebar">Menu</aside>
<main class="content">Lesson content</main>
</div>
/* CSS */
.layout { display: flex; width: 900px; }
.sidebar { width: 250px; }
.content { width: 800px; }
Expected output
Requested sizes: sidebar 250px + content 800px = 1050px Container: 900px Overflow: 150px Result with the default flex-shrink: 1 sidebar about 214px (shrunk by about 36px) content about 686px (shrunk by about 114px) The sidebar is narrower than the 250px that was requested.
The code runs, but the sidebar quietly loses width. Its logo and menu may look cramped, and the developer never asked for that.
What goes wrong?
- Unwanted shrinking: The sidebar becomes narrower than its set width.
- Squashed content: Logos, icons and buttons can be compressed.
- Surprising results: The width you wrote is not always the width you get.
- Harder testing: Each screen width gives different sizes.
- Poor clarity: The CSS does not say which item may shrink.
Now think about a website with many components, such as icon buttons, avatars and sidebars. Letting everything shrink equally becomes harder to control.
Remember: The problem is not shrinking itself. The real problem is letting every item shrink when only some of them should.
Animation 1: How default shrinking compresses every item
One rule, every item compressed
The red highlights show that every item gives up width, including the ones that should stay fixed.
The animation highlights the default rule applied to items that should stay fixed.
S3. The Solution: Decide Which Items May Shrink
Flexbox gives us a simpler design.
Flex shrink factor = a number that says how willing an item is to shrink when space is short.
Instead of letting everything compress, give each item the shrink factor that matches its role.
flex-shrink: 0means the item never shrinks below its flex basis.flex-shrink: 1means the item shrinks normally. This is the default.- A larger value, such as
2, makes the item shrink more than items with1.
Now the sidebar keeps its width, and the content area absorbs the shortage.
Example 2: A sidebar that does not shrink
Assumption: This example uses standard CSS supported by all modern browsers.
.layout { display: flex; width: 900px; }
.sidebar {
width: 250px;
flex-shrink: 0; /* never shrink */
}
.content {
width: 800px; /* free to shrink (default 1) */
}
Expected output
Requested sizes: sidebar 250px + content 800px = 1050px Container: 900px Overflow: 150px Result sidebar 250px (flex-shrink: 0, keeps its width) content 650px (absorbs the full 150px)
Notice the difference. The sidebar keeps the width we asked for, and only the content area gives up space.
Animation 2: The controlled shrinking design
One property, different roles
Each item gets the shrink value that matches its job.
The animation highlights each item and the shrink value that fits its role.
Compare both approaches
| Without flex-shrink control | With flex-shrink control |
|---|---|
| Every item shrinks when space runs out. | Only the chosen items shrink. |
| Fixed-looking widths are not guaranteed. | Important items keep their size. |
| Icons and avatars can be squashed. | Icons and avatars stay intact. |
| Sizes change in ways that are hard to predict. | The shortage goes where you decide. |
| Layout intent is hidden. | Layout intent is clear. |
The idea to remember
- flex-shrink = how much an item shrinks when the items are too large for the container.
- Negative free space = the shortage that appears when the items need more room than the container has.
- Flex basis = the starting size of an item before shrinking or growing.
- Shrink factor = the number given to
flex-shrink, with a default of 1. - Minimum size = the limit below which an item will not shrink.
S4. Step-by-Step Code Breakdown
1. Know when shrinking happens
Shrinking only happens when the combined size of the items is larger than the container's main size. If the items fit, flex-shrink has no visible effect.
2. Know the default value
Every flex item starts with flex-shrink: 1. A value of 0 turns shrinking off. Negative values are not valid.
3. Understand how the shortage is shared
The shortage is shared in proportion to each item's flex-shrink value multiplied by its starting size (its flex basis). This means that a bigger item gives up more pixels than a smaller one, even when both have the same shrink value.
Suppose three items are 300px wide each in a 600px container. Together they need 900px, so 300px must be removed.
Example 3: Three ways to share the shortage
.container { display: flex; width: 600px; }
.item { width: 300px; }
/* Case A: all items keep the default (1) */
/* Case B: only the first item is protected */
.item-1 { flex-shrink: 0; }
/* Case C: the second item shrinks twice as readily */
.item-2 { flex-shrink: 2; }
Expected output
Each item asks for 300px. Total 900px. Container 600px. Shortage 300px. Case A (1, 1, 1): 200px 200px 200px Case B (0, 1, 1): 300px 150px 150px Case C (1, 2, 1): 225px 150px 225px How case C is worked out: shrink weights = 1x300, 2x300, 1x300 = 300, 600, 300 (total 1200) item 1 gives up 300 x 300/1200 = 75px -> 225px item 2 gives up 300 x 600/1200 = 150px -> 150px item 3 gives up 300 x 300/1200 = 75px -> 225px
| Value | Behaviour |
|---|---|
1 |
The item shrinks normally. This is the default. |
0 |
The item does not shrink below its flex basis. |
2 |
The item takes a larger share of the shortage than items with 1. |
0.5 |
The item takes a smaller share of the shortage than items with 1. |
4. Protect icons, avatars and buttons
Small fixed elements, such as icons, should not be squeezed. Give them flex-shrink: 0, and let the flexible text item absorb the shortage.
Text has one more limit. A flex item will not shrink below its minimum content size by default, so a long unbroken title can force the row to overflow. Setting min-width: 0 removes that limit and lets the text item shrink and be truncated.
Example 4: A row with a fixed icon and a shrinking title
<!-- HTML -->
<div class="row">
<img class="icon" src="icon.png" alt="">
<span class="title">A very long lesson title that does not fit in one line</span>
</div>
/* CSS */
.row { display: flex; align-items: center; gap: 12px; width: 240px; }
.icon { width: 40px; height: 40px; flex-shrink: 0; }
.title {
min-width: 0;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Expected output
[icon 40px] A very long lesson tit... The icon keeps its 40px size (flex-shrink: 0). The title shrinks to the remaining width and ends with an ellipsis.
5. Remember how shrinking works with wrapping
When flex-wrap: wrap is used, items move onto a new line when they do not fit. They shrink only if a single item is still too large for a line on its own. If you want items to compress instead of wrapping, keep flex-wrap: nowrap.
Good to know: The shorthand flex sets flex-grow, flex-shrink and flex-basis together. It is covered in the next article.
S5. Architecture and Real-World Trade-Offs
Role-based shrinking keeps components predictable. It is useful in app layouts, toolbars, list rows and form rows.
How the design fits a website
Imagine a website with a sidebar layout, a toolbar and a list of rows with icons. Each area decides which items may give up space.
A focused shrinking flow
Each website area decides which item absorbs the shortage.
The animation traces each website area to the shrink values it uses.
Benefits for larger websites
- Maintainability: The shrink rule lives next to the item that needs it.
- Testing: Fixed elements keep their size at every screen width.
- Flexibility: One flexible item can absorb the shortage for the whole row.
- Clear ownership: Each item states how it behaves under pressure.
- Better visuals: Icons and avatars are not distorted.
Important trade-offs
- If every item has
flex-shrink: 0, nothing can absorb the shortage and the row overflows. - An item cannot shrink below its minimum content size unless you change
min-width(ormin-heightfor columns). - Larger items give up more pixels, because the share depends on the starting size too.
- Truncating text hides content, so use it only where that is acceptable.
Important: Shrinking only happens when the items need more room than the container has. If everything already fits, flex-shrink makes no visible difference.
S6. Common Mistakes and Best Practices
Mistake 1: Assuming the shortage is shared equally
Wrong: Expect two items with the same flex-shrink to lose the same number of pixels.
Why it fails: The share depends on the shrink value multiplied by the starting size, so the larger item loses more.
Better: Work out the shares in proportion to shrink value times starting size.
Rule: Shrink share = shrink factor x flex basis.
Mistake 2: Ignoring the minimum content size
Wrong: Expect a long text item to shrink below the width of its longest unbroken word.
Why it fails: By default, a flex item does not shrink below its minimum content size, so the row overflows.
Better: Add min-width: 0 and handle the text with overflow or wrapping.
Rule: Remove the minimum limit when the item must shrink freely.
Mistake 3: Believing a set width guarantees the size
Wrong: Write width: 250px on a sidebar and expect exactly 250px.
Why it fails: In a flex container, the width acts as the starting size, and the item can still shrink.
Better: Add flex-shrink: 0 to items that must keep their size.
Rule: A set width is a starting point, not a guarantee.
Mistake 4: Expecting shrinking when wrapping is on
Wrong: Use flex-wrap: wrap and expect items to compress before moving to a new line.
Why it fails: With wrapping, items that do not fit move to the next line instead of shrinking.
Better: Use nowrap when you want items to compress, and wrap when you want them to flow.
Rule: Decide whether items should compress or wrap.
Mistake 5: Setting flex-shrink: 0 on every item
Wrong: Protect every item from shrinking.
Why it fails: No item can absorb the shortage, so the row overflows the container.
Better: Leave at least one flexible item that can shrink.
Rule: Every row needs one item that can give way.
S7. How This Comes Up in Interviews
Question 1: What does flex-shrink do?
What the interviewer wants: They want to know whether you understand how flex items behave when space is short.
Strong answer:
"flex-shrink controls how much a flex item shrinks when the items are too large for the container. The default is 1, so items shrink. A value of 0 stops the item from shrinking below its flex basis."
Question 2: How is the shortage shared between items?
Strong answer:
"In proportion to each item's flex-shrink value multiplied by its flex basis. So with equal shrink values, the larger item gives up more pixels."
Question 3: Why is my logo getting squashed in a flex row?
Strong answer:
"The logo has the default flex-shrink of 1, so it shrinks when the row is too narrow. I would set flex-shrink: 0 on the logo and let a text item absorb the shortage."
Question 4: Why does a long text item still overflow even though flex-shrink is 1?
Strong answer:
"A flex item will not shrink below its minimum content size by default. Setting min-width: 0, together with overflow handling, lets the item shrink below that size."
What should different experience levels explain?
- Junior developer: Explain the default value of 1 and how 0 prevents shrinking.
- Senior developer: Explain the proportional calculation and the minimum content size.
- Technical architect: Explain how shrink conventions keep icons, avatars and sidebars consistent across a design system.
S8. Hands-On Practice
Beginner: Compare the default and zero
Create a 600px container with three items of 300px each. Observe them with the default shrink, then set flex-shrink: 0 on the first item.
Expected result: With the default, each item becomes 200px. With 0 on the first item, it stays 300px and the others become 150px each.
Intermediate: Protect a sidebar
Build a layout with a 250px sidebar and a wide content area in a narrower container. Use flex-shrink: 0 so the sidebar keeps its width.
Hint: Resize the browser and watch which item changes.
Advanced: Build a truncating row
Create a row with an icon and a long title. Keep the icon at a fixed size, and truncate the title with an ellipsis. Then remove min-width: 0 and observe what changes.
Expected result: With min-width: 0, the title is truncated. Without it, the long text can force the row to overflow.
S9. Next Steps and Community
Previous article: Flex Grow – Controlling How Items Expand.
Next article: Flex Basis and Flex Shorthand – Understanding Item Sizing.
Continue the series by learning how flex-basis sets the starting size of an item, and how the flex shorthand combines grow, shrink and basis in one line.
Try the examples in your own HTML and CSS project. Resize the window, change the shrink values, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Decide which items give way, and let the rest keep their shape.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.