html - 为什么 100% 不代表 100% 高度?

标签 html css

我正在尝试让一些 div 展开以填满屏幕,但我很吃力。我已经在 this jsfiddle 上分解了这个问题.

我真正想知道的是,为什么 div 的 100% min-height 在其父项时没有扩展到该高度(或根本没有)具有相同的属性并扩展?

<body>
    <div>
        stuff
    </div>
</body>

body {
    min-height: 100%;
    background: red;
}
div {
    min-height: 100%;
    background: grey;
}

最佳答案

CSS 2.1 spec 中涵盖了该问题:

<percentage>

Specifies a percentage height. The percentage is calculated with respect to the height of the generated box's containing block. 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 value computes to 'auto'. A percentage height on the root element is relative to the initial containing block. Note: For absolutely positioned elements whose containing block is based on a block-level element, the percentage is calculated with respect to the height of the padding box of that element. This is a change from CSS1, where the percentage was always calculated with respect to the content box of the parent element.

因此,澄清一下,百分比高度将引用其包含 block 的高度(除非它是 position: absoluteposition: fixed)。如果该包含 block 没有指定的高度,则百分比将引用 auto,它实际上不会做太多事情。

position: absolute 将引用的包含 block 更改为最近的位置(absoluterelativefixed ) 元素。

position: fixed 将引用的包含 block 更改为视口(viewport)。

因此,如果您在包含 block 上指定了一个高度,在包含 block 上指定了一个非静态的位置,或者不介意使用视口(viewport)作为包含 block ,那么您可以有效地使用百分比高度。

请看我的demonstration at jsFiddle

关于html - 为什么 100% 不代表 100% 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7880365/

相关文章:

css - 如何用大花括号设置 block 引用的样式

html - IE 未连接到外部 IE 特定的 CSS 文件

html - 悬停时图像上的文字受滤镜亮度影响

css - 边框悬停在 div 上(CSS)

html - 对齐 LI 标签中的 3 个 div

javascript - 设置 html 输入或文本区域的值

html - 基本 html 上的 Div 相关错误

javascript - 如何表示面包屑 child ?

javascript - HTML CSS 顶部拖动条幻灯片以显示更多信息

css - Safari 中的 Bootstrap 文本框高度问题