28

In the code and jsfiddle below, flexbox proportions changes with content. I am feeling I do not understand the real purpose of flexbox here. If we are giving flex-grow properties for the proportions we desire, why do the boxes grow with content?

Notice when dataDiv has new span content in it, proportions are broken with the content. You can observe how it is the expected proportions when you delete the span inside dataDiv. Why does this occur?

https://jsfiddle.net/4shaz5oy/

.container {
  display: flex;
  flex-flow: row wrap;
  height: 100vh;
}
.mapBox {
  flex: 2;
  background-color: red;
}
.controlBox {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: green;
}
.controlPanel {
  flex: 1;
  max-height: 33%;
  background-color: yellow;
  padding: 5px;
  text-align: center;
}
.dataPanel {
  flex: 2;
  max-height: 66%;
  background-color: blue;
  padding: 5px;
}
<div class="container">
  <div class="mapBox"></div>
  <div class="controlBox">
    <div class="controlPanel">
      <div class="buttonDiv"></div>
      <div class="buttonDiv"></div>
      <div class="buttonDiv"></div>
    </div>
    <div class="dataPanel">
      <div class="dataDiv">
        <span>yoyoyoyasdasdadsadasdasdasdasdasdasdasdadada</span>
      </div>
    </div>
  </div>
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
Ozum Safa
  • 1,458
  • 2
  • 15
  • 27
  • Possible duplicate of [flex-grow not sizing flex items as expected](http://stackoverflow.com/questions/34644807/flex-grow-not-sizing-flex-items-as-expected) – Asons Mar 23 '16 at 16:06

2 Answers2

16

The flex-grow defines how the remaining space should be distributed amongst the flex items, not the items themselves.

For their size you use flex-basis

html, body {
  margin: 0;
}
.container {
    display: flex;
    flex-flow: row wrap;
    height: 100vh;
}

.mapBox {
    flex: 2;
    flex-basis: 66%;
    background-color: red;
}

.controlBox {
    flex: 1;
    flex-basis: 33%;
    display: flex;
    flex-direction:column;
    background-color:green;
}

.controlPanel {
    flex: 1;
    max-height: 33%;
    background-color: yellow;
    padding: 5px;
    text-align: center;
}

.dataPanel {
    flex: 2;
    max-height: 66%;
    background-color: blue;
    padding: 5px;
}
<div class="container">
  <div class="mapBox">

  </div>

  <div class="controlBox">
    <div class="controlPanel">
      <div class="buttonDiv">

      </div>
      <div class="buttonDiv">

      </div>
      <div class="buttonDiv">

      </div>
    </div>

    <div class="dataPanel">
      <div class="dataDiv">
        <span>yoyoyoy as da sd ad sa da sd as da sd as da sd as da sd ad ada</span>
      </div>
    </div>
  </div>
</div>

Based on comments, here is a simplified sample of how to keep size

html, body{
  margin: 0;
}
.flex, .left, .right {
  display: flex;
}
.left, .right {
  flex: 1;
  flex-direction: column;
}
.left {
  background: red;
  flex-basis: 66.66%;
}
.right {
  flex-basis: 33.33%;
}
.item1 {
  background: yellow;
  overflow: auto;
  height: 33.33vh;
}
.item2 {
  background: lightblue;
}
<div class="flex">
  <div class="left">
    Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 Bla 0 <br>
    Bla 0<br>
    Bla 0<br>
    Bla 0<br>
    Bla 0<br>

  </div>
  <div class="right">
    <div class="item1">
      Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 Bla 1 <br>
      Bla 1<br>
      Bla 1<br>
      Bla 1<br>
      Bla 1<br>

      Bla 1<br>
      Bla 1<br>
      Bla 1<br>
      Bla 1<br>
      Bla 1<br>

    </div>
    <div class="item2">
      Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 Bla 2 <br>
      Bla 2<br>
      Bla 2<br>
      Bla 2<br>
      Bla 2<br>

      Bla 2<br>
      Bla 2<br>
      Bla 2<br>
      Bla 2<br>
      Bla 2<br>

      Bla 2<br>
      Bla 2<br>
      Bla 2<br>
      Bla 2<br>
      Bla 2<br>

    </div>
  </div>  
</div>
Asons
  • 84,923
  • 12
  • 110
  • 165
  • Basis solves the problem with horizontal growing, which I knew it would. However at the end, when you add content into dataDiv, heights of conrolPanel and dataPanel still changes. I guess it is what flex is for and there is no way to change that. Probably just my view on it. If I want fixed height maybe I should absolute positioning instead of flex for controlBox. – Ozum Safa Mar 24 '16 at 11:53
  • 6
    @OzumSafa `flex` has some flaws, which I solved many times with an absolute positioned inner div, set to `overflow: auto`. This way it doesn't grow vertical but rather start to scroll. – Asons Mar 24 '16 at 12:20
  • @OzumSafa Updated my answer with a new 2:nd sample. – Asons Mar 24 '16 at 13:04
  • Also works in safari. It is an interesting solution, however somewhat a hack. I will mark it as answered. Thanks. – Ozum Safa Mar 24 '16 at 14:36
  • @OzumSafa Thanks, and I just updated ... works across browsers now, less hacky :) – Asons Mar 24 '16 at 14:37
7

It looks like everything works as expected, the problem is there is no space among yoyoyoyasdasdadsadasdasdasdasdasdasdasdadada that focus the box to grow.

If you set .dataPanel {word-break: break-all;} you'll see the differences.

Stickers
  • 75,527
  • 23
  • 147
  • 186