强制嵌套div的最小高度为100%? [英] Force nested divs to have min-height of 100%?
问题描述
我知道 min-height:100%
只有在父元素具有某个数值时才会占用其父元素的高度的至少100%高度,但如果我有几个嵌套的div,我想要他们都有100%的最小高度?我试过 min-height:inherit
但是没有工作?我知道我可以解决这个问题的JavaScript通过简单地检查浏览器高度值文档加载,然后分配到我的嵌套div的min-height属性,但我想知道是否可能解决这个问题只需要css?
I know that min-height: 100%
will only work to take up a minimum of 100% of its parent element's height if the parent element has some numeric value for height, but what if I have a few nested divs and I want them all to have a min-height of 100%? I tried min-height:inherit
but that didn't work either? I know I can probably solve this problem with JavaScript by simply checking the browser height value on document load and then assigning that to the min-height property of my nested divs, but I'd like to know if it would be possible to solve this with just css?
编辑:我还需要提到我需要我的最外层和我的嵌套div都有一个最小高度
Edit: I should also mention that I need my outer most div and my nested divs all to have a min-height of 100% such that they take up at least the height of the browser, but expand if needed.
推荐答案
min-height:inherit;
应该可以正常工作: http://jsfiddle.net/ugxbs/
min-height: inherit;
should work: http://jsfiddle.net/ugxbs/
EDIT
对于百分比值和预期行为,后嵌套 min-height。你应该做的是对所有父母使用 height
属性,然后添加min-height到最内层 DIV
。
As for percentage values and the expected behavior, there is no logic behind nested min-height. What you should do is to use the height
property for all parents, then add min-height to the inner most DIV
.
F.ex:
<html>
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
</html>
CSS:
html, body, .outer { height: 100% }
.inner { min-height: 100%; }
这样,您就可以告诉浏览器从顶部设置所有外部元素( HTML
)到100%的高度。这将使这些元素跨浏览器高度。然后只需在包含内容的最内层元素中添加 min-height
。
This way, you are telling the browser to set all outer elements from the top (HTML
) to a height of 100%. This will make these elements stretch across the browser height. Then just add a min-height
to the inner most element that contains the content.
设置 height
并不意味着它的孩子的内容过多,除非你添加 overflow:hidden;
。
Setting a height
doesn’t mean that it’s children’s excessive content will fall out, unless you add overflow:hidden;
.
这篇关于强制嵌套div的最小高度为100%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!