2016-01-13 115 views
0

由于某些原因,最小高度在Firefox上不起作用。我试着在身上设置最小高度,但Firefox完全忽视它。由于我的页面是动态的,因此我不能将高度设置为100%。我该怎么办?Firefox忽略CSS中的最小高度

body { 
 
border: 1px solid black; 
 
width: 100%; 
 
min-height: 100%; 
 
}
<body> 
 

 
This is the body. 
 

 
</body>

+2

“高度”百分比适用于绝对定位的元素。如果你需要它提供百分比,然后将'position:absolute;'添加到'body'风格 –

+0

@SaqibAmin min-height在谷歌浏览器中工作得很好。 – frosty

+0

我只是重新确认了一个空文档的样式,你必须添加'position:absolute;'以便按照你想要的方式工作 –

回答

3

height百分数都继承(即包括min-height and max-height,太)。从CSS规范:

指定用于确定使用的值的百分比。 该百分比是根据生成的包含框的高度来计算的。

大多数人不知道的是,body就像任何其他的元素,所以是html。人们也没有意识到的是这些元素没有固有的高度。 html的父项是视口,它的确实具有100%的固有高度。视口是 - 或多或少 - 浏览器窗口,减去任何菜单或标题栏。

由于height百分比从他们的父母继承,你不必为你的html元素的height一套,你的min-height: 100%; CSS不具有价值,采取100%的。所以你的身体基本上服用0的min-height: 100%

为了解决这个问题,简单地告诉你html元至100%的视口的高度:

html { 
 
    height: 100%; /* this is the important bit */ 
 
} 
 

 
body { 
 
    border: 1px solid black; 
 
    width: 100%; 
 
    min-height: 100%; 
 
    margin: 1px; /* I added this to make the border around the body a little easier to see. Normally you want to set it to 0 or leave it alone completely */ 
 
}
<body> 
 
This is the body. 
 
</body>

但是,如果你不希望你的整个文档设置为与视口一样高(我强烈建议您这样做),您还可以在您的body元素上使用position: absolute;,这样无论其父元素的高度如何,百分比高度都将始终解析。这是Saqib在上述评论中试图得到的结果。从CSS规格上最小高度和高度分别为:

如果未明确指定包含块的高度(即,它取决于内容的高度),并且该元件没有绝对定位,该百分比值被视为'0'('min-height')或'none'('max-height')。

-

注意,绝对定位的元素的包含块的高度独立于元件本身的尺寸,并且因此这样的元件上的百分比高度总是能够得到解决。

body { 
 
    border: 1px solid black; 
 
    width: 100%; 
 
    min-height: 100%; 
 
    position: absolute; 
 
    margin: 1px; /* I added this to make the border around the body a little easier to see. Normally you want to set it to 0 or leave it alone completely */ 
 
}
<body> 
 
This is the body. 
 
</body>

(我不知道你的代码是在Chrome中工作,但在你的问题的代码在Chrome浏览器,因为它不会在Firefox相同的行为。)