Why does the code below not stretch my 100px items?
The results are basically close to this:
[100px|100px|100px|.........]
That looks a lot like flex-start, not flex-stretch. Is this the intended behavior?
I'm looking for:
[...100px...100px...100px...]
.box {
display: flex;
justify-content: stretch;
width: 500px;
}
.item {
width: 100px;
}
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
Why doesn't justify-content: stretch work? Because in flexbox there is no such value with justify-content . See MDN: stretch is not supported by flexible boxes (flexbox). So your rule is invalid and the browser defaults to justify-content: flex-start , the initial setting.
justify-content only has an effect if there's space left over after your flex items have flexed to absorb the free space. In most/many cases, there won't be any free space left, and indeed justify-content will do nothing.
The items are evenly distributed within the alignment container along the main axis. The spacing between each pair of adjacent items is the same. The empty space before the first and after the last item equals half of the space between each pair of adjacent items.
justify-content only works on flex-direction: row and flex-direction: row-reverse. In flex-direction: column you have to use align-items: flex-end;.
Because - if you want the items to stretch in width - you have to allow the flex items to grow by adding flex-grow: 1;
:
.box {
display: flex;
justify-content: stretch;
width: 500px;
border: 1px solid red;
}
.item {
width: 100px;
border: 1px solid green;
flex-grow: 1;
}
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
If you don't mean they should grow, but simply be distributed across the whole width of their container, use justify-content: space-between
od ... space-around
:
.box {
display: flex;
justify-content: space-around;
width: 500px;
border: 1px solid red;
}
.item {
width: 100px;
border: 1px solid green;
}
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
Why doesn't
justify-content: stretch
work?
Because in flexbox there is no such value with justify-content
.
See MDN: stretch is not supported by flexible boxes (flexbox).
So your rule is invalid and the browser defaults to justify-content: flex-start
, the initial setting.
Use the flex-grow
property on flex items to achieve the desired effect.
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