Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a child's height relative to its flex-grown parent?

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>
like image 882
GetFree Avatar asked Dec 09 '25 16:12

GetFree


1 Answers

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>
like image 153
Nenad Vracar Avatar answered Dec 11 '25 13:12

Nenad Vracar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!