Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't justify-content: stretch work?

Tags:

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>
like image 436
Beef Erikson Avatar asked Aug 30 '18 23:08

Beef Erikson


People also ask

Why does justify-content stretch not work?

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.

Does justify-content work without display flex?

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.

What is true about justify-content space evenly?

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.

Does justify-content work with Flex direction column?

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;.


2 Answers

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>
like image 157
Johannes Avatar answered Sep 20 '22 16:09

Johannes


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.

like image 33
Michael Benjamin Avatar answered Sep 18 '22 16:09

Michael Benjamin