萬盛學電腦網

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

使用 CSS 將 DIV 分為左右兩欄的方法

情境說明

早期版面常將廣告置於左上,內容隨之流至廣告下方。若資料較短,使用者易忽略。改用左右分欄可讓側邊廣告與主內容並列,提升可見度。

核心 CSS 技巧

父容器不設寬度(或設 100%),子容器分別 float: left 並指定百分比寬度:

#exhibitor114 { margin: 0 auto; width: 100%; }
#exhibitorleft  { float: left; width: 44%; background: #eee; }
#exhibitorright { float: left; width: 56%; }

完整 HTML 結構

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>左右分欄示範</title>
  <style type="text/css">
    #exhibitor114 { margin: 0 auto; width: 100%; }
    #exhibitorleft  { float: left; width: 44%; height: 100%; background: #eeeeee; }
    #exhibitorright { float: left; width: 56%; height: 100%; }
  </style>
</head>
<body>
  <div id="exhibitor114">
    <div id="exhibitorleft">左側內容(如廣告)</div>
    <div id="exhibitorright">右側主內容</div>
  </div>
</body>
</html>

注意事項

  • 父層需清除浮動(如 overflow: hidden 或 clearfix),否則高度塌陷。
  • 現代佈局建議改用 Flexbox 或 Grid,語法更簡潔、響應式支援更好。
copyright © 萬盛學電腦網 all rights reserved