css把底部footer固定在頁面底部

來源:互聯網
上載者:User

看完這篇文章後決定用第三個方法,其實很想用jq的,方便靈活,諮詢了好多老前輩後,得知Js控制DOM在效能上影響比較大,能用css和html解決就不要用js了。
HTML

 代碼如下 複製代碼

<div id="container">
<div>.........</div>
&nbsp;
<div class="push"><!-- not put anything here --></div>
</div>
&nbsp;
<div id="footer">Footer Section</div>
CSS

html,
body{
    height: 100%;
    margin:0;
    padding:0;
}
#container {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -330px;/*margin-bottom的負值等於footer高度*/
}
.push,
#footer {
    height: 330px;
    clear:both;
}

html和body標籤:html,body標籤和前兩種方法一樣,需要設定“height:100%”並重設“margin”和“padding”為0;
div#container:方法三關鍵區段就在於div#container的設定,首先需要設定其最小高度(min-height)為100%,為了能相容好IE6,需要對min-height進行相容處理(具體處理方法看前面或代碼)另外這裡還有一個關鍵點在div#container容器上需要設定一個margin-bottom,並且給其取負值,而且值的大小等於div#footer和div.push的高度,如果div#footer和div.push設定了padding和border值,那麼div#container的margin-bottom負值需要加上div#footer和div.push的padding和border值。也就是說“div#container{margin-bottom:-[div#footer的height+padding+border]或者-[div.push的height+padding+border]}”。一句話說:div#container的margin-bottom負值需要和div#footer以及div.push的高度一致(如果有padding或border時,高度值需要加上他們);
div.push:在div.push中我們不應該放置任何內容,而且這個div必須放置在div#container容器中,而且是最底部,並且需要設定其高度值等於div#footer的值,最好加上clear:both來清除浮動。div.push容器在此處所起的作用就是將footer往下推。
div#footer容器:div#footer容器和方法二一樣,不能放到div#container內部,而和div#container容器同級,如果需要設定元素和footer之間的間距,最好使用padding來代替margin值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.