html - 填充窗口的剩余高度

标签 html css flexbox

<分区>

我知道这个问题之前已经被问过很多次了,但我似乎没有把我的例子说对。如何让紫色的 div 填充窗口的剩余高度?

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            height: 100%;
            margin: 0;
        }

        .container {
            display: flex;
            flex-flow: column;
            height: 100%;
        }

        .topbox {
            background-color: red;
            flex: 0 1 40px;
        }

        .bottombox {
            background-color: purple;
            flex: 1 1 auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="topbox"></div>
        <div class="bottombox"></div>
    </div>
</body>
</html>

最佳答案

在 html 标签上添加 100% 高度。

html  {
    height: 100%;
}

<!DOCTYPE html>
<html>

<head>
  <style>
    html {
      height: 100%;
    }
    
    body {
      height: 100%;
      margin: 0;
    }
    
    .container {
      display: flex;
      flex-flow: column;
      height: 100%;
    }
    
    .topbox {
      background-color: red;
      flex: 0 1 40px;
    }
    
    .bottombox {
      background-color: purple;
      flex: 1 1 auto;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="topbox"></div>
    <div class="bottombox"></div>
  </div>
</body>

</html>

关于html - 填充窗口的剩余高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678664/

上一篇:javascript - HTML结束后的空白

下一篇:javascript - 如何通过单击图像隐藏倒三 Angular 形?

相关文章:

html - 如何在跨度中居中文本?

html - "@media only screen and ()"css 未覆盖

javascript - 在触摸最上面的 div 时选择它下面的 div 的内容 - z-index 属性

css - 如何将两个 div 保持在同一行上?

css - 指定 flexbox cross size grow 值

html - 将 html 表单与 paypal 集成

javascript - 每个对象的触发事件

html - 如何仅使用 CSS 在未知高度的表头中垂直居中排序图标

css - flexbox 容器内灵活的图像大小

css - Flexbox 元素不会到达容器的底部?