- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
CSS flex-shrink Controlling Flex Item Shrinkage
Discover available options and use cases for responsive layouts.
Introduction
The flex-shrink
CSS property is crucial for creating flexible and responsive web designs. It controls how flex items shrink when there isn’t enough space in their container. By setting the flex-shrink
value, you can make sure elements adjust proportionally to fit within the available space. This prevents overflow and ensures a balanced layout.
Widely supported across browsers, flex-shrink
has been available since September 2015. This means you can use it confidently in your projects without worrying about compatibility issues.
Description
The flex-shrink
property determines how much a flex item will shrink relative to other items in the same container when space is limited. It works with the flex-basis
property, which sets the initial size of a flex item. When the combined size of all items exceeds the container’s size, the browser distributes the negative free space according to the flex-shrink
values.
Items with higher flex-shrink
values will shrink more than those with lower values. This ensures a balanced layout where larger items don’t shrink disproportionately compared to smaller items.
Syntax
The syntax for the flex-shrink
property is simple:
<number>
: Specifies the shrink factor. Negative values are invalid, and the default is1
.inherit
: Inherits theflex-shrink
value from the parent element.initial
: Sets theflex-shrink
value to its default value, which is1
.revert
: Reverts the value to the user agent’s default stylesheet.revert-layer
: Reverts the value to the browser’s default stylesheet.unset
: Resets the property to its natural value, which means it will be either inherited or set to the initial value.
Values
The flex-shrink
property accepts a single <number>
value, which determines the shrink factor of a flex item. Here are the possible values:
<number>
: A positive number that specifies the shrink factor. Negative values are invalid, and the default value is1
.inherit
: Inherits theflex-shrink
value from the parent element.initial
: Sets theflex-shrink
value to its default value, which is1
.revert
: Reverts the value to the user agent’s default stylesheet.revert-layer
: Reverts the value to the browser’s default stylesheet.unset
: Resets the property to its natural value, which means it will be either inherited or set to the initial value.
Formal Definition
The flex-shrink
property is formally defined as follows:
Property | Value |
---|---|
Initial Value | 1 |
Applies to | Flex items, including in-flow pseudo-elements |
Inherited | No |
Computed Value | As specified |
Animation Type | A number |
Formal Syntax
The formal syntax for the flex-shrink
property is:
This syntax specifies that the flex-shrink
property accepts a positive number value, with 0
being the minimum and no upper limit.
Examples
Setting Flex Item Shrink Factor
This example shows how negative free space is distributed based on the item’s shrink factor. It includes five flex items with a flex-shrink
value greater than 0, which have a combined width greater than their parent flex container’s width.
HTML
CSS
Result
The flex items don’t overflow their container because they can shrink. The 500px
of negative free space is distributed among the five items based on their flex-shrink
values. The first three items have flex-shrink: 1
set. D has flex-shrink: 1.5
and E has flex-shrink: 2
set. The final width of D and E is less than the others, with E smaller than D.
Basic Usage of flex-shrink
In this example, we demonstrate the flex-shrink
property in a flex container with five div elements, each styled to illustrate different aspects of flex-shrink
behavior, such as default, initial, and inherited values.
HTML
Output
This example shows how the flex-shrink
property affects the size of flex items within a container. The second div has a higher flex-shrink
value, causing it to shrink more compared to the other divs.
Specifications
The flex-shrink
property is defined in the following specification:
Browser Compatibility
The flex-shrink
property is widely supported across many devices and browser versions. It has been available across browsers since September 2015. Below is the compatibility table:
Browser | Version | Release Date |
---|---|---|
Chrome | 29.0 | Aug 2013 |
Firefox | 28.0 | Mar 2014 |
IE/Edge | 11.0 | Oct 2013 |
Opera | 17.0 | Aug 2013 |
Safari | 9.0 | Sep 2015 |
See Also
- Basic concepts of flexbox
- Controlling ratios of flex items along the main axis
- CSS flexible box layout module
FAQs
What does the flex-shrink property do in CSS?
The flex-shrink
property in CSS determines how much a flex item will shrink relative to the other items inside a flex container when there isn’t enough space. It defines the proportion by which the item should reduce in size.
How does flex-shrink: 0 affect an item?
Setting flex-shrink: 0
prevents the item from shrinking when space is limited. It keeps the item’s size fixed, even if it causes overflow or other items to shrink more.
Can I allow one item to shrink more than others?
Yes, you can set a higher flex-shrink
value for the item you want to shrink more. For example, an item with flex-shrink: 2
will shrink twice as much as one with flex-shrink: 1
.
What is the difference between flex-shrink and flex-grow?
flex-grow
controls how items expand when there is extra space, while flex-shrink
controls how items reduce in size when space is limited. Both work together to create flexible layouts.
What is the default value of flex-shrink?
The default value of flex-shrink
is 1
, meaning the item will shrink proportionally when space is limited unless a different value is specified.
Supported Browsers
The flex-shrink
property is supported by the following browsers:
- Google Chrome 5.0
- Edge 12
- Mozilla Firefox 4.0
- Safari 5.0
- Opera 11.1
This broad support ensures that your designs using flex-shrink
will work consistently across various platforms and devices.
Talk with CEO
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.