0

Here is the link to my fiddle for reference.

.container {
  background-color: gray;
  position: relative;
  padding: 20px;
  height: 70%;
  /* uncomment this and will work as expected */
  /* height: 70px; */
}

.child1 {
  width: 75%;
  display: inline-block;
  height: 100px;
}

.child2 {
  background-color: green;
  width: 75%;
  float: right;
  height: 100%;
}
<div class="container">
  <div class="child1">Child 1</div>
  <div class="child2">Child 2</div>
</div>

Parent's height is 100px(can see in devtools) after calculation for child1. Child2 is applied 100% height equaling to 100px, but in computed style(can see in devtools) it is showing 0px.

I am assuming it's because parent's height is calculated at run-time. Any help?

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38

2 Answers2

1

Because parent height is also in percentage. It will work in the following conditions:

  • Parent of your div has 100% height
  • Parent of your div has fixed height
  • Parent of your div has some content and due to which it has some height.

Currently, it does not know 100% of what.

PM.
  • 1,735
  • 1
  • 30
  • 38
0

Using a % value in height requires one of the following:

  1. If all parent elements have a percentage height, ALL parent elements to have their height explicitly defined.
  2. Otherwise, a parent element must have a fixed height (not a %).

The parent of your .container element probably doesn't have a height value defined, or the parent of that element, etc.

Remember that the html and body elements count too.

html, body
{
    height: 100%;
}
Liftoff
  • 24,717
  • 13
  • 66
  • 119