3

I've been reading stack overflow questions and other blogs on what flex-basis is supposed to do, but I still can't fully grasp how it affects the behaviour of an element.

Here's an example with my code:

.item {
  background-color: red;
  border: 1px solid pink;
  margin: 10px;
  padding: 10px;
}

.container {
  display: flex;
}

.container .item {
  height: 50px;
  flex-grow: 1;
}

.container3 {
  flex-direction: row;
  width: 500px;
}

.container3 .a,
.container3 .b {
  flex-basis: 200px;
}
<div class="container container3">
  <div class="item a">Segmentation fault</div>
  <div class="item b">Null pointer exception</div>
  <div class="item c">hello</div>
  <div class="item d">hello</div>
</div>

I set flex-basis to 200px for items a and b but they are both shorter than 200px width. I was under the impression that flex-basis is the "initial" width of the item, meaning "min-width" of an item. But clearly that's not true, so I don't understand what "initial" means.

What is the specific mathematical formula that calculate the width of item a and b when a pixel value is given for flex-basis?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
John
  • 32,403
  • 80
  • 251
  • 422

1 Answers1

6

An initial setting of a flex container is flex-shrink: 1.

This means that flex items are allowed to shrink in order to not overflow the container.

Once you disable this feature, flex items won't shrink below their specified flex-basis values.

* { flex-shrink: 0; } /* NEW */

.item {
  background-color: red;
  border: 1px solid pink;
  margin: 10px;
  padding: 10px;
}

.container {
  display: flex;
  border: 1px dashed black;
}

.container .item {
  height: 50px;
  flex-grow: 1;
}

.container3 {
  flex-direction: row;
  width: 500px;
}

.container3 .a,
.container3 .b {
  flex-basis: 200px;
}
<div class="container container3">
  <div class="item a">Segmentation fault</div>
  <div class="item b">Null pointer exception</div>
  <div class="item c">hello</div>
  <div class="item d">hello</div>
</div>

I set flex-basis to 200px for items a and b but they are both shorter than 200px width. I was under the impression that flex-basis is the "initial" width of the item, meaning "min-width" of an item. But clearly that's not true, so I don't understand what "initial" means.

Actually, it is true. flex-basis sets the initial length of the item. The initial width of your items is 200px, as you defined. Then flex-shrink and flex-grow are applied.

What is the specific mathematical formula that calculate the width of item a and b when a pixel value is given for flex-basis?

The calculations are explained here:

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    As it is in my question, I see `.item.a` having a width of 156px. Do you know what mathematical formula is used to determine that? Why is it 156px as opposed to 152px or any other value? I ask because I'm trying to predict the behaviour a `flex-basis` will have on an element when flex-shrink isn't specified. – John Jan 11 '18 at 15:51
  • See the link at the end of my answer. – Michael Benjamin May 01 '18 at 13:19