DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> 前端技巧
如何讓頁腳永遠固定在頁面底部?四種方案完整實作
編輯:前端技巧     

作為一個 Web 前端攻城師,做頁面時肯定碰到過這種現象:當一個 HTML 頁面內容很少時,footer 會隨著一起「飄」上來,停在頁面半腰中間,視覺效果極差。特別是現在寬螢幕越來越多,這種尷尬更是常見。

先釐清需求:我們要的是 footer 永遠固定在頁面的底部,而不是永遠固定在螢幕的底部。換句話說:

  • 內容很少時 → footer 貼在瀏覽器視窗底部(不能懸在半空)。
  • 內容超過視窗高度時 → footer 跟在內容後面,位於頁面最底部。

總結一句:footer 永遠沉底。

以下四種方法,前三種純 CSS,最後一種用 jQuery。

方法一:padding-bottom 保留空間(Matthew James Taylor)

HTML

<div id="container">
  <div id="header">Header Section</div>
  <div id="page" class="clearfix">
    頁面內容部分
  </div>
  <div id="footer">Footer Section</div>
</div>

你可以在 div#page 裡增加更多結構,例如三欄佈局:

<div id="container">
  <div id="header">Header Section</div>
  <div id="page" class="clearfix">
    <div id="left">Left Sidebar</div>
    <div id="content">Main content</div>
    <div id="right">Right sidebar</div>
  </div>
  <div id="footer">Footer Section</div>
</div>

基本上只需要四個 div:#container 是容器,裡面包含 #header(頭部)、#page(主體)、#footer(頁腳)。

CSS

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#container {
  min-height: 100%;
  height: auto !important;
  height: 100%;          /* IE6 不識別 min-height */
  position: relative;
}
#header {
  background: #ff0;
  padding: 10px;
}
#page {
  width: 960px;
  margin: 0 auto;
  padding-bottom: 60px;  /* 等於 footer 的高度 */
}
#footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 60px;          /* 頁腳高度 */
  background: #6cf;
  clear: both;
}
/* ===== 主體內容 ===== */
#left    { width: 220px; float: left;  margin-right: 20px; background: lime; }
#content { width: 480px; float: left;  margin-right: 20px; background: orange; }
#right   { width: 220px; float: right; background: green; }

原理拆解

  1. html / body:必須設 height:100%,這樣容器才能使用百分比高度;同時把 margin 與 padding 歸零。
  2. #container:設 min-height:100%,確保內容很少時仍能撐滿整個視窗。IE6 不支援 min-height,需用 height:auto !important; height:100%; 這個 hack 繞過。另外要設 position:relative,讓內部絕對定位的元素以它為基準。
  3. #page:關鍵是設定 padding-bottom,且值要等於或略大於 footer 的高度,用來預留空間。也可以用 border-bottom-width 代替,但千萬不能用 margin-bottom,否則效果會失效。
  4. #footer:設固定高度,並絕對定位在 bottom:0,這樣它就固定在 container 底部。

優點:結構簡單清晰,無需 JS 與 hack(除 IE6 的 min-height)即可相容各瀏覽器,也適應 iPhone。

缺點:footer 必須設定固定高度,且 #page 的 padding-bottom 要與之一致。

方法二:負 margin-top

HTML

這次 #footer 與 #container 是同輩關係,而非包在內部:

<div id="container">
  <div id="header">Header Section</div>
  <div id="page" class="clearfix">
    <div id="left">Left sidebar</div>
    <div id="content">Main content</div>
    <div id="right">Right sidebar</div>
  </div>
</div>
<div id="footer">Footer section</div>

CSS

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

#container {
  min-height: 100%;
  height: auto !important;
  height: 100%;
}
#page { padding-bottom: 60px; }  /* 等於 footer 高度 */

#footer {
  position: relative;
  margin-top: -60px;             /* 負值,等於 footer 高度 */
  height: 60px;
  clear: both;
  background: #c6f;
}

#header  { padding: 10px; background: lime; }
#left    { width: 18%; float: left; margin-right: 2%; background: orange; }
#content { width: 60%; float: left; margin-right: 2%; background: green; }
#right   { width: 18%; float: left; background: yellow; }

方法一與方法二有三點完全相同:html/body 設 100% 高度並歸零 margin/padding、container 設 min-height 並處理 IE6、page 設 padding-bottom 等於 footer 高度。

不同之處:

  1. footer 放在 container 之外,兩者同級。若你有新元素要放在與 container 同級,必須絕對定位,否則會破壞 container 的 min-height。
  2. footer 使用負的 margin-top,值等於自身高度,並與 page 的 padding-bottom 相等。

優點:結構簡單,無需 JS 與 hack。缺點:footer 高度必須固定,無法自適應。

方法三:push 空元素

HTML

<div id="container">
  <div id="header">Header Section</div>
  <div id="page" class="clearfix">
    <div id="left">Left sidebar</div>
    <div id="content">Main Content</div>
    <div id="right">Right Content</div>
  </div>
  <div class="push"><!-- 不要放任何內容 --></div>
