萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

div+CSS 版面佈局完全指南:float、position、多欄實戰模式與進階技巧

div+CSS 佈局是從表格佈局向語意化、分離關注點邁進的關鍵里程碑。雖現代已有 Flexbox/Grid,但理解 float/position 經典機制對維護舊專案、掌握 CSS 核心渲染模型仍具不可替代價值。本文系統整理兩大定位體系、主流多欄佈局模式、跨瀏覽器 Hack 與進階等高技巧。

核心定位機制對比

Float 浮動佈局 —— 文檔流式

float: none | left | right
  • 元素脫離常規流向左/右浮動,後續內容環繞
  • 相對定位:隨瀏覽器視窗、解析度變化自適應
  • 需配合 clear: both 清除浮動,防止父容器高度塌陷

經典兩欄 float 範例

<div id="wrap">
    <div id="column1">左欄</div>
    <div id="column2">右欄</div>
    <div class="clear"></div>
</div>

#wrap { width: 100%; height: auto; }
#column1 { float: left; width: 40%; }
#column2 { float: right; width: 60%; }
.clear { clear: both; }

Position 定位佈局 —— 絕對控制

position: static | absolute | fixed | relative

經典兩欄 absolute 範例

#wrap { position: relative; width: 770px; }
#column1 { position: absolute; top: 0; left: 0; width: 300px; }
#column2 { position: absolute; top: 0; right: 0; width: 470px; }

關鍵差異對照

值行為定位基準文檔流
static預設,無特殊定位常規流在流中
relative相對自身原位置偏移自身原位置仍佔據空間
absolute脫離文檔流,絕對定位最近的非 static 祖先,或 body脫離,不佔空間
fixed固定於視窗 (IE6- 不支援)viewport脫離
維度FloatAbsolute Position
自適應性高,隨容器/視窗變化低,固定座標寬度
內容驅動高度支援 (需 clearfix)不支援,高度需固定或 JS 計算
來源順序獨立否 (HTML 順序影響視覺)是 (任意順序)
維護難度中 (清除浮動、外距疊加)高 (脫離流、重疊風險)
適用場景主流頁面佈局、響應式覆蓋層、Modal、Tooltip、固定側邊欄

結論:一般頁面佈局優先選 float (現代改用 Flex/Grid);absolute 專用於脫離流的覆蓋型元件。

主流多欄佈局模式速查表

單欄居中

body { margin: 0; padding: 0; text-align: center; }
#content { margin-left: auto; margin-right: auto; width: 400px; }

兩欄 (左右)

#bodycenter { width: 700px; margin: 0 auto; overflow: auto; }
#dv1 { float: left; width: 280px; }
#dv2 { float: right; width: 410px; }

三欄 (左右固定,中間自適應) —— 聖杯佈局雛形

body { margin: 0; padding-left: 200px; padding-right: 190px; min-width: 240px; }
.column { position: relative; float: left; }
#center { width: 100%; }
#left { width: 180px; right: 240px; margin-left: -100%; }
#right { width: 130px; margin-right: -100%; }

三行三欄完整架構

#header { width: 100%; }
#wrap { width: 100%; }
#column { float: left; width: 60%; }
#column1 { float: left; width: 30%; }
#column2 { float: right; width: 30%; }
#column3 { float: right; width: 40%; }
.clear { clear: both; }
#footer { width: 100%; }

CSS Hack 解決 IE5.x/6 盒模型差異

問題:IE5.x 盒模型 width = content + padding + border,標準瀏覽器 width = content。

Tantek Hack (BMH)

.content { 
    width: 400px;           /* 錯誤寬度,所有瀏覽器讀取 */
    voice-family: "\"}\"";  /* IE5.x 忽略後續 */
    voice-family: inherit;
    width: 300px;           /* 正確寬度,IE6+/現代瀏覽器覆蓋 */
}
html>body .content { width: 300px; }  /* CSS2 選擇器,非 IE5 讀取 */

簡化 !important Hack

.content {
    width: 300px !important;  /* 現代瀏覽器生效 */
    width: 400px;             /* IE6 讀取此行 (不支援 !important) */
}
html>body .content { width: 300px; }

現代建議:直接設定 box-sizing: border-box 統一盒模型,徹底解決歷史包袱。

進階技巧:多欄等高

方法一:背景圖填充法 (Faux Columns)

#wrap { width: 776px; background: url(bg.gif) repeat-y 300px; }
#column1 { float: left; width: 300px; }
#column2 { float: right; width: 476px; }
.clear { clear: both; }

原理:容器背景圖在欄交界處垂直重複,視覺偽裝等高。簡單、相容性極佳、無 JS 依賴。

方法二:JavaScript 動態計算

讀取各欄高度 → 取最大值 → 套用至所有欄。靈活但需 JS、重繪成本高、響應式需監聽 resize。

方法三:負 margin-bottom + padding-bottom + overflow:hidden (One True Layout)

.container { overflow: hidden; }
.column { 
    float: left; 
    padding-bottom: 9999px; 
    margin-bottom: -9999px; 
}

原理:巨大 padding 撐高、負 margin 抵銷、容器 overflow 截斷。純 CSS、無圖片、支援動態內容。現代首選純 CSS 方案。

滿螢幕高度佈局 (經典 770px~1024px 自適應)

參考資源:/tech/web/2005/3124.asp

  • html, body { height: 100%; margin: 0; }
  • 容器 min-height: 100%; 內容區 padding-bottom 預留 footer 高度
  • Footer 絕對定位 bottom: 0 或負 margin 向上拉回

現代化遷移路徑

經典問題現代解法 (CSS3+)
Float 清除.clearfix::after { content:''; display:table; clear:both; } 或 display: flow-root
垂直置中Flex: align-items: center / Grid: place-items: center
等高欄Flex 預設等高 / Grid 自動等高
聖杯/雙飛翼佈局Grid: grid-template-areas 一行定義
響應式斷點媒體查詢 + 相對單位 (rem/%)
盒模型差異*, *::before, *::after { box-sizing: border-box; }

總結

div+CSS 以 float/position 為核心,構建了 Web 佈局的黃金十年。掌握其機制、缺陷與 Hack 技巧,是理解現代 Flex/Grid 設計動機的最佳切入點。新專案請擁抱現代佈局模組;舊專案維護時,善用 box-sizing: border-box、Clearfix、負 margin 等經典技巧穩定現狀,漸進式重構為宜。

copyright © 萬盛學電腦網 all rights reserved