CSS flex-shrink property
Example
A, B, C set flex-shrink:1, D, E set to flex-shrink:2:
<
!
DOCTYPE
html
<
html
<
head
<
meta
charset
=
"
utf-8
"
<
title
Runoob Tutorial(runoob.com)
title
<
style
#content
{
display:
flex
;
width:
500
px
;
}
#content
div
{
flex-basis:
120
px
;
border:
3
px
solid
rgba
(
0
,
0
,
0
,
.2
)
;
}
.box
{
flex-shrink:
1
;
}
.box1
{
flex-shrink:
2
;
}
style
head
<
body
<
p
The total width of the div is 500px, flex-basis is 120px.
p
<
p
A, B, C set flex-shrink:1. D, E set to flex-shrink:2
p
<
p
The widths of D, E differ from A, B, C
p
<
div
id
=
"
content
"
<
div
class
=
"
box
"
style
=
"
background-color:red;
"
A
div
<
div
class
=
"
box
"
style
=
"
background-color:lightblue;
"
B
div
<
div
class
=
"
box
"
style
=
"
background-color:yellow;
"
C
div
<
div
class
=
"
box1
"
style
=
"
background-color:brown;
"
D
div
<
div
class
=
"
box1
"
style
=
"
background-color:lightgreen;
"
E
div
div
body
html
Try it »
Example analysis:
The default value of flex-shrink is 1. If this property is not explicitly defined, the shrinking of space is automatically calculated as a ratio based on the default value of 1 after summing all factors.
In this example, A, B, C explicitly define flex-shrink as 1, and D, E define flex-shrink as 2, so the total remaining space is divided into 7 parts: A, B, C take 1 part each, and D, E take 2 parts each, that is 1:1:1:2:2
We can see that the parent container is defined as 500px and the child items as 120px. The sum of the child items is 600px, exceeding the parent container by 100px. This excess 100px needs to be absorbed by A, B, C, D, E through the shrink factors, so the weighted total is 1001+1001+1001+1002+100*2=700px.
So we can calculate how much overflow will be removed from A, B, C, D, E:
A 被移除溢出量:(100*1/700)*100,即约等于14px
B 被移除溢出量:(100*1/700)*100,即约等于14px
C 被移除溢出量:(100*1/700)*100,即约等于14px
D 被移除溢出量:(100*2/700)*100,即约等于28px
E 被移除溢出量:(100*2/700)*100,即约等于28px
Finally, the actual widths of A, B, C, D, E are: 120-14=106px, 120-14=106px, 120-14=106px, 120-28=92px, 120-28=92px. In addition, these widths include the border.
Browser support
The numbers in the table indicate the version number of the first browser that supports this property.
The number immediately following -webkit-, -ms-, or -moz- is the first version that supports the prefixed property.
| property | |||||
|---|---|---|---|---|---|
| flex-shrink | 29.0 | ||||
| 21.0 -webkit- | 11.0 | ||||
| 10.0 -ms- | 28.0 | ||||
| 18.0 -moz- | 9.0 | ||||
| 6.1 -webkit- | 17.0 |
Definition and Usage
The flex-shrink property specifies the shrink rule for flex elements. Flex elements shrink only when the sum of the default widths is greater than the container, and the amount of shrinking is based on the value of flex-shrink.
**Note:** If the element is not a flexbox object, the flex-shrink property has no effect.
| Default value: | 1 |
|---|---|
| Inherited: | No |
| Animatable: | Yes. See Animatable. Try it |
| Version: | CSS3 |
| JavaScript syntax: | object.style.flexShrink="5" Try it |
CSS Syntax
flex-shrink:
number
|initial|inherit;
Attribute values
| Value | Description |
|---|---|
| number | A number that specifies how much the item will shrink relative to the other flexible items. The default value is 1. |
| initial | Sets this property to its default value. See initial. |
| inherit | Inherits this property from its parent element. See inherit. |
Related Articles
CSS Reference Manual:flex Property
CSS Reference Manual:flex-basis Property
CSS Reference Manual:flex-direction Property
CSS Reference Manual:flex-flow Property
CSS Reference Manual:flex-grow Property
CSS Reference Manual:flex-wrap Property
CSS Reference Manual