html - 从内部div继承高度到外部div

标签 html css

我有一个包含以下内容的 div。

<div class="outsideDiv">
  <div>
    <p>Some text</p>
  </div>

  <div class="imgContainer">
    <img />
  </div>
</div>

imgContainer 位于包含 p 的 div 右侧

outsideDiv 没有预定义的高度。它调整到 p 元素的高度。因此,如果 p 元素的高度为 500px,则外部 div 的高度将为 500px。我的网站是响应式的,因此 p 的高度(以及 outsideDiv 的高度)不是固定的,它会根据屏幕而变化。这部分工作完美。

我希望 imgContainer 的高度与 outsideDiv 的高度相等。 我已经尝试过使用绝对位置和相对位置并且它有效但我不想使用这种方式。我也试过继承高度但没有运气。

最佳答案

.imgContainer {
  background: rgba(0,0,0,.5);
  position: absolute;
  width:100%;
  height:100%;
  bottom: -100%;
}
.outsideDiv {
  position: relative;
}
<div class="outsideDiv">
  <div>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p><p>Some text</p><p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    <p>Some text</p>
    
  </div>

  <div class="imgContainer">
    <img />
  </div>
</div>

关于html - 从内部div继承高度到外部div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373458/

相关文章:

html - Bootstrap,使用粘性页脚垂直和水平居中图像

javascript - 包含 50 个以上单词的选择框

jquery - 使用 jquery 将 div 的选择变成网格

javascript - 试图获取元素的占位符属性的样式

php - 更改 Wordpress 中动态内容的边框颜色

html - 将 html 链接的可点击区域扩展到包含其他内容的包装 li 元素的大小

html - Sendgrid 电子邮件在 Gmail 和 Microsoft Outlook 上的呈现方式不同

javascript - ng-repeat 中的按钮每秒更改 10 次,导致其不可点击

jquery - 宽度 : calc(auto + 50px); doesn't work

javascript - 重新加载下拉框内容?