</div>
<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 -60px;   /* margin-bottom 負值等於 footer 高度 */
}
.push, #footer {
  height: 60px;
  clear: both;
}

#header  { padding: 10px; background: lime; }
#left    { width: 18%; float: left; margin-right: 2%; background: orange; }
#content { width: 60%; float: left; margin-right: 2%; background: green; }
#right   { width: 18%; float: left; background: yellow; }
#footer  { background: #f6c; }

原理:container 的 margin-bottom 取負值,值等於 footer 與 push 的高度(若有 padding 或 border 需一併加總)。.push 是一個空元素,放在 container 最底部,用來把 footer 「推」下去。

優點:簡單明瞭,相容所有瀏覽器。缺點:多了一個 .push 元素,同樣限制 footer 高度。

方法四:jQuery 動態計算

HTML

<div id="header">Header Section</div>
<div id="page" class="clearfix">
  <div id="left">Left sidebar</div>
  <div id="content">Main Content</div>
  <div id="right">Right Content</div>
</div>
<div id="footer">Footer Section</div>

沒有任何多餘標籤,只要確保 footer 在 body 最後面。

CSS

* { margin: 0; padding: 0; }

.clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after  { clear: both; }
.clearfix        { zoom: 1; /* IE < 8 */ }

#footer { height: 60px; background: #fc6; width: 100%; }

#header  { padding: 10px; background: lime; }
#left    { width: 18%; float: left; margin-right: 2%; background: orange; }
#content { width: 60%; float: left; margin-right: 2%; background: green; }
#right   { width: 18%; float: left; background: yellow; }

重要:html、body 不可設 height:100%,否則此方法失效。

jQuery

// 用 load 而非 ready,以防視窗高度受圖片影響
$(window).bind("load", function() {
  var footerHeight = 0,
      footerTop = 0,
      $footer = $("#footer");

  positionFooter();

  function positionFooter() {
    footerHeight = $footer.height();
    // footer 距螢幕頂部的距離
    footerTop = ($(window).scrollTop() + $(window).height() - footerHeight) + "px";

    // 若頁面內容高度小於視窗高度,footer 絕對定位到螢幕底部;
    // 否則保留正常的靜態定位
    if ( ($(document.body).height() + footerHeight) < $(window).height() ) {
      $footer.css({ position: "absolute" }).stop().animate({ top: footerTop });
    } else {
      $footer.css({ position: "static" });
    }
  }

  $(window).scroll(positionFooter).resize(positionFooter);
});

使用要點:正確引入 jQuery 並載入這段程式;footer 必須在 body 最後;html/body 不可設 100% 高度。

優點:結構簡單,無需多餘標籤,相容所有瀏覽器,footer 可不固定高度。缺點:不支援 JS 的環境無法顯示,且每次改變視窗大小會閃動一下。

方法五(現代最佳解):Flexbox + margin-top:auto

以上四法都是為了繞過「CSS 無法自動填滿剩餘空間」這個限制而生的。而 flexbox 天生就解決了這個問題,只要三行:

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

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  flex: 1 0 auto;   /* 關鍵:讓主體撐開剩餘空間 */
}

footer {
  flex: 0 0 auto;   /* 高度由內容決定 */
}

HTML 結構乾淨俐落:

<body>
  <header>Header Section</header>
  <main>頁面內容</main>
  <footer>Footer Section</footer>
</body>

這個方案沒有任何缺點:footer 高度可以是 auto(內容多它就高、內容少它就矮)、不需要多餘的 push 元素、不需要負 margin、不需要 JS、不需要 IE hack。

方法六:CSS Grid

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

三行搞定,語義比 flex 更直觀:1fr 就是「剩下的全部空間」。

方案選擇建議

方案footer 高度可自適應需要額外標籤需要 JS推薦度
方法一 padding-bottom否否否歷史方案
方法二 負 margin-top否否否歷史方案
方法三 push 元素否是否歷史方案
方法四 jQuery是否是不推薦
方法五 Flexbox是否否首選
方法六 Grid是否否首選

前三種方法都是同一個思路的不同變體:讓 container 至少佔滿視窗,再用 padding / 負 margin 為 footer 騰出空間。它們在 flexbox 普及前是標準答案,但每個都要求 footer 高度固定,這是繞不掉的限制。

如果你今天要實作這個效果,直接用 flexbox 的 flex:1 或 grid 的 1fr,三行程式碼,沒有任何代價。而理解前四種方法的價值,在於看清「CSS 布局能力是如何一步步演進的」——這個脈絡能幫你判斷什麼時候該用什麼工具。

補充一個實務細節:行動裝置 Safari 的網址列會伸縮,導致 100vh 大於實際可視高度。請改用 100dvh(dynamic viewport height)並保留 100vh 作為 fallback:

body {
  min-height: 100vh;
  min-height: 100dvh;   /* 支援的瀏覽器會採用這行 */
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved