當main的高度足夠長的時候,緊跟在<\main>後面的元素<\footer>會跟在其後面;css
當<\main>元素比較短的時候(好比小於屏幕的高度),咱們指望這個<\footer>元素可以「粘連」在屏幕的底部。 html
三個組成部分:wrap容器,main內容,footer腳部bash
//html結構
<body>
<div id="wrap">
<div id="main">
main<br/>
main<br/>
</div>
</div>
<div id="footer"></div>
</body>
//css樣式
html, body {
height: 100%;
margin: 0;
}
#wrap{
width: 100%;
min-height: 100%;
}
/*內容區須要讓出一部分區域,防止內容被蓋住*/
#main{
padding-bottom: 30px;
}
//wrap包裹內容的最小高度是100%,此時將footer的部分經過margin-top拉上去30px。
#footer{
width: 100%;
height: 30px;
background-color: yellow;
margin-top: -30px;
}
複製代碼
<body>
<div class="wrapper">
content
<div class="push"></div>
</div>
<footer class="footer"></footer>
</body>
//css樣式
html, body {
height: 100%;
margin: 0;
}
.wrapper {
min-height: 100%;
margin-bottom: -50px;
}
.footer,
.push {
height: 50px;
}
複製代碼
//html結構
<body>
<div class="content">
content
</div>
<footer class="footer"></footer>
</body>
//css樣式
html {
height: 100%;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
.footer{
height: 50px;
background-color: red;
}
複製代碼