我不想显式设置 body
的高度并以百分比设置子 div
的高度。
<!doctype html>
<html>
<body>
<div style="height:10%">
fdjfdkjl
</div>
<div style="height:90%">
dsds
</div>
</body>
</html>
但是,这是行不通的。为什么?在规范中有行为描述:
When the percent notation is used height of the element is calculated relatively to height of the parent. If the parent is doesn't set explicitely the browser's window is used.
好的,我可以将 body
设置为 100%,但这也不起作用:它的高度只是包裹了内容。为什么没有使用浏览器的窗口?
最佳答案
正如@MattK 所说,
html, body { height: 100%; }
解决问题。我认为这种方法行之有效,因为 div
和 body
的父级已定义大小。 html
没有父级,使用了窗口的 height
。
关于html - 为没有高度的父元素计算高度时使用百分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12321773/