詳解CSS五種方式實(shí)現(xiàn)Footer置底

頁(yè)腳置底(Sticky footer)就是讓網(wǎng)頁(yè)的footer部分始終在瀏覽器窗口的底部。
當(dāng)網(wǎng)頁(yè)內(nèi)容足夠長(zhǎng)以至超出瀏覽器可視高度時(shí),頁(yè)腳會(huì)隨著內(nèi)容被推到網(wǎng)頁(yè)底部;但如果網(wǎng)頁(yè)內(nèi)容不夠長(zhǎng),置底的頁(yè)腳就會(huì)保持在瀏覽器窗口底部。
方法一:將內(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、這個(gè)方法需要容器里有額外的占位元素(div.push)。
2、div.wrapper的margin-bottom需要和div.footer的-height值一樣,注意是負(fù)height。
方法二:將頁(yè)腳的margin-top設(shè)為負(fù)數(shù)
給內(nèi)容外增加父元素,并讓內(nèi)容部分的padding-bottom與頁(yè)腳的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)容太多則可能會(huì)破壞布局。
<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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- 我們經(jīng)常會(huì)遇到這樣的問(wèn)題:如何用css來(lái)實(shí)現(xiàn)底部元素可“粘住底部”的效果,對(duì)于“粘住底部”,本篇文章就來(lái)介紹一下。非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
利用CSS使footer固定在頁(yè)面底部的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇利用CSS使footer固定在頁(yè)面底部的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-13html的footer置于頁(yè)面最底部的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇html的footer置于頁(yè)面最底部的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-13詳解HTML5將footer置于頁(yè)面最底部的方法(CSS+JS)
這篇文章主要介紹了詳解HTML5將footer置于頁(yè)面最底部的方法(CSS+JS)的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-11