【问题标题】:Set height of a div to another div height [duplicate]将一个div的高度设置为另一个div的高度[重复]
【发布时间】:2020-07-25 06:33:32
【问题描述】:

我有一个这样的页面:

<div id="parent">
    <div id="childA">
        List of some contents
    </div>
    <div id="childB">
        The main content
    </div>
</div>


#childA{
    display: inline-block;
}
#childB{
    display: inline-block;
}

我想将父 div 的高度设置为 childB 的高度,而不是 childA。 如果 childA 作为滚动条,以防它变得比 childB 大。 编辑:我刚刚意识到我可以从不同的角度解决这个问题。我希望它可以帮助你理解我想要什么。 我需要根据它旁边的div(childB)的高度来设置一个div(childA)的高度。

#childB{
    overflow-y: scroll;
}

两个孩子的身高各不相同,但我希望我父母的身高等于 childB 的身高。 我尝试使用表:

#parent{
    display: table;
}
#childB{
    display: table-row;
}

但是当childA大于childB时,childA会不断增加父母的身高

附:我知道我可以使用 JavaScript,但我不想使用它。

【问题讨论】:

  • 父元素的高度将是两个子元素的高度之和。我看不出为什么您希望父级的高度等于第二个元素的高度。此外,如果您希望父母的高度等于第二个孩子的高度(如果您成功,这将导致第二个孩子流出父母)。为什么不将第一个孩子移出父母?
  • 我想让我的 div 彼此相邻(这就是我使用 display: inline-block; 的原因)

标签: html css height


【解决方案1】:

我希望下面的示例能让您了解在 childA 上设置一些 height 和 overflow-y 有何帮助。

#parent {
  display: flex;
  border: 1px solid black;
}

#childA {
  overflow-y: scroll;
  height: 60px;
}

#childB {

}
<div id="parent">
  <div id="childA">
    List of some contents
    List of some contents
    List of some contents
    List of some contents
    List of some contents
  </div>
  <div id="childB">
    The main content
    The main content
    The main contentThe main contentThe main content
    The main content
    The main content
    The main content
    The main content
  </div>
</div>

【讨论】:

  • 当然可以,但我不想为 childA 设置高度值。只是防止它比childB大
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2014-05-22
  • 2011-03-17
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
相关资源
最近更新 更多