html - 与顶部 :0px;min-height:100% the div collapses even if the height of the parent is fixed

标签 html css

我在 blink、gecko 和 trident 引擎中测试了它。第一个 div 的呈现高度为零。 如果您添加高度(无论值是多少),绿色子项可见并且最小高度处于事件状态。

<html><head>
<title>min-height and missing height</title>
</head>
<body style="width: 1024px; height: 768px;">
    <div id="outer" style="
min-width: 100%;min-height: 100%;
position:absolute;
left: 0px;top: 0px;
/* height:0; */
">
        <div id="inner" style="background-color:green;height:100%;width:100%"></div>
    </div>

</body>
</html>

Spec

If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the percentage value is treated as '0' (for 'min-height') or 'none' (for 'max-height').

但由于包含 block 的高度是固定的,我认为这部分并没有真正描述崩溃背后的原因。

有人对此有解释(和解决方法)吗? 我可以添加一个假高度:0,但这在我的应用程序中非常昂贵,因为所有尺寸都可以随时更改。

最佳答案

...the div collapses even if the height of the parent is fixed

好吧,在这种情况下,父元素的高度不固定body 具有固定高度,但它不是内部 div 的父元素(它是'祖 parent ')。

此外,内部 div 不是绝对定位的 - 所以 - 正如规范所说:内部 div 的计算高度将为 0。

一个简单的解决方法是将 position:absolute 添加到内部 div。

body {
  width: 1024px;
  height: 768px;
}
.outer {
  min-width: 100%;
  min-height: 100%;
  position: absolute;
  left: 0px;
  top: 0px;
  border: 1px solid green;
  /* height:0; */
}
.inner {
  background-color: green;
  height: 100%;
  width: 100%;
  position: absolute; /* <-- added this */
}
<div class="outer">
  <div class="inner"></div>
</div>

关于html - 与顶部 :0px;min-height:100% the div collapses even if the height of the parent is fixed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47886344/

相关文章:

angularjs - HTML5 原生 DragEnter/DragLeave 事件交替触发

javascript - 使用 jquery 设置样式的随机值

jQuery slideDown/slideUp 问题

javascript - $routeProvider 不工作

jquery - HTML 数值输入字段模式。

jquery mobile data-theme 属性不能到处使用

html - 如何在 CSS 网格系统中偏移 div 列

html - 定位文本和图像

javascript - 生成随机位置后如何设置随机颜色(比如说绿色)

javascript - 使用 jQuery 添加新输入字段时如何计算总数