I don't understand what makes flex items switch from grow to shrink behavior? At first I thought it is the flex-basis
value, but what if only one item has it, or none?
Also, the specs on flex-basis
say:
The flex-basis CSS property specifies the flex basis which is the initial main size of a flex item.
What does that mean? If I have one item with it, and the rest are default, why is it usually starts a little bigger than the value? Where does the added space come from?
As a final recap: flex-basis controls how large an element will be along the main-axis before any growing or shrinking occurs. Flex-grow determines how much it will grow in proportion to sibling elements, and flex-shrink determines how much it will shrink.
flex is a shorthand property of flex-grow , flex-shrink and flex-basis . Then, the difference is that the flex base size will be 0 in the first case, so the flex items will have the same size after distributing free space.
The element will not shrink: it will retain the width it needs, and not wrap its content. Its siblings will shrink to give space to the target element. Because the target element will not wrap its content, there is a chance for the flexbox container's content to overflow.
Try setting the flex-shrink property to 0 on the . flex-box .
The flex-basis
property sets the initial main size of the flex item, before free space is distributed by other flex properties.
This means that the flex item is first sized for its defined width (in flex-direction: row
) or height (in flex-direction: column
).
In other words, in a row-direction flex container, where flex-basis: 100px
is equivalent to width: 100px
, the item would have a width of 100px.
That's the initial main size.
THEN, other flex properties are applied. Namely, flex-grow
and flex-shrink
.
If the container is wider than 100px, then flex-grow: 1
expands the item to fill the extra space.
If the container is less than 100px, then flex-grow: 1
is ignored and flex-shrink: 1
reduces the size of the item according to a flex sizing algorithm.
So, to answer your question:
When does flex switch from grow to shrink?
Assuming both properties are enabled (i.e., they are not flex-grow: 0
or flex-shrink: 0
), the switch will occur when the sum total of flex-basis
/ width
/ height
on flex items is no longer less or more than the length of the container.
To make this a bit more clear:
When the sum total of flex-basis
/ width
/ height
on flex items is no longer more than the length of the container, this means there is no overflow and the items don't consume all available space. flex-grow
works.
When the sum total of flex-basis
/ width
/ height
on flex items is no longer less than the length of the container, this means there is overflow and the items must shrink to fit inside the container. flex-shrink
works.
From the spec:
flex-grow
This [property] specifies the flex grow factor, which determines how much the flex item will grow relative to the rest of the flex items in the flex container when positive free space is distributed.
flex-shrink
This [property] specifies the flex shrink factor, which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when negative free space is distributed.
flex-basis
This [property] specifies the initial main size of the flex item, before free space is distributed according to the flex factors.
https://www.w3.org/TR/css-flexbox-1/#flex-property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With