html - 容器 : one shrinks to fit its content, 中的 2 个堆叠 div,另一个扩展以填充剩余空间

标签 html css

这是一个jsfiddle包含我正在尝试做的基本 html 和 css。

这是我的 html:

#mother {
  width: 100%;
  height: 100vh;
  background-color: white;
}

#header,
#footer {
  width: 100%;
  height: 20px;
  background-color: pink;
}

#title {
  width: 100%;
  background-color: grey;
}

#main {
  width: 100%;
  background-color: cyan;
}
<div id="mother">
  <div id="header">
  </div>
  <div id="title">
    Title content of indeterminate length
  </div>
  <div id="main">
    Main body content of indeterminate length
  </div>
  <div id="footer">
  </div>
</div>

我正在尝试创建一个将填充 100% 窗口高度的网站。

布局需要由 4 个部分组成: - 标题 - 标题 - 主要的 - 页脚

页眉和页脚具有固定的高度。标题和主要内容的长度未知。

我的理想行为是标题容器缩小以适合其内容,主容器扩展以填充页脚之前的剩余可用空间。主容器内将有 div,其大小需要调整为其高度的 100%。

使用绝对定位的元素或表格是可以接受的,只要它有 IE 支持(只需要支持最新版本,但支持旧版本就很棒了)。

最佳答案

尝试使用 Flexbox。 flex:1 意味着它将尽可能多地填充空间。因此,不使用 flex:1 保留标题可以使其缩小以适合其内容。

此外,不要忘记使用浏览器支持所需的所有前缀。

http://shouldiprefix.com/

这是 jsfiddle

html, body{

  height:100%;

}

#mother {
  width: 100%;
  height: 100vh;
  background-color: white;
  display: -webkit-box;  /* OLD - iOS 6-, Safari 3.1-6, BB7 */
  display: -ms-flexbox;  /* TWEENER - IE 10 */
  display: -webkit-flex; /* NEW - Safari 6.1+. iOS 7.1+, BB10 */
  display: flex;         /* NEW, Spec - Firefox, Chrome, Opera */
  flex-direction:column;
}

#header, #footer {
  width: 100%;
  height: 20px;
  background-color: pink;
}

#title {
  width: 100%;
  background-color: grey;
}

#main {
  width: 100%;
  background-color: cyan;
  flex:1;
}

关于html - 容器 : one shrinks to fit its content, 中的 2 个堆叠 div,另一个扩展以填充剩余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903878/

相关文章:

javascript - Angular 路由漂亮 URL 错误

javascript - 单击更改 DIV 位置并为 jQuery 设置动画

html - 旋转图像不平滑动画

html - 下拉菜单的问题

javascript - 使用 AJAX 向 PHP Web 服务提交基本表单数据

html - 在 css 中乘法的第 n 个 child

javascript - 同时调整多个元素的大小

javascript - 在屏幕上查找行的位置

html - 根据文本长度设置div背景颜色

CSS 高度计算不加起来