這是因爲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
。