【问题标题】:Stretch (vertically) middle div between the other two在其他两个之间拉伸(垂直)中间 div
【发布时间】:2016-04-26 03:18:11
【问题描述】:

有没有办法拉伸中间 div,使其填充其他两个(页眉,页脚)之间的剩余空间,高度有限。这三个都放置在覆盖 100% 视口空间的父元素中。条件是所有三个 div 都必须相对于它的内容,但有限制(max-height)(中间的除外),所以没有绝对定位;它们必须具有相同的父元素。我确实发现虽然类似的解决了problem,但是使用了一个表结构,这不是我要寻找的,但具有我想应用于我的概念的完全相同的行为。

这里是codepen,但我还没有弄清楚如何解决它。还有一点,我不想用JS。

更新 同样不用flexbox,calc(),tables;

【问题讨论】:

  • 在你的 CSS 中有很多东西......如果你的父 div 是 100vh 那么如果你给第一个高度和最后一个高度,我不明白你想要做什么 100%一个高度你只需要给中间的一个 100% 不?
  • @DanyCode 如果我给中间的 100%,它将覆盖视口底部的所有空间,但也会将页脚 div 推离屏幕。
  • 和高度:calc(100% - ...px); ...px 是高度 header+footer
  • @DanyCode 它只适用于初始值。但是如果页面加载后它发生了变化。

标签: html css


【解决方案1】:

CSS 表格(不是实际表格)

body {
  margin: 0;
}
.parent {
  height: 100vh;
  display: table;
  width: 100%;
}
header,
footer {
  max-height: 25vh;
  display: table-row;
  background: lightblue;
}
main {
  height: 100%;
  background: pink;
  display: table-row;
}
<div class="parent">
  <header>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ea delectus quod, odit minus doloremque nesciunt voluptates veniam possimus voluptate sapiente provident, magnam sed, ipsam pariatur.</p>
  </header>
  <main>
    <p>content</p>
  </main>
  <footer>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vel sed eligendi quibusdam voluptatibus consequatur cum, velit illo. Aperiam similique laudantium praesentium veniam repellat consequuntur et!</p>
  </footer>
</div>

Support 至少支持 IE8。

但是,我不能推荐此解决方案,但鉴于 OP 对此设置的异常限制,这是唯一剩下的 CSS 解决方案 AFAIK。

【讨论】:

  • Flexbox 就是为此而构建的。支持IE10及以上,真的不错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2010-10-26
  • 2012-08-01
  • 2011-02-14
  • 2014-03-13
  • 2019-10-01
  • 1970-01-01
相关资源
最近更新 更多