-1

I have two divs on left and right. Second div contains lots of dynamic data, so height cannot be fixed. Then, how to make first div's height same as second div?


       <div style="width: 100%;">
                <div  class="first"> 
                    Left Div 
                </div>
                <div  class="second"> 
                    <h5> hello </h5>
                    <h5> hello </h5>
                    <h5> hello </h5>
                </div>
        </div>
.first{
width: 50%;
float: left;
background: yellow;
}
.second{
margin-left: 50%;
background: grey;
}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Sreehari Avikkal
  • 305
  • 2
  • 15

3 Answers3

1

CSS Flexible Box Layout

.d-flex {
  display: flex;
}
.first, .second {
  flex: 1 1 auto;
}
.first {
  background-color: yellow;
}
.second {
  background-color: grey;
}
<div class="d-flex">
  <div class="first">
    Left Div
  </div>
  <div class="second">
    <h5> hello </h5>
    <h5> hello </h5>
    <h5> hello </h5>
  </div>
</div>
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
1

You just need to apply flex property to your divs

.first {
  flex: 1;
  background: yellow;
}

.second {
  flex: 1;
  background: grey;
}
<div style="width: 100%;display:flex;">
  <div class="first">Left Div </div>
  <div class="second">
    <h5> hello </h5>
    <h5> hello </h5>
    <h5> hello </h5>
  </div>
</div>
Diwyansh
  • 2,961
  • 1
  • 7
  • 11
0

The hard way is by using JavaScript. Try something like:

document.querySelector('.first').style.height = document.querySelector('.second').clientHeight + 'px';

The problem with this code is that you need to apply it every time the screen resized.

Second solution is by using "flex" instead of "float"

.first {
            width: 50%;
            background: yellow;
        }

        .second {
            width: 50%;
            background: grey;
        }

        .parent {
            display: flex;
        }
<div class="parent" style="width: 100%;">
        <div class="first">
            Left Div
        </div>
        <div class="second">
            <h5> hello </h5>
            <h5> hello </h5>
            <h5> hello </h5>
        </div>
    </div>

Or using "flex" instead of width.

Alaa
  • 225
  • 1
  • 4