html - 高度 100% flex

标签 html css flexbox

我想让内容 div 适合 100% 的高度。由于某种原因,容器 div 恰好是一个 flex 元素。将 100% 高度设置为 flex 元素中的 div 是否合适?或者我也应该将内容 div 设置为 flex 元素。

此外,flex-direction 部分令人困惑。列不起作用,但行可以。我想 flex-direction 只影响 flex 元素。

jsfiddle here

<div class="wrapper">
  <div class="container">
    <div class="content">
      Hello there
    </div>
  </div>
</div>


html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

.wrapper {
  border: 1px solid red;
  padding: 20px;
  min-height: 100%;
  display: flex;
  /* change flex-direction from column to row will work */
  flex-direction: column;
}

.container {
  border: 1px solid green;
  padding: 20px;
  flex: 1;
}

.content {
  border: 1px solid blue;
  height: 100%;
}

最佳答案

你可以重叠(嵌套) flex 盒子:

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

.wrapper {
  border: 1px solid red;
  padding: 20px;
  min-height: 100%;
  display: flex;
  /* change flex-direction from column to row will work */
  flex-direction: column;
  box-sizing:border-box;
}

.container {
  display:flex;
  flex-direction: column;/* up to your needs */
  border: 1px solid green;
  padding: 20px;
  flex: 1;
}

.content {
  border: 1px solid blue;
  flex:1;
}
<div class="wrapper">
  <div class="container">
    <div class="content">
      Hello there
    </div>
  </div>
</div>

您可能还介意 box-sizing 属性以在尺寸计算中包含边框和填充。

关于html - 高度 100% flex ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48712511/

相关文章:

php - 评级脚本问题

css - 寻找什么颜色)搭配

html - 纯 HTML 和 CSS 选项卡在 Firefox 中显示不正确

html - Flexbox 中的等高图像

html - 如何使 html 表格故意超出其容器的宽度?

javascript - 单选按钮未切换

javascript - 将多个值传递到另一页

javascript - 如果媒体宽度 < x,则更改 HTML

html - 什么工具可以分析我的网站并报告未使用/不需要的 CSS?

html - 放置在其他容器内时对齐中断