Make two DIVs the same height? #2

2019-08-24 18:46发布

问题:

I made this topic earlier and a solution has been suggested, but for some reason it doesn't work and I hope to find some further help.

I want to make two DIVs the same height (so when one div becomes bigger in height, the other one should do the same).

This is my HTML:

<div class="aProductHeader">
    <div class="omschrijving">
        <h3>omschrijving</h3>
    </div>
</div>
<div class="aProduct">
    <div class="omschrijving">
        <span class="entry inner">
            Toddler bestek blauw
        </span>
    </div>
</div>

I would like .aProductHeader .omschrijving and .aProduct .omschrijving to keep the same height.

This is my current CSS:

.aProductHeader .omschrijving {
    background: #cac;
    min-height: 30px;
}

.aProduct .omschrijving {
    background: #cec;
    min-height: 30px;
}

And this is the solution which has been suggested to me, I put this before the :

<script>

var one = document.getElementsByClassName(".aProductHeader .omschrijving"),
    two = document.getElementsByClassName(".aProduct .omschrijving");

for (i = 0; i < one.length; i++)
{
  if (one[i].offsetHeight > two[i].offsetHeight)
  {
    two[i].style.height = one[i].offsetHeight+"px";
  }else{
    one[i].style.height = two[i].offsetHeight+"px";
  }
}

</script>

However, the result I am still getting is this:

Any ideas on how to do this?

回答1:

Is this what you are trying to do?

I tried using display:table & display: table-cell;.

CSS

#kasticketProducts{
    overflow:hidden;
    width:250px; /* for testing only */
    display: table;
}
.aProductHeader{
    display: table-cell; 
    background: #cac;
}

.aProduct{
    background: #cec;
    display: table-cell;     
}

Check this blog for more info.



回答2:

You can try this... Actually header has margin so write CSS for h3 as

h3 {
margin: 0;
}

If above is working then try below structure..

<div>
<div class="aProductHeader">
    <div class="omschrijving">
        <h3>omschrijving</h3>
    </div>
</div>
<div class="aProduct">
    <div class="omschrijving">
        <span class="entry inner">
            Toddler bestek blauw
        </span>
    </div>
</div>
</div>

and add this CSS to your CSS

.aProductHeader .omschrijving {
    background: #cac;
    min-height: 30px;
}
.aProductHeader {
    float: left;
}
.aProduct {
    overflow: hidden;
}
.aProduct .omschrijving {
    background: #cec;
    min-height: 30px;
}
h3 {
    margin: 0;
}

working example http://jsfiddle.net/e6ba3/



回答3:

.aProduct{background-color: #FFFFFF;
border-left: 1px solid #E6E6E6;
border-right: 1px solid #E6E6E6;
position: relative;} .aProductHeader{border-top: 1px solid #CA542B;
float: left;
min-height: 65px;
padding-bottom: 4px;
padding-top: 4px;
position: relative;
text-shadow: 1px 1px 1px #000000;
width: 70px;
z-index: 2;}.wrap{border-radius: 0 0 0 5px;
bottom: 0;
left: 0;
margin: 0;
padding-right: 1px;
position: absolute;
top: 0;
width: 70px;
z-index: 1;} 

Wtrite your html
  <div class="wrapper" style="position:realtive;border:1px solid red;"> 
    <div class="wrap"></div>
    <div calss="aProductHeader">xxxxx</div><div class="aProduct">xxx</div>
  </div>


回答4:

No need to write Javascript/jQuery, you can achieve this through CSS.

It will be flexible, and keep both div height same.

Here is Code:

HTML:

<div class="main">
    <div class="aProductHeader">
        <div class="omschrijving">
            <h3>omschrijving</h3>
        </div>
    </div>
    <div class="aProduct">
        <div class="omschrijving">
            <span class="entry inner">
                Toddler bestek blauw
            </span>
            <span class="entry inner">
                Toddler bestek blauw
            </span>
            <span class="entry inner">
                Toddler bestek blauw
            </span>
            <span class="entry inner">
                Toddler bestek blauw
            </span>
            <span class="entry inner">
                Toddler bestek blauw
            </span>
            <span class="entry inner">
                Toddler bestek blauw
            </span>
        </div>
    </div>
</div>

CSS:

.main { position:relative; display:table;}
.aProductHeader .omschrijving {
    background: #cac;
    min-height: 30px;
    float:left;
    width:150px;
    position:absolute; left:0; top:0; bottom:0;
}

.aProduct .omschrijving {
    background: #cec;
    min-height: 30px;
    float:left;
    width:200px;
    margin-left:160px;
}

Enjoy !!!