萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS Sticky Footer 實戰:頁腳永久固定頁面底部的多種方案與最佳實踐

問題場景

頁面內容較少時,頁腳飄在視窗中間;內容多時,頁腳正常隨內容下沉。需求:**內容少時頁腳貼底,內容多時頁腳自然流動**。

方案對比

  • 代碼極少、無魔法數字、自適應高度
  • IE10+ 需前綴
  • 語意清晰、兩行代碼
  • IE 不支援
  • 兼容 IE8+
  • 需知 footer 固定高度、需佔位元素
  • 簡單
  • 內容多時遮擋內容、滾動體驗差
  • 無額外 DOM
  • 需知 header/footer 高度、vh 在移動端有地址欄問題
  • 方案原理優點缺點推薦度
    Flexbox 佈局(現代首選) body 為 flex column,main flex:1 撐開 ★★★★★
    Grid 佈局 body 為 grid,行模板 auto 1fr auto ★★★★★
    負 Margin + 側欄佔位(傳統經典) wrapper min-height:100%,負 margin 抵消 footer 高度 ★★★☆☆
    position: fixed footer 固定視窗底部 ❌ 不推薦
    calc() + min-height main min-height: calc(100vh - header - footer) ★★★★☆

    方案一:Flexbox Sticky Footer(推薦標準寫法)

    <body class="site">
      <header class="site-header">頁首</header>
      <main class="site-main">主要內容</main>
      <footer class="site-footer">頁腳</footer>
    </body>
    
    .site {
      display: flex;
      flex-direction: column;
      min-height: 100vh; /* 關鍵:至少佔滿視窗高度 */
    }
    .site-main {
      flex: 1; /* 關鍵:佔據剩餘空間,推送 footer 到底部 */
      /* 等同 flex: 1 0 auto; */
    }
    .site-header,
    .site-footer {
      flex-shrink: 0; /* 防止被壓縮 */
    }

    優勢:footer 高度任意(不需固定)、無額外 DOM、無魔法數字、響應式天然適配。

    方案二:Grid Sticky Footer(最語意化)

    .site {
      display: grid;
      grid-template-rows: auto 1fr auto; /* header / main / footer */
      min-height: 100vh;
    }
    /* 子元素無需任何額外樣式 */

    Grid 明確定義三行軌道,中間 `1fr` 自動分配剩餘空間,最直觀。

    方案三:傳統負 Margin 法(IE8+ 兼容專用)

    <body>
      <div class="wrapper">
        <header>頁首</header>
        <main>內容</main>
        <div class="footer-placeholder"></div> /* 關鍵佔位 */
      </div>
      <footer class="footer">頁腳</footer>
    </body>
    
    html, body { height: 100%; margin: 0; }
    .wrapper {
      min-height: 100%;
      margin-bottom: -120px; /* footer 高度負值 */
      padding-bottom: 120px; /* 保護內容不被 footer 遮蓋 */
    }
    .footer,
    .footer-placeholder {
      height: 120px; /* 必須固定高度 */
    }
    .footer-placeholder { clear: both; } /* 清除浮動 */

    關鍵點:

    1. `html,body{height:100%}` 建立百分比高度參考鏈
    2. `.wrapper{min-height:100%}` 確保最小撐滿視窗
    3. 負 `margin-bottom` 向上拉動 footer
    4. 同值 `padding-bottom` / 佔位元素 保護內容區不被覆蓋
    5. IE6 用 `_height:100%` hack 替代 `min-height`

    方案四:calc() 動態計算(無額外 DOM、footer 固定高度)

    .site-main {
      min-height: calc(100vh - 80px - 120px); /* 100vh - header高 - footer高 */
      /* 或用 CSS 變數 */
      min-height: calc(100vh - var(--header-h) - var(--footer-h));
    }

    移動端陷阱:`100vh` 包含瀏覽器地址欄,滾動時地址欄隱藏會導致視窗高度變化、內容跳動。解決:

    • 用 `dvh` (Dynamic Viewport Height) —— CSS Viewport Units Level 1,支援度較新
    • 或 JS 監聽 `resize` 動態設定 `--vh` 變數
    • 或退回 Flex/Grid 方案(不依賴 vh)

    進階場景:內容區內部再有 Sticky 元素

    .site-main {
      display: flex;
      flex-direction: column;
      flex: 1;
      overflow: auto; /* 內部滾動 */
    }
    .sticky-sidebar {
      position: sticky;
      top: 1rem;
      align-self: flex-start; /* 防止被拉伸 */
      max-height: calc(100vh - 2rem);
      overflow: auto;
    }

    常見坑與調試技巧

    現象原因排查
    Footer 仍在中間body 無 min-height:100vh 或 flex-direction 錯誤檢查 html/body 高度鏈、flex 方向
    內容被 Footer 遮蓋缺少 padding-bottom / placeholder補齊保護空間
    Footer 高度變化導致跳動負 margin 方案需固定高度改用 Flex/Grid 或 JS 同步高度
    移動端地址欄導致 100vh 不準vh 包含地址欄用 dvh、Flex/Grid、或 JS 方案

    總結選型建議

    1. 新專案(現代瀏覽器):Grid `grid-template-rows: auto 1fr auto` — 最語意、最少代碼、零魔法數字
    2. 2. 需兼容 IE10/11:Flexbox `flex:1` — 成熟穩定、自適應高度 3. 極舊專案維護:負 Margin + 佔位元素 — 兼容 IE8+,但需固定 footer 高度 4. 特殊需求(如內部滾動):Flex + `overflow:auto` 組合

    Sticky Footer 是 CSS 佈局的「Hello World」級經典問題。現代 CSS 以 Flex/Grid 徹底解決了「需要知道高度」這一核心痛點,請毫不猶豫地使用它們。

    copyright © 萬盛學電腦網 all rights reserved