The MDN states the following:
The flex-shrink CSS property sets the flex shrink factor of a flex item. If the size of all flex items is larger than the flex container, items shrink to fit according to flex-shrink.
I went to the W3C to find out how to calculate the specifics. Here, I have used the following source code as an example.
* {
margin: 0;
}
.p {
display: flex;
margin: 0 auto;
width: 100px;
height: 50vh;
border: 4px solid red;
}
.c {
width: 250%;
border: 4px solid green;
}
<div class="p">
<div class="c"></div>
</div>
Here, according to the W3C, the flex shrink factor is used for the flex item .c
.
c. Distribute free space proportional to the flex factors.
If the remaining free space is zero
Do nothing.
If using the flex shrink factor
For every unfrozen item on the line, multiply its flex shrink factor by its inner flex base size, and note this as its scaled flex shrink factor. Find the ratio of the item’s scaled flex shrink factor to the sum of the scaled flex shrink factors of all unfrozen items on the line. Set the item’s target main size to its flex base size minus a fraction of the absolute value of the remaining free space proportional to the ratio.
Note this may result in a negative inner main size; it will be corrected in the next step.
Otherwise
Do nothing.
If the above source code is used to calculate along this specification, it will look like the following, and item's target main size will be negative.
This calculation is somewhere wrong because actually browser extends the flex item to the entire flex container.
scaled flex shrink factor:
[flex shrink factor] * [flex base size]
=>1 * 250%
=> 250px- sum of the scaled flex shrink factors:
1
item’s target main size:
[flex base size] - ([remaining free space] * ([scaled flex shrink factor] / [sum of the scaled flex shrink factors]))
=>
250% - (100px * (250% / 1))
=>250px - (100px * 250px)
=> -24750px (expected result: 100px)
Question
- What is wrong with this calculation?
- Also, how do I get the width of the entire flex container as a result of calculating the width of the flex item?