萬盛學電腦網

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

HTML DIV+CSS 實作全螢幕通欄版面完整範例

通欄(Full-width Bar)指橫跨整個視窗寬度的版面區塊(如頂部導覽、底部版權),內容區再置中限制寬度。以下提供可直接套用的完整架構範例。

核心架構原則

  • 外層 width: 100% 撐滿視窗,背景色/圖延伸全幅。
  • 內層 width: 980px; margin: 0 auto; 置中承載內容。
  • 使用 box-sizing: border-box 避免 padding 撐破寬度。

完整 HTML + CSS 範例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>通欄版面範例</title>
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  ul { list-style: none; }
  img { border: none; vertical-align: top; }
  a { text-decoration: none; color: #fff; }
  a:hover { text-decoration: underline; }

  /* 通欄結構 */
  #header { width: 100%; background: #eee; }
  #menu_bg { width: 100%; background: pink; height: 50px; }
  #menu { width: 980px; margin: 0 auto; }
  #menu li { width: 101px; float: left; }
  #menu a { display: block; width: 100px; height: 48px; line-height: 48px; text-align: center; background: black; border-bottom: 2px solid black; }
  #menu a:hover { border-bottom-color: pink; }

  #wrap { width: 980px; height: 500px; margin: 0 auto; background: green; }
  #footer_bg { width: 100%; background: pink; height: 50px; }
  #footer { width: 980px; margin: 0 auto; }
  .friend_links li { display: inline; margin-right: 15px; }
</style>
</head>
<body>
  <div id="header">
    <div id="logo">Logo 區塊</div>
    <div id="menu_bg">
      <div id="menu">
        <ul>
          <li class="current"><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
          <li><a href="#">Price</a></li>
        </ul>
      </div>
    </div>
  </div>

  <div id="wrap">主內容區</div>

  <div id="footer_bg">
    <div id="footer">
      <p class="copyright">© 2024 公司名稱</p>
      <ul class="friend_links">
        <li>百度</li><li>Google</li><li>有道</li><li>騰訊</li>
      </ul>
    </div>
  </div>
</body>
</html>

關鍵細節補充

  1. #menu li 寬度 101px × 4 = 404px,配合 #menu 980px 仍有餘裕,實務上建議用 flex 平均分配。
  2. border-bottom 做為 hover 指示器,比改變背景色更輕量。
  3. Footer 友情連結用 display: inline 水平排列,簡潔高效。

現代化重構建議

/* Flexbox 版通欄選單 */
#menu { display: flex; justify-content: center; }
#menu li { flex: 1; max-width: 120px; }
#menu a { display: flex; align-items: center; justify-content: center; }

Flexbox 免清除浮動、自適應項目數、垂直置中原生支援,強烈建議新專案採用。

copyright © 萬盛學電腦網 all rights reserved