3

In the following example, using flex-box layout, I need the black boxes' height to be 50% of the blue box's height.
The blue box has style flex-grow: 1 so that it occupies all remaining space inside the red box.

Is it even possible to do this with the flex-box layout?
If not, is there any other way with pure CSS and no table layouts?

* {box-sizing: border-box}
div {padding: 5px; border: 1px solid red ; display: flex }
col1 {border: 1px solid green}
col2 {flex-grow: 1; border: 1px solid blue; padding: 10px; margin-left: 5px}
item {display: inline-block; border: 1px solid black; width: 30% ; height: 50%}
<div>
<col1>
Content
<br><br><br><br><br>
of
<br><br><br><br><br>
this
<br><br><br><br><br>
column
</col1>
<col2>
<item>
Height must be 50% of parent's
</item>
<item>
Height must be 50% of parent's
</item>
<item>
Height must be 50% of parent's
</item>
</col2>
</div>
GetFree
  • 40,278
  • 18
  • 77
  • 104

1 Answers1

3

You could create pseudo elment on parent and use flex: 1 on it so it will take half of its height and other half will take child element

* {
  box-sizing: border-box;
}
body,
html {
  margin: 0;
  padding: 0;
}
.content {
  display: flex;
  border: 1px solid black;
  padding: 10px;
}
.parent {
  flex-grow: 1;
  display: flex;
  padding: 10px;
  flex-direction: column;
  border: 1px solid blue;
}
.random {
  padding: 20px;
  margin-right: 20px;
  border: 1px solid green;
}
.parent:after {
  content: '';
  flex: 1;
}
.child {
  flex: 1;
  border: 1px solid red;
}
<div class="content">
  <div class="random">
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <div class="parent">
    <div class="child">asd</div>
  </div>
</div>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176