29

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>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Beef Erikson
  • 319
  • 1
  • 3
  • 4

5 Answers5

47

Because - if you want the items to stretch in width - you have to allow the flex items to grow by adding flex-grow: 1;:

(justify-content: stretch; is not needed at all here BTW - it has no effect)

.box {
  display: flex;
  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>
Johannes
  • 64,305
  • 18
  • 73
  • 130
  • pretty cool, If you're changing your screen size and increase numbers then you can see your last row is not looks good. – symi khan Nov 20 '18 at 06:55
  • here justify-content:stretch does nothing but flex-grow:1 is responsible for growing/stretching the items to fill the container – bhavya_w Aug 02 '23 at 21:06
  • @bhavya_w What do you mean - that's what I wrote? – Johannes Aug 02 '23 at 21:39
  • @Johannes You don't specifically mention that justify-content:stretch does nothing w.r.t growing flex-items to fill the flex-parent and what makes it more confusing is that in the first code snippet you are using justify-content:stretch on .box class which does nothing. – bhavya_w Aug 02 '23 at 21:53
23

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.

Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • According to MDN (https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content) it exists ... and if we check the new draft (https://drafts.csswg.org/css-align-3/#propdef-justify-content) they said it behave like `flex-start` .. probably MDN need to be more precise as this value is probably used with CSS-grid? – Temani Afif Aug 31 '18 at 00:07
  • As I've [observed and written about before](https://stackoverflow.com/q/49474480/3597276), MDN is second hand information. The contributors are human and make mistakes. Go directly to the specs for the official and correct information. – Michael Benjamin Aug 31 '18 at 00:10
  • But here (https://drafts.csswg.org/css-align-3/#propdef-justify-content) I can read that justify-content take a value called `` and this value can be stretch : https://drafts.csswg.org/css-align-3/#typedef-content-distribution. I understand it's not possible for flexbox but it exists – Temani Afif Aug 31 '18 at 00:22
  • 2
    But `justify-content: stretch` still doesn't apply in flexbox, as defined in the flexbox spec. – Michael Benjamin Aug 31 '18 at 00:25
3

Yes, it is supposed to look like that for flex containers, flex-start is used instead of flex-stretch. Here is what W3C states about justify-content:stretch

The justify-content property applies along the main axis, but since stretching in the main axis is controlled by flex, stretch behaves as flex-start.

Also, I might be late in answering the question but MDN has also updated their page about justify-content. See the notes under stretch property MDN

Vaibhav
  • 771
  • 6
  • 9
2

Well, simply put, in order for flex-stretch to kick in, it requires combined item width to be equal or greater than the flex container. In other words, you can use 500px or even something as obscure as 10000px. And it will stretch proportionately. Usually, though... to battle this problem we use 100% on each item. This assumes 100% of the component, but again... if your container is 500px, that's what 100% will equal to. Use that or greater values.

When I was learning flex I found this simple flex generator invaluable, I think it covers the case you're talking about visually.

desoga
  • 4,179
  • 1
  • 15
  • 18
  • 1
    it's not correct, if you use a bigger value then the shrink effect will take place here not the stretch effect. – Temani Afif Aug 31 '18 at 00:08
1

An alternative way to space out items evenly is:

.class {
    display: flex;
    justify-content: space-between;
}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
mrshl
  • 531
  • 3
  • 12
  • I think he's asking why it's not stretching on smaller width elements though, which is an issue I've encountered before as well. I think it's just default behavior. – InfiniteStack Aug 30 '18 at 23:50
  • If I'm using your trick then my flex items look weird, because flex items have different width respective to the content. Please look here: https://codepen.io/Sulaman/pen/oQoqOw Please change code pen view to vertical. – symi khan Nov 20 '18 at 06:50
  • This isn't what he's asking. He wants a space-between applied with a max-width stretch for each item applied. – Jason Rice Apr 26 '19 at 22:34