Greediness with specified width of flex blocks in CSS
Let's say we have several flex blocks. Let's set flex-grow to 1 for one of these blocks, and set all other elements to some width. As a result, all blocks will have a fixed width, and our chosen greedy block will occupy all the remaining available space:
<div class="parent">
<div class="child elem1"></div>
<div class="child elem2"></div>
<div class="child elem3"></div>
</div>
.parent {
display: flex;
width: 400px;
height: 200px;
border: 1px solid red;
}
.child {
height: 50px;
border: 1px solid green;
}
.elem1 {
width: 100px;
}
.elem2 {
flex-grow: 1;
}
.elem3 {
width: 100px;
}
:
The characteristic effect is especially noticeable when the parent width is in percentages. In this case, when the parent width changes, our greedy block will have a floating width, and all the others will have a fixed width: