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

先釐清需求:我們要的是 footer 永遠固定在頁面的底部,而不是永遠固定在螢幕的底部。換句話說:
總結一句:footer 永遠沉底。
以下四種方法,前三種純 CSS,最後一種用 jQuery。
<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(頁腳)。
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; }
height:100%,這樣容器才能使用百分比高度;同時把 margin 與 padding 歸零。min-height:100%,確保內容很少時仍能撐滿整個視窗。IE6 不支援 min-height,需用 height:auto !important; height:100%; 這個 hack 繞過。另外要設 position:relative,讓內部絕對定位的元素以它為基準。padding-bottom,且值要等於或略大於 footer 的高度,用來預留空間。也可以用 border-bottom-width 代替,但千萬不能用 margin-bottom,否則效果會失效。bottom:0,這樣它就固定在 container 底部。優點:結構簡單清晰,無需 JS 與 hack(除 IE6 的 min-height)即可相容各瀏覽器,也適應 iPhone。
缺點:footer 必須設定固定高度,且 #page 的 padding-bottom 要與之一致。
這次 #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>
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 高度。
不同之處:
優點:結構簡單,無需 JS 與 hack。缺點:footer 高度必須固定,無法自適應。
<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>
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 高度。
<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 最後面。
* { 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%,否則此方法失效。
// 用 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 的環境無法顯示,且每次改變視窗大小會閃動一下。
以上四法都是為了繞過「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。
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; /* 支援的瀏覽器會採用這行 */
}