I am confused why the first button doesn’t fill the whole width here:
<div style="display:flex;">
<button style="width:100%;"> </button>
<button style="flex-shrink: 0;"> </button>
<button> </button>
</div>
Why does flexbox interrupt the width?
3
Answers
the first button doesn’t fill the whole width because of how flexbox distributes space. By default, flex items have
flex-shrink: 1
, allowing them to shrink. When you setwidth: 100%
on the first button, it initially takes up all space, but then flexbox redistributes the space to accommodate the other buttons.It fills the entire width available to it by its flex container, which has to leave room for the remaining items in the container.
If you’re expecting the items to wrap, the default value for
flex-wrap
when not specified isnowrap
. You can set a custom value to allow the items to wrap:It does. As reflected in your code snippet.
However, if you’re expecting the two sibling buttons to shrink to zero, then you have to remove the default padding and borders on button elements.