CSS實現(xiàn)Sticky Footer的示例代碼
所謂 “Sticky Footer”,并不是什么新的前端概念和技術,它指的就是一種網(wǎng)頁效果:如果頁面內(nèi)容不足夠長時,頁腳固定在瀏覽器窗口的底部;如果內(nèi)容足夠長時,頁腳固定在頁面的最底部。但如果網(wǎng)頁內(nèi)容不夠長,置底的頁腳就會保持在瀏覽器窗口底部。

實現(xiàn)
方法
1. 將內(nèi)容部分的底部外邊距設為負數(shù)
這是個比較主流的用法,把內(nèi)容部分最小高度設為100%,再利用內(nèi)容部分的負底部外邊距值來達到當高度不滿時,頁腳保持在窗口底部,當高度超出則隨之推出的效果。
<body>
<div class="wrapper">
content
<div class="push"></div>
</div>
<footer class="footer"></footer>
</body>html, body {
height: 100%;
margin: 0;
}
.wrapper {
min-height: 100%;
/* 等于footer的高度 */
margin-bottom: -50px;
}
.footer,
.push {
height: 50px;
}
這個方法需要容器里有額外的占位元素(如.push)
需要注意的是.wrapper的margin-bottom值需要和.footer的負的height值保持一致,這一點不太友好。
2. 將頁腳的頂部外邊距設為負數(shù)
既然能在容器上使用負的margin bottom,那能否使用負margin top嗎?當然可以。
給內(nèi)容外增加父元素,并讓內(nèi)容部分的底部內(nèi)邊距與頁腳高度的值相等。
<body>
<div class="content">
<div class="content-inside">
content
</div>
</div>
<footer class="footer"></footer>
</body>html, body {
height: 100%;
margin: 0;
}
.content {
min-height: 100%;
}
.content-inside {
padding: 20px;
padding-bottom: 50px;
}
.footer {
height: 50px;
margin-top: -50px;
}
不過這種方法和上一種一樣,都需要額外添加不必要的html元素。
3. 使用flexbox彈性盒布局
以上三種方法的footer高度都是固定的,通常來說這不利于網(wǎng)頁布局:內(nèi)容會改變,它們都是彈性的,一旦內(nèi)容超出固定高度就會破壞布局。所以給footer使用flexbox吧,讓它的高度可以變大變小變漂亮~(≧∇≦)
<body>
<div class="content">
content
</div>
<footer class="footer"></footer>
</body>html {
height: 100%;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
你還可以在上面添加header或在下面添加更多元素。可從以下技巧選擇其一:
- flex: 1 使內(nèi)容(如:.content)高度可以自由伸縮
- margin-top: auto
請記住,我們有《Flexbox完整指南(英) 》呢~
4. absolute
通過絕對定位處理應該是常見的方案,只要使得頁腳一直定位在主容器預留占位位置。
<div class="wrapper">
<div class="content"><!-- 頁面主體內(nèi)容區(qū)域 --></div>
<div class="footer"><!-- 需要做到 Sticky Footer 效果的頁腳 --></div>
</div>html, body {
height: 100%;
}
.wrapper {
position: relative;
min-height: 100%;
padding-bottom: 50px;
box-sizing: border-box;
}
.footer {
position: absolute;
bottom: 0;
height: 50px;
}
這個方案需指定 html、body 100% 的高度,且 content 的 padding-bottom 需要與 footer 的 height 一致。
5. calc
通過計算函數(shù) calc 計算(視窗高度 - 頁腳高度)賦予內(nèi)容區(qū)最小高度,不需要任何額外樣式處理,代碼量最少、最簡單。
<div class="wrapper">
<div class="content"><!-- 頁面主體內(nèi)容區(qū)域 --></div>
<div class="footer"><!-- 需要做到 Sticky Footer 效果的頁腳 --></div>
</div>.content {
min-height: calc(100vh - 50px);
}
.footer {
height: 50px;
}
如果不需考慮 calc() 以及 vh 單位的兼容情況,這是個很理想的實現(xiàn)方案。同樣的問題是 footer 的高度值需要與 content 其中的計算值一致。
6. table
通過 table 屬性使得頁面以表格的形態(tài)呈現(xiàn)。
<div class="wrapper">
<div class="content"><!-- 頁面主體內(nèi)容區(qū)域 --></div>
<div class="footer"><!-- 需要做到 Sticky Footer 效果的頁腳 --></div>
</div>html, body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
min-height: 100%;
}
.content {
display: table-row;
height: 100%;
}
需要注意的是,使用 table 方案存在一個比較常見的樣式限制,通常 margin、padding、border 等屬性會不符合預期。筆者不建議使用這個方案。當然,問題也是可以解決的:別把其他樣式寫在 table 上。
7. 使用Grid網(wǎng)格布局
grid比flexbox還要新很多,并且更佳很簡潔,我們同樣有《Grid完整指南(英) 》奉上~
<body>
<div class="content">
content
</div>
<footer class="footer"></footer>
</body>html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: 1fr auto;
}
.footer {
grid-row-start: 2;
grid-row-end: 3;
}
遺憾的是,網(wǎng)格布局(Grid layout)目前僅支持Chrome Canary和Firefox Developer Edition版本。
總結
以上幾種實現(xiàn)方案,筆者都在項目中嘗試過,每個實現(xiàn)的方法其實大同小異,同時也都有自己的利弊。其中有的方案存在限制性問題,需要固定頁腳高度;其中有的方案需要添加額外的元素或者需要 Hack 手段。同學們可以根據(jù)頁面具體需求,選擇最適合的方案。
當然,技術是不斷更新的,也許還有很多不同的、更好的方案。但相信大家最終目都是一樣的,為了更好的用戶體驗!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章

CSS Sticky Footer 幾種實現(xiàn)方式
這篇文章主要介紹了CSS Sticky Footer 幾種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習2019-06-05
這篇文章主要介紹了CSS Sticky Footer實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-05
詳解CSS經(jīng)典布局之Sticky footer布局
這篇文章主要介紹了詳解CSS經(jīng)典布局之Sticky footer布局的相關資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-08
這篇文章主要介紹了詳解Sticky Footer 絕對底部的兩種套路,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-03
css sticky footer經(jīng)典布局的實現(xiàn)
這篇文章主要介紹了css sticky footer經(jīng)典布局的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學2020-03-02






