html - 如果兄弟容器溢出,则使子容器继承父容器高度

标签 html css

我创建了一个外部容器,其中至少有两个子容器。这 2 个容器之一(内部容器 1)的内容使其在其高度溢出。由此,外容器也在增长。

第二个内部容器继承了外部容器的高度,但它只是检索设置为某个值的外部容器的高度。

如何让第二个容器增长到外容器的全高?

提示:我觉得这根本不可能。

这里是一个片段:

* {
  box-sizing: border-box;
}

.padding {
  padding: 10px;
}

.outer-container {
  height: 400px;
  width: 100%;
  background-color: yellow;
  overflow-y: scroll;
}

.overflowing-container {
  width: 20%;
  display: inline-block;
  background-color: salmon;
  overflow-y: visible;
}

.inherit-height-container {
  height: inherit;
  width: 70%;
  display: inline-block;
  background-color: salmon;
  vertical-align: top;
}

.large-element {
  height: 250px;
  width: 100%;
  margin-top: 5px;
  background-color: lightsalmon;
}
<div class="outer-container padding">
  <div class="overflowing-container padding">
    <div class="large-element"></div>
    <div class="large-element"></div>
    <div class="large-element"></div>
  </div>
  <div class="inherit-height-container"></div>
</div>

最佳答案

如果你可以编辑你的 html,你可以只添加一个内部包装器并使其灵活:

* {
  box-sizing: border-box;
}

.padding {
  padding: 10px;
}

.outer-container {
  height: 400px;
  width: 100%;
  background-color: yellow;
  overflow-y: auto;
}

.inner-container {
  display: flex;
}

.overflowing-container {
  width: 20%;
  display: inline-block;
  background-color: salmon;
  margin-right: 5px;       /* not sure if you want this gap */
}

.inherit-height-container {
  /* remove height from this */
  width: 70%;
  display: inline-block;
  background-color: salmon;
  vertical-align: top;
}

.large-element {
  height: 250px;
  width: 100%;
  margin-top: 5px;
  background-color: lightsalmon;
}
<div class="outer-container padding">
  <div class="inner-container">
    <div class="overflowing-container padding">
      <div class="large-element"></div>
      <div class="large-element"></div>
      <div class="large-element"></div>
    </div>
    <div class="inherit-height-container"></div>
  </div>
</div>

关于html - 如果兄弟容器溢出,则使子容器继承父容器高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53629215/

相关文章:

html - 即使我使用元视口(viewport),网站 View 也很小

html - 将边框图像底部放置到 div

html - html(css)中图像拼贴上方的容器

javascript - 使用粘性 div 平滑滚动

html - aria-hidden 是否需要一个值?

html - 芯片关闭按钮不会遵守 css

相等字符 "="的 HTML 字符实体

javascript - 滚动缩小 slider

javascript - Ajax javascript 在按钮下显示消息

html - Bootstrap 网格溢出图像