經典 Sticky Footer 需求:頁面內容少時 footer 貼視窗底部;內容多時 footer 隨內容自然下推,且縮放視窗不重疊。純 CSS 解法,結構清晰、相容性佳。


<div id="wrap">
<div id="main" class="clearfix">
<div id="content"></div>
<div id="side"></div>
</div>
</div>
<div id="footer"></div>
關鍵:#footer 在 #wrap 外部,同級於 #wrap。
html, body { height: 100%; margin: 0; }
#wrap { min-height: 100%; margin-bottom: -100px; } /* footer 高度 */
#footer { height: 100px; clear: both; }
.clearfix::after { content:""; display:block; clear:both; height:100px; } /* 佔位防重疊 */
原理:#wrap 撐滿視窗高度,負 margin 向上拉回 footer 高度,clearfix::after 撐開等高空間,footer 自然落在該空間。
body { display: flex; flex-direction: column; min-height: 100vh; margin: 0; }
#main { flex: 1; } /* 自動撐滿剩餘空間 */
#footer { /* 固定或自適應高度 */ }
Flex 方案更簡潔、無需負 margin、無佔位元素,IE10+ 完美支援,為現代首選。