0

My problem is I want 2 columns with same height, and I used for this display: flex in container. One of this columns has a child, and I want this child have percentage of parents, but it inherit the percentage from the body.

This is my example code:

<style>
  .container {
    display: flex;
  }
  .container > div {
    float: left;
    width: 200px;
  }
  .parent1 {
    border: solid 1px red;
  }
  .parent2 {
    border: solid 1px blue;
  }
  .child {
    background-color: yellow;
    height: 100%; /*the problem is here, the div inherit the height of the body*/
  }
</style>
<div class="container">
  <div class="parent1">
    dynamic test
    <br />dynamic test
    <br />dynamic test
    <br />dynamic test
    <br />dynamic test
  </div>
  <div class="parent2">
    <div class="child">
      child
    </div>
  </div>
</div>
m4n0
  • 29,823
  • 27
  • 76
  • 89
Laurianti
  • 903
  • 1
  • 5
  • 19

1 Answers1

2

Height doesn't work like that with flex-boxes. But you can use nested flex-boxes to make the child grow.

.container {
  display: flex;
}
.container > div {
  float: left;
  width: 200px;
}
.parent1 {
  border: solid 1px red;
}
.parent2 {
  border: solid 1px blue;
}
.child {
  background-color: yellow;
  /* Remove the following line */
  /* height: 100%;  the problem is here, the div inherit the height of the body */
}

/* Add this */

.parent2 {
  display: flex;
  flex-direction: column; /* To have several children with % heights */
}
.child {
  flex: 1 0 0px;
}
.child+.child {
  background: aqua;
}
<div class="container">
  <div class="parent1">
    dynamic test
    <br />dynamic test
    <br />dynamic test
    <br />dynamic test
    <br />dynamic test
    <br />dynamic test
  </div>
  <div class="parent2">
    <div class="child">
      child
    </div>
    <div class="child">
      child
    </div>
  </div>
</div>
woestijnrog
  • 1,549
  • 12
  • 9