footer置于頁面最底部的方法

首先你需要判斷你的footer是固定高度還是任意高度的,因?yàn)槎叩姆椒ㄓ兴鶇s別,

現(xiàn)在先來說情形一,footer高度是固定的。有兩種方法:

方法一:footer高度固定+絕對定位

css樣式

html{height:100%;}

body{min-height:100%;margin:0;padding:0;position:relative;}

.header{background-color: #ffe4c4;}

.main{padding-bottom:100px;background-color: #bdb76b;}/*main的padding-bottom值要等于或大于footer的height*/

.footer{position:absolute;bottom:0;width:100%;height:100px;background-color: #ffc0cb;}? ? ? ??

html代碼

<div class="header"></div>

<div class="main"></div>

<div class="footer"></div>

方法二:footer高度固定+margin負(fù)值

css樣式

html,body{height:100%;margin:0;padding:0;}

.container{min-height:100%;}

.header{background-color: #ffe4c4;}

.main{padding-bottom:100px;background-color: #bdb76b;}/*main的padding-bottom值要等于或大于footer的height值*/.footer{height:100px;margin-top:-100px;background-color: #ffc0cb;}/*margin-top(負(fù)值的)高度等于footer的height值*/

html代碼

<div class="container">

? ? ?<div class="header"></div>

? ? ?<div class="main"></div>

</div>

<div class="footer"></div>

如果footer高度任意,上面兩種方法就失效了,要用下面的方法

方法三:footer高度任意+js

css樣式

/*動態(tài)為footer添加類fixed-bottom*/.fixed-bottom {position:fixed;bottom:0;width:100%;}

<script type="text/javascript">

$(function(){

function footerPosition(){

$("footer").removeClass(".fixed-bottom");

? ? var contentHeight=document.body.scrollHeight,//網(wǎng)頁正文高度

? ? ? ? ? winHeight=window.innerHeight;// 可是窗口高度,不包括瀏覽器頂部導(dǎo)航欄

if(!(contentHeight>winHeight)){

//當(dāng)網(wǎng)頁正文高度小于可是窗口高度時,為footer添加類fixed-bottom

$("footer").addClass("fixed-bottom");

}else{

$(footer).removeClass("fixed-bottom");

}

}

footerPosition();

$(window).resize(footerPosition);

});

</script>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 不會用代碼框,所以看著有些亂套,,,,html部分 <!DOCTYPE html> 迅雷看看 ...
    這就是個帥氣的名字閱讀 1,891評論 0 0
  • 1. tab列表折疊效果 html: 能源系統(tǒng)事業(yè)部 崗位名稱: 工作地點(diǎn) 崗位名...
    lilyping閱讀 2,007評論 0 1
  • 各種純css圖標(biāo) CSS3可以實(shí)現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,977評論 0 8
  • 當(dāng)天真無畏的你 在愛情的磨練里 退去了鋒芒 是否是成長 你努力挽回卻被拒絕的愛情 已經(jīng)在那一刻就已經(jīng)被撕碎了 愛你...
    guanyans閱讀 332評論 0 0
  • 品嘗了四十八年人世界的滋滋味味,酸酸甜甜,苦苦辣辣,唯獨(dú)沒有深刻的體會到真正的快樂是什么。鳳凰涅槃,死即是生,開始...
    段舒琴閱讀 296評論 0 0

友情鏈接更多精彩內(nèi)容