詳解CSS五種方式實現(xiàn)Footer置底
頁腳置底(Sticky footer)就是讓網(wǎng)頁的footer部分始終在瀏覽器窗口的底部。
當(dāng)網(wǎng)頁內(nèi)容足夠長以至超出瀏覽器可視高度時,頁腳會隨著內(nèi)容被推到網(wǎng)頁底部;但如果網(wǎng)頁內(nèi)容不夠長,置底的頁腳就會保持在瀏覽器窗口底部。

方法一:將內(nèi)容部分的margin-bottom設(shè)為負(fù)數(shù)
<div class="wrapper">
<!-- content -->
<div class="push"></div>
</div>
<div class="footer">footer</div>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.wrapper {
min-height: 100%;
margin-bottom: -50px; /* 等于footer的高度 */
}
.footer, .push {
height: 50px;
}
1、這個方法需要容器里有額外的占位元素(div.push)。
2、div.wrapper的margin-bottom需要和div.footer的-height值一樣,注意是負(fù)height。
方法二:將頁腳的margin-top設(shè)為負(fù)數(shù)
給內(nèi)容外增加父元素,并讓內(nèi)容部分的padding-bottom與頁腳的height相等。
<div class="content">
<div class="content-inside">
<!-- content -->
</div>
</div>
<div class="footer">footer</div>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.content {
min-height: 100%;
}
.content-inside {
padding: 20px;
padding-bottom: 50px;
}
.footer {
height: 50px;
margin-top: -50px;
}
方法三:使用calc()設(shè)置內(nèi)容高度
<div class="content"> <!-- content --> </div> <div class="footer">footer</div>
.content {
min-height: calc(100vh - 70px);
}
.footer {
height: 50px;
}
這里假設(shè)div.content和div.footer之間有20px的間距,所以70px=50px+20px
方法四:使用flexbox彈性盒布局
以上三種方法的footer高度都是固定的,如果footer的內(nèi)容太多則可能會破壞布局。
<div class="content"> <!-- content --> </div> <div class="footer">footer</div>
html {
height: 100%;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
方法五:使用Grid網(wǎng)格布局
<div class="content"> <!-- content --> </div> <div class="footer">footer</div>
html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: 1fr auto;
}
.footer {
grid-row-start: 2;
grid-row-end: 3;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- 我們經(jīng)常會遇到這樣的問題:如何用css來實現(xiàn)底部元素可“粘住底部”的效果,對于“粘住底部”,本篇文章就來介紹一下。非常具有實用價值,需要的朋友可以參考下2018-10-10
- 下面小編就為大家?guī)硪黄肅SS使footer固定在頁面底部的實例代碼。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-13
html的footer置于頁面最底部的簡單實現(xiàn)方法
下面小編就為大家?guī)硪黄猦tml的footer置于頁面最底部的簡單實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-13詳解HTML5將footer置于頁面最底部的方法(CSS+JS)
這篇文章主要介紹了詳解HTML5將footer置于頁面最底部的方法(CSS+JS)的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-11

