你正在尋找一個固定的邊界或動態的邊界?您的代碼存在的問題是W3C盒式模型。在默認模型中,填充,邊距和邊框被添加到元素的大小。所以在你的代碼中,你真正告訴它的是「使框100%,然後添加10px的邊框」。
通常一個簡單的變化是手動開關盒模型,但不幸的屬性不與height: 100%
發揮好。所以,你有幾種選擇:
1)如果你正在尋找一個固定的邊界,這是一個好招:http://css-tricks.com/body-border/ 2)如果你需要一個動態的邊界,你需要周圍的額外高度邊界以某種方式得到補充道。這裏有一種方法:
html,body { height:100%; padding: 0; margin: 0; }
#container {
min-height:100%;
border-right: 5px solid #000;
border-left: 5px solid #000;
position: relative; /* relative postion so we can absolutely position footer within it */
}
#header {
height: 100px;
border-top: 5px solid #000;
background-color: red;
}
#content { padding: 0 0 100px 0; } /*padding must be equal to the height of the footer*/
#footer {
height: 100px;
border-bottom: 5px solid #000;
background-color: blue;
position: absolute;
bottom: 0;
width: 100%; /* with absolute position, a width must be declared */
}
HTML
<div id="container">
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
</div>
的jsfiddle這裏:http://jsfiddle.net/Qw2cb/
是的,這在2012年不存在了。很好的回答tho –
的確,我知道。認爲這可能對別人有幫助! – daneczech