CSS flex-shrink Property
The flex-shrink property controls how much a flex item can shrink relative to other flex items when there is insufficient space along the main axis of a flex container.
It accepts a non-negative number called a shrink factor. Flexbox combines this factor with the item's flex base size to determine how negative free space is distributed. The property applies to flex items, not the flex container itself.
flex-shrink Syntax
The flex-shrink property accepts a single non-negative number. The number determines the item's shrink factor when negative free space must be distributed.
Basic Syntax
selector {
flex-shrink: 1;
}
Common Values
.item-one {
flex-shrink: 0;
}
.item-two {
flex-shrink: 1;
}
.item-three {
flex-shrink: 2;
}
.item-four {
flex-shrink: 0.5;
}
CSS-Wide Keywords
.item {
flex-shrink: inherit;
}
.item {
flex-shrink: initial;
}
.item {
flex-shrink: unset;
}
.item {
flex-shrink: revert;
}
.item {
flex-shrink: revert-layer;
}
The initial value is 1, which allows flex items to shrink when necessary, subject to their minimum sizes and other constraints.
Negative numbers, percentages, and length units such as px are not valid flex-shrink values.
Property Values
The value of flex-shrink is a non-negative number. Whole numbers and decimal values are permitted.
| Value | Description |
|---|---|
0 |
Prevents the item from shrinking through the flex shrink algorithm. |
1 |
Allows the item to shrink when necessary. This is the initial value. |
2 |
Assigns twice the shrink factor of an item using 1. |
0.5 |
Assigns half the shrink factor of an item using 1. |
<number> |
Any finite, non-negative number representing a shrink factor. |
CSS-Wide Values
| Value | Description |
|---|---|
inherit |
Uses the computed value of the parent element. |
initial |
Restores the initial value, 1. |
unset |
Restores 1 because the property is not inherited. |
revert |
Reverts the property according to the CSS cascade origins. |
revert-layer |
Reverts the property according to the CSS cascade layers. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 1 |
| Inherited | No |
| Applies to | Flex items |
| Computed value | As specified |
| Animation type | Number |
| Percentages | Not applicable |
Numeric shrink factors can interpolate during CSS transitions and animations. The resulting item size also depends on the flex layout algorithm.
How It Works
Flexbox determines the initial main sizes of the items. When their combined sizes exceed the available space on a flex line, the flex shrink algorithm can reduce those sizes.
Step 1: Create a Flex Container
.container {
display: flex;
width: 400px;
}
Step 2: Define the Item Bases
.item {
flex-basis: 200px;
}
Three items with a 200px basis each require a combined 600px before accounting for other sizing constraints.
Step 3: Allow Shrinking
.item {
flex-shrink: 1;
min-width: 0;
}
The items can shrink to help fit within the 400px container. In this example, min-width: 0 removes the automatic minimum width restriction.
Step 4: Assign Different Shrink Factors
.item-one {
flex-shrink: 1;
}
.item-two {
flex-shrink: 2;
}
.item-three {
flex-shrink: 1;
}
When the items have equal flex bases and no limiting constraints, the middle item loses twice as much space as either outer item.
Important Distinction
The shrink factor alone does not determine the amount of space removed. Flexbox scales each shrink factor by the item's flex base size.
Shrink Factors
A shrink factor determines an item's relative participation in reducing sizes when negative free space exists.
Equal Shrink Factors
.item-one {
flex-shrink: 1;
}
.item-two {
flex-shrink: 1;
}
.item-three {
flex-shrink: 1;
}
With equal flex bases and no limiting constraints, the items shrink by equal amounts.
Unequal Shrink Factors
.item-one {
flex-shrink: 1;
}
.item-two {
flex-shrink: 3;
}
.item-three {
flex-shrink: 1;
}
If the items have equal flex bases, the middle item receives three times the shrink allocation of either outer item, provided no minimum sizes interfere.
Decimal Shrink Factors
.item-one {
flex-shrink: 0.5;
}
.item-two {
flex-shrink: 1;
}
.item-three {
flex-shrink: 0.5;
}
Decimal factors are valid. They participate in the same scaled shrink calculation as whole numbers.
Shrink Factors Below One
.item-one {
flex-shrink: 0.2;
}
.item-two {
flex-shrink: 0.3;
}
When the sum of the shrink factors is below 1, Flexbox can distribute only a fraction of the negative free space, leaving some overflow.
Important Consideration
Unlike growth factors, shrink factors are multiplied by their corresponding flex base sizes. Two items with the same shrink factor can therefore lose different amounts of space.
Distributing Negative Free Space
Negative free space occurs when the items' required main-axis sizes exceed the available space on a flex line.
Example Calculation
Consider a 400px flex container containing three items, each with a flex basis of 200px. Assume there are no gaps, margins, borders, padding, or limiting size constraints.
| Measurement | Value |
|---|---|
| Container width | 400px |
| Combined flex bases | 600px |
| Negative free space | 200px |
| Shrink factors | 1:2:1 |
Calculate the Scaled Shrink Factors
Each shrink factor is multiplied by its flex base size.
Item 1: 1 × 200px = 200
Item 2: 2 × 200px = 400
Item 3: 1 × 200px = 200
Total scaled shrink factor = 800
Calculate the Space Removed
Item 1: 200px × (200 / 800) = 50px
Item 2: 200px × (400 / 800) = 100px
Item 3: 200px × (200 / 800) = 50px
Final Item Sizes
| Item | Flex Basis | Space Removed | Final Size |
|---|---|---|---|
| Item 1 | 200px | 50px | 150px |
| Item 2 | 200px | 100px | 100px |
| Item 3 | 200px | 50px | 150px |
CSS Example
.container {
display: flex;
width: 400px;
}
.item {
flex-basis: 200px;
flex-grow: 0;
min-width: 0;
}
.item-one {
flex-shrink: 1;
}
.item-two {
flex-shrink: 2;
}
.item-three {
flex-shrink: 1;
}
This simplified calculation illustrates the role of scaled shrink factors. Actual layout may require additional adjustments when minimum sizes, maximum sizes, or other constraints apply.
flex-shrink: 0
The value 0 prevents a flex item from shrinking through the flex shrink algorithm.
Syntax
.item {
flex-shrink: 0;
}
Example
.container {
display: flex;
gap: 15px;
}
.icon {
flex: 0 0 48px;
}
.content {
flex: 1 1 auto;
min-width: 0;
}
How It Works
The icon retains its 48px flex basis while the content area can shrink to accommodate the available space.
Preventing Shrinking Does Not Prevent Overflow
.item {
flex-basis: 300px;
flex-shrink: 0;
}
If the container is narrower than the item's required size, the item may overflow because it cannot shrink through flex sizing.
Important Consideration
Use flex-shrink: 0 selectively for elements that need to preserve their main-axis size, such as icons, fixed-size controls, or specific layout components.
flex-shrink: 1
The value 1 is the initial shrink factor. It allows a flex item to become smaller when the container has insufficient main-axis space.
Syntax
.item {
flex-shrink: 1;
}
Example
.container {
display: flex;
width: 400px;
}
.item {
flex-basis: 200px;
flex-shrink: 1;
min-width: 0;
}
How It Works
When three equal-basis items occupy a 400px container, they can shrink to approximately 133.33px each, assuming no other sizing constraints.
Default Behavior
Because the initial value is 1, explicitly declaring flex-shrink: 1 is often unnecessary unless another rule has changed the value.
Important Consideration
The default automatic minimum size of a flex item may prevent it from shrinking enough to fit. A shrink factor of 1 does not override minimum size constraints.
Unequal Shrinking
Different shrink factors allow some items to surrender more space than others when a flex line is too large for its container.
Equal Bases with Different Factors
.container {
display: flex;
width: 400px;
}
.item {
flex-basis: 200px;
min-width: 0;
}
.item-one {
flex-shrink: 1;
}
.item-two {
flex-shrink: 2;
}
.item-three {
flex-shrink: 1;
}
Because the flex bases are equal, the middle item loses twice as much space as either outer item when constraints permit.
Different Bases with Equal Factors
.item-one {
flex-basis: 100px;
flex-shrink: 1;
}
.item-two {
flex-basis: 200px;
flex-shrink: 1;
}
The second item has twice the flex base size. Its scaled shrink factor is therefore twice that of the first item, even though both use flex-shrink: 1.
Why Scaling Matters
Scaling shrink factors by flex bases helps avoid taking the same absolute amount of space from a small item and a much larger item.
Important Consideration
A larger shrink factor does not always produce a smaller final item. The starting flex bases and applicable minimum sizes must also be considered.
Relationship with flex-basis
The flex-basis property establishes an item's initial main size before flexible growth or shrinking occurs.
Fixed Flex Basis
.item {
flex-basis: 200px;
flex-shrink: 1;
}
The item starts with a 200px flex basis and can shrink when negative free space is distributed.
Automatic Flex Basis
.item {
flex-basis: auto;
flex-shrink: 1;
}
With auto, the flex base size can depend on the item's main-size property or content.
Zero Flex Basis
.item {
flex-basis: 0;
flex-shrink: 1;
}
A zero flex basis produces a zero scaled shrink factor. The item does not receive a share of negative free space through that factor, although its final size can still be affected by minimum size constraints and other flex layout rules.
Comparison
| Property | Purpose |
|---|---|
flex-basis |
Establishes the initial main size. |
flex-shrink |
Controls participation in distributing negative free space. |
Important Consideration
Flex basis and shrink factor work together. The shrink factor cannot be interpreted independently of the item's base size.
Relationship with flex-grow
The flex-grow and flex-shrink properties handle opposite free-space conditions.
Positive Free Space
.item {
flex-grow: 1;
}
When positive free space exists, eligible items can expand according to their growth factors.
Negative Free Space
.item {
flex-shrink: 1;
}
When negative free space exists, eligible items can shrink according to their scaled shrink factors.
Comparison
| Characteristic | flex-grow | flex-shrink |
|---|---|---|
| Initial value | 0 |
1 |
| Space condition | Positive free space | Negative free space |
| Calculation | Uses growth factors | Uses shrink factors scaled by flex bases |
| Purpose | Controls expansion | Controls reduction |
Using Both Together
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;
}
The item can grow or shrink depending on the amount of available space, subject to its sizing constraints.
flex Shorthand
The flex shorthand combines flex-grow, flex-shrink, and flex-basis into one declaration.
Individual Properties
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;
}
Equivalent Shorthand
.item {
flex: 1 1 200px;
}
Common Shorthand Values
| Declaration | Meaning |
|---|---|
flex: initial |
Equivalent to 0 1 auto. |
flex: auto |
Equivalent to 1 1 auto. |
flex: none |
Equivalent to 0 0 auto. |
flex: 1 |
Commonly expands to 1 1 0% in browsers. |
flex: 0 0 150px |
Disables growth and shrinking with a 150px basis. |
flex: 1 2 200px |
Sets growth to 1, shrinking to 2, and basis to 200px. |
Why the Shorthand Is Useful
The shorthand sets all three flex sizing components together, reducing the chance that an earlier value will produce unexpected results.
Important Consideration
The flex shorthand applies to flex items. The flex-flow shorthand applies to flex containers and controls direction and wrapping.
Minimum Sizes and Overflow
A flex item may refuse to shrink as expected because its automatic minimum size is based on its content.
The Automatic Minimum Width
.container {
display: flex;
}
.content {
flex-shrink: 1;
}
In a horizontal row layout, a long unbreakable string or large content element can prevent the item from shrinking below its content-based minimum width.
Allowing an Item to Shrink
.content {
flex: 1 1 auto;
min-width: 0;
overflow-wrap: anywhere;
}
The min-width: 0 declaration removes the automatic minimum width constraint, while overflow-wrap: anywhere allows long text to break when necessary.
Column Layouts
.container {
display: flex;
flex-direction: column;
height: 400px;
}
.content {
flex: 1 1 auto;
min-height: 0;
overflow: auto;
}
In a column layout, min-height: 0 can allow the content item to shrink vertically. The overflow rule provides scrolling if the content is taller than its available space.
Maximum and Minimum Constraints
Explicit min-width, min-height, max-width, and max-height values can also influence final flex item sizes.
Important Consideration
Increasing flex-shrink does not override a minimum size. Review the item's minimum main size when shrinking appears ineffective.
Row and Column Layouts
The flex-shrink property reduces sizes along the main axis, which is determined by flex-direction.
Row Layout
.container {
display: flex;
flex-direction: row;
width: 400px;
}
.item {
flex: 0 1 200px;
min-width: 0;
}
In a horizontal writing mode, the items can shrink horizontally when their combined flex bases exceed the available width.
Column Layout
.container {
display: flex;
flex-direction: column;
height: 300px;
}
.item {
flex: 0 1 150px;
min-height: 0;
}
In a horizontal writing mode, the items can shrink vertically when their combined flex bases exceed the available height.
Main-Axis Comparison
| Direction | Shrinking Axis |
|---|---|
row |
Inline axis |
row-reverse |
Inline axis, reversed |
column |
Block axis |
column-reverse |
Block axis, reversed |
Important Consideration
The physical shrinking direction depends on the writing mode. In a typical horizontal writing mode, rows shrink in width and columns shrink in height.
Shrinking and Wrapping
The flex-wrap property determines whether flex items remain on one line or can move onto additional flex lines.
Single-Line Layout
.container {
display: flex;
flex-wrap: nowrap;
}
.item {
flex: 0 1 200px;
}
All items remain on one flex line. When space is insufficient, eligible items may shrink.
Wrapping Layout
.container {
display: flex;
flex-wrap: wrap;
}
.item {
flex: 0 1 200px;
}
Items can move onto additional flex lines instead of all remaining on one line.
Independent Flex Lines
Each flex line resolves its own flexible sizing. Items on separate lines do not participate in a single shared shrink calculation.
Important Consideration
Wrapping does not eliminate all shrinking. Items can still shrink within their assigned flex line when the sizing algorithm requires it.
Practical Uses
Different shrink factors can help preserve important layout elements while allowing other content to adapt to narrower containers.
Fixed-Size Icon
.media {
display: flex;
gap: 12px;
}
.media-icon {
flex: 0 0 48px;
}
.media-content {
flex: 1 1 auto;
min-width: 0;
}
The icon retains its assigned width while the content area can shrink.
Flexible Search Field
.search-bar {
display: flex;
gap: 10px;
}
.search-bar input {
flex: 1 1 auto;
min-width: 0;
}
.search-bar button {
flex: 0 0 auto;
}
The input can shrink as the container narrows, while the button retains its content-based size.
Responsive Sidebar
.layout {
display: flex;
gap: 20px;
}
.sidebar {
flex: 0 0 200px;
}
.main-content {
flex: 1 1 300px;
min-width: 0;
}
The sidebar maintains its width, while the main content can shrink to accommodate the remaining space.
Responsive Stacking
@media (max-width: 700px) {
.layout {
flex-direction: column;
}
.sidebar {
flex: 0 1 auto;
}
.main-content {
flex: 1 1 auto;
}
}
At narrower viewport widths, the layout stacks vertically rather than continuing to shrink the horizontal content area.
flex-shrink Example
This example compares equal shrinking, unequal shrinking, and a layout where the first item is prevented from shrinking.
HTML Example
<div class="shrink-demo">
<h3>Equal Shrinking: 1, 1, 1</h3>
<div class="shrink-equal">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<h3>Unequal Shrinking: 1, 2, 1</h3>
<div class="shrink-unequal">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<h3>First Item Does Not Shrink: 0, 1, 1</h3>
<div class="shrink-fixed">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
CSS Example
.shrink-demo > div {
display: flex;
gap: 8px;
width: 100%;
max-width: 320px;
padding: 10px;
margin-bottom: 20px;
background-color: #f1f5f9;
}
.shrink-demo > div > div {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
flex-grow: 0;
flex-basis: 140px;
min-width: 0;
min-height: 45px;
background-color: #336699;
color: #fff;
font-weight: bold;
}
.shrink-demo .shrink-equal > div {
flex-shrink: 1;
}
.shrink-demo .shrink-unequal > div {
flex-shrink: 1;
}
.shrink-demo .shrink-unequal > div:nth-child(2) {
flex-shrink: 2;
}
.shrink-demo .shrink-fixed > div {
flex-shrink: 1;
}
.shrink-demo .shrink-fixed > div:first-child {
flex-shrink: 0;
}
Equal Shrinking: 1, 1, 1
Unequal Shrinking: 1, 2, 1
First Item Does Not Shrink: 0, 1, 1
In the first example, all three items shrink equally because their flex bases and shrink factors match. In the second, the middle item loses more space because its shrink factor is larger. In the third, the first item retains its 140px basis while the remaining items absorb the necessary reduction.
All three containers use the same available width. The different results come from the shrink factors assigned to the items.
Common Mistakes
Several common misunderstandings can make flex-shrink behave differently from what a developer expects.
Assuming Equal Factors Mean Equal Shrinking
Equal shrink factors do not guarantee equal amounts of space will be removed. The factors are scaled by the flex base sizes.
Ignoring Minimum Sizes
Flex items may have automatic minimum sizes that prevent them from shrinking. Increasing the shrink factor does not remove those constraints.
Expecting Shrinking Without Negative Free Space
If the items fit within the available main-axis space, there is no need to shrink them.
Using Negative Values
.item {
flex-shrink: -1;
}
This declaration is invalid. Shrink factors must be non-negative.
Confusing flex-shrink with flex-grow
flex-shrink controls negative free space, while flex-grow controls positive free space.
Preventing Shrinking Without Considering Overflow
Setting flex-shrink: 0 may preserve an item's size but can cause horizontal or vertical overflow if the container is too small.
Ignoring flex-wrap
A wrapping container may place items on additional lines instead of shrinking all items together on one line.
Important Considerations
Keep the following points in mind when using flex-shrink:
- Purpose: Controls how flex items shrink when space is insufficient.
- Initial value:
1. - Inheritance: The property is not inherited.
- Applies to: Flex items.
- Accepted values: Non-negative numbers, including decimals.
- Invalid values: Negative numbers, percentages, and length units.
- Zero: Prevents shrinking through the flex shrink algorithm.
- One: Allows shrinking and is the initial value.
- Larger factors: Increase relative participation in shrinking.
- Scaled factors: Shrink factors are multiplied by flex base sizes.
- Negative free space: Must exist for shrinking to occur.
- flex-basis: Establishes the starting main size and affects scaled shrink factors.
- flex-grow: Controls expansion when positive free space exists.
- flex shorthand: Combines growth, shrinking, and basis.
- Main axis: Determines the dimension affected by shrinking.
- Row layouts: Shrinking usually affects width in a horizontal writing mode.
- Column layouts: Shrinking usually affects height in a horizontal writing mode.
- Minimum sizes: Can prevent items from shrinking as expected.
- Overflow: May occur when items cannot shrink enough to fit.
- Wrapping: Shrinking is resolved independently on each flex line.
- Factors below one: Can leave some negative free space unresolved.
- Animation: Numeric shrink factors can interpolate.
- Responsive layouts: Shrink factors help items adapt to limited container space.
Use flex-shrink when flex items need to adapt to limited space. For predictable results, consider each item's flex basis, minimum size, wrapping behavior, and the amount of negative free space.
Browser Support
The CSS flex-shrink property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.
The standard, unprefixed property has broad support. Older browsers may have used prefixed or earlier Flexbox implementations.
Supported Example
.container {
display: flex;
}
.container > div {
flex-shrink: 1;
}
Feature Detection
@supports (display: flex) and (flex-shrink: 1) {
.container {
display: flex;
}
.container > div {
flex-shrink: 1;
}
}
Fallback Example
.container > div {
display: block;
}
@supports (display: flex) and (flex-shrink: 1) {
.container {
display: flex;
}
.container > div {
flex-shrink: 1;
}
}
Without Flexbox support, the items remain in normal block layout. Supporting browsers can shrink eligible flex items when space is insufficient.
Compatibility Considerations
Although support is broad, test layouts involving automatic minimum sizes, nested flex containers, and overflowing content when precise shrinking behavior is important.
For current compatibility information, visit Can I Use? .
Specifications
The flex-shrink property is defined in the CSS Flexible Box Layout Module Level 1. The specification describes how shrink factors are scaled by flex base sizes to distribute negative free space.
CSS Flexible Box Layout Module Level 1: flex-shrink Property
The flexible length resolution algorithm explains how negative free space is distributed and how size constraints affect the final main sizes of flex items.
CSS Flexible Box Layout Module Level 1: Resolving Flexible Lengths
These definitions establish the relationship between flex bases, shrink factors, negative free space, and minimum size constraints.
