Things can get confusing in terms of how flex-grow
and flex-basis
interact. Let's consider the case of three flex items of differing content lengths and the following flex
rules applied to them:
flex: 1 1 auto;
In this case the flex-basis
value is auto
and the items don't have a width set, and so are auto-sized. This means that flexbox is looking at the max-content
size of the items. After laying the items out we have some positive free space in the flex container, shown in this image as the hatched area:
We are working with a flex-basis
equal to the content size so the available space to distribute is subtracted from the total available space (the width of the flex container), and the leftover space is then shared out equally among each item. Our bigger item ends up bigger because it started from a bigger size, even though it has the same amount of spare space assigned to it as the others:
If what you actually want is three equally-sized items, even if they start out at different sizes, you should use this:
flex: 1 1 0;
Here we are saying that the size of the item for the purposes of our space distribution calculation is 0
— all the space is up for grabs and as all of the items have the same flex-grow
factor, they each get an equal amount of space distributed. The end result is three equal width, flexible items.
Try changing the flex-grow
factor from 1 to 0 in this live example to see the different behavior: