2

Here I have a parent div containing two children. Child 1 is set to 100% height, but it does not display at the actual height of parent, because child 2 is forcing the parent div taller.

<div style="height: 50px; width:106px">
  <div style="border: 1px solid red; display: flex">
    &nbsp;
    <span style="border: 1px solid blue; height: 100%;">Child 1</span>
    &nbsp;
    <span style="border: 1px solid green; height: 100px;">Child 2</span>
    &nbsp;
  </div>
</div>

enter image description here

How can I get child 1 to match the actual height of the parent div in this scenario? I want to do this with pure HTML/CSS.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
BBaysinger
  • 6,614
  • 13
  • 63
  • 132

2 Answers2

2

your issue is because your child1 is inherit from parent. if you set it 100%, means the size of your child 1 will be 50px. to make it auto remove your child 1 height: 100%; below is a sample.

<div style="height: 50px; width:106px">
  <div style="border: 1px solid red; display: flex">
    &nbsp;
    <span style="border: 1px solid blue;">Child 1</span>
    &nbsp;
    <span style="border: 1px solid green; height: 100px;">Child 2</span>
    &nbsp;
  </div>
</div>
i3lai3la
  • 980
  • 6
  • 10
2

Another answer to this question is correct in terms of the solution, but the explanation is incorrect.

You have a flex container.

The container has two span children: span1 and span2.

span1 is set to height: 100%

span2 is set to height: 100px.

Because span1 is set to height: 100% and the flex container has no defined height, this element computes to height: auto (the height of the content).

Here is a more complete explanation of how this works:

However, a default setting on a flex container is align-items: stretch. This means that flex items will expand to cover the length of the container's cross axis, which would be the height in this case.

But this default setting is overridden by the height property on flex items.

Therefore, remove height: 100% so align-items: stretch can work.

Now you have two equal height columns.

<div style="height: 50px;">
  <div style="border: 1px solid red; display: inline-flex; padding: 5px;">
    <span style="border: 1px solid blue;">Child 1</span>
    <span style="border: 1px solid green; height: 100px; margin-left: 5px;">Child 2</span>
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701