html - 高度 100%,内部高度 100% 被前面的 sibling 插入

标签 html css

已经试过了

但是“带继承的表”解决方案对我不起作用

基本上,我要做的是填充高度也为 100% 的 parent 的可用高度;问题是我不想做溢出隐藏并且 calc() 不是一个选项,因为前面的 sibling 可能有动态内容

这是我正在尝试做的事情的代码笔 https://codepen.io/arnotes/pen/PXMojN

<!DOCTYPE html>
<html>

<head>
    <style>
        * {
            box-sizing: border-box;
        }

        .h100 {
            height: 100%;
        }
    </style>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>
</head>

<body style="box-sizing: border-box; height: 100vh !important; margin: 0px; padding: 2em">
    <button>test button</button>
    <br>
    <button>test button</button>
    <br>
    <button>test button</button>
    <div class="h100" style="background-color: red;padding: 2em">
        <button>test button</button>
        <br>
        <button>test button</button>
        <br>
        <button>test button</button>
        <div class="h100" style="background-color:blue;padding: 2em">
            <button>test button</button>
            <br>
            <button>test button</button>
            <br>
            <button>test button</button>
            <div class="h100" style="background-color:yellow">
                test yellow
            </div>
        </div>
    </div>
</body>

</html>

最佳答案

我认为最简单的解决方案是使用嵌套的 flex 容器,您可以在其中依靠 flex-grow:1 让您的元素填充剩余空间:

* {
  box-sizing: border-box;
}

.h100 {
  flex-grow:1; /*fill the remaining space*/
  display: flex;
  flex-direction: column;
  align-items: flex-start; /*avoid stretch effect on button*/
  width:100%; /*make the div 100%*/
}

body {
  margin: 0;
  height: calc(100vh - 4em); /*Here you need calc to remove padding*/
  padding: 2em;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

button {
  flex-shrink:0; /*avoid button shrinking*/
}
<body style="height: 100vh !important; margin: 0px; padding: 2em">
  <button>test button</button>
  <button>test button</button>
  <button>test button</button>
  <div class="h100" style="background-color: red;padding: 2em">
    <button>test button</button>
    <button>test button</button>
    <button>test button</button>
    <div class="h100" style="background-color:blue;padding: 2em">
      <button>test button</button>
      <button>test button</button>
      <button>test button</button>
      <div class="h100" style="background-color:yellow">
        test yellow
      </div>
    </div>
  </div>

关于html - 高度 100%,内部高度 100% 被前面的 sibling 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54263593/

相关文章:

javascript - Bootstrap - 日期时间选择器添加像今天这样的自定义按钮

html - CSS:自定义复选框不起作用

javascript - 使用 CSS 排列元素

html - 制作自定义输入文本类型

html - 带有 2 个响应列的 Bootstrap 4 流体 Flex 行

css - 自定义字体不适用于移动设备

html - 与导航栏位于同一行的 FontAwesome 图标

javascript - 从另一个获取 css 高度值

javascript - 将文档内 SVG 栅格化为 Canvas

html - 如何在html中的标题框架上设置搜索输入框