这是因为height
属性的百分比值是相对于包含框的块的高度而言的。因此100%
意味着整个高度。
10.5 Content height: the 'height' property
指定的百分比高度。百分比的计算方法为 尊重所生成框的高度containing block。如果未明确指定包含块的高度 (即,其 取决于内容高度),并且该元素不是绝对 定位,则该值计算为'auto'。 root element上的百分比高度相对于initial containing block。
一个解决办法是使用负余量为第二<div>
元件以除去srcollbar,然后加入position: relative;
到第一个,使其回到在第二一个的顶部。
在这种情况下,我们应该使用padding
第二DIV的顶部,以推动其内容下来,也是为了增加box-sizing: border-box
计算包括填充边界框的高度:
Example Here
<div class="parent">
<div class="top"></div>
<div class="content"></div>
</div>
.parent { height:100%; }
.top {
height: 100px;
position: relative;
}
.content {
background-color: gold;
min-height: 100%;
margin-top: -100px; /* equals to the height of .top element */
padding-top: 100px; /* equals to the height of .top element */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
值得注意的是,这种方法would work on IE8+。
时下所有主流浏览器都支持box-sizing: border-box
,但是你用隔离元件代替padding
+ box-sizing
推.content
下的内容:
Example Here。
<div class="content">
<div class="spacer"></div>
<!-- content goes here -->.
</div>
.spacer, .top {
height: 100px;
}
这种方法将上工作IE 6/7 +(*)
或者,也可以嵌套在.content
内.top
元件和丢弃.parent
在为了达到同样的效果正在处理中IE 6/7 +(*)。
Example Here。
<div class="content">
<div class="top"></div>
<div class="inner">
<!-- content goes here -->
</div>
</div>
(*)IE6 +通过使用height
属性,IE7 +通过使用min-height
。