【问题标题】:Make two DIVs the same height? #2使两个 DIV 高度相同? #2
【发布时间】:2014-05-19 17:06:19
【问题描述】:

我之前提出过这个话题,有人提出了解决方案,但由于某种原因它不起作用,我希望能找到进一步的帮助。

我想让两个 DIV 的高度相同(所以当一个 div 的高度变大时,另一个也应该这样做)。

这是我的 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>

我希望.aProductHeader .omschrijving.aProduct .omschrijving 保持相同的高度。

这是我当前的 CSS:

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

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

这是向我建议的解决方案,我把它放在前面:

<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>

但是,我仍然得到的结果是这样的:

关于如何做到这一点的任何想法?

【问题讨论】:

  • 你为什么不把两者都放在一个班级里呢?并使它们的相对高度和高度均为 100%?
  • 您只能使用CSS 执行此操作。 DEMO - 尝试使用display: table;
  • 尝试了这两种解决方案,但当我应用它们时,它们都没有做任何事情。 :s
  • @2339870 好吧,通过我的演示,您可以看到它有效。提供更多相关代码,我会看看。制作一个 JSFiddle。

标签: javascript html css height


【解决方案1】:

this 你想做什么?

我尝试使用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;     
}

查看此blog 了解更多信息。

【讨论】:

    【解决方案2】:

    你可以试试这个... 实际上 header 有边距,所以将 h3 的 CSS 写为

    h3 {
    margin: 0;
    }
    

    如果上面的工作,然后尝试下面的结构..

    <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>
    

    并将这个 CSS 添加到你的 CSS 中

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

    工作示例 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】:

        无需编写Javascript/jQuery,通过CSS即可实现。

        它将是灵活的,并保持两个 div 高度相同。

        这是代码:

        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;
        }
        

        享受!!!

        【讨论】:

        • 这是什么鬼?
        • 看起来您只是将大量随机的CSS 扔在一起直到它起作用。 (我不知道这是否可行,因为我没有尝试过,也许做一个演示)。但我可以告诉你,你不知道你在做什么。 position: absolutefloat: left;margin-left: 160px; 这毫无意义,如果您不知道自己在做什么,请不要提交答案。
        • 查看这个 Fiddle (jsfiddle.net/M8Kjz),我已经在我的一个项目中实现了这项技术。 link
        • 什么小提琴?如果你使用它也没关系。这是一个糟糕的解决方案,一半的CSS 不需要在那里。刚看了,是的,不要用这个。不需要绝对定位。
        • 请看DEMO - 这是一个更好的解决方案。还有其他方法,但我不建议使用您的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-08
        • 2014-11-05
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-22
        相关资源
        最近更新 更多