正常流 = Normal Flow(非 Document Flow,W3C 標準術語)
div、p、h1-h6)span、a、strong)img、input、button — 擁有寬高、水平排列對應規格書:
| 方案 | 觸發條件 | 脫離文檔流 | 定位基準 | 佔據空間 | 層疊級別 |
|---|---|---|---|---|---|
| 正常流 | 默認 | 否 | 包含塊 | 是 | 0 (底層) |
| 浮動 | float: left/right | 部分 (文本環繞) | 包含塊邊緣/前一浮動元素 | 否 (不佔據文檔流空間) | 1 (文本之上) |
| 絕對定位 | position: absolute/fixed | 完全 | 最近非 static 祖先 / 視口 | 否 | 由 z-index 決定 |
| 相對定位 | position: relative | 否 (視覺偏移) | 自身正常位置 | 是 (保留原空間) | 由 z-index 決定 |
| 黏性定位 | position: sticky | 否/視情況 | 最近滾動祖先 / 視口 | 是 | 由 z-index 決定 |
觸發條件:
float ≠ noneposition: absolute/fixed/stickydisplay: flex/grid/table-cell/table-caption/flow-rootoverflow ≠ visible (hidden/auto/scroll)contain: layout/paint/content/strictBFC 特性:
line-height 決定vertical-align: baseline (默認),可改 middle/top/bottomtext-align、letter-spacing、word-spacing/* 僅發生於正常流塊級元素垂直方向 */
.parent { margin-bottom: 20px; }
.child { margin-top: 30px; }
/* 實際間距 = max(20px, 30px) = 30px */
/* 不重疊場景 */
- Flex/Grid 容器的子元素
- 絕對定位元素
- 建立 BFC 的元素
- 根元素
- 有 padding/border/clearance 分隔的相鄰元素
clear: left/right/both;
/* 強制元素頂部邊緣低於前面對應方向浮動元素底部 */
/* 實際原理:增加元素上邊距直到滿足條件 */
| 定位類型 | 包含塊 |
|---|---|
| static/relative | 最近祖先塊級盒內容邊緣 |
| absolute | 最近非 static 祖先的 padding box |
| fixed | 視口 (ICB) / 最近 transform/filter/perspective 祖先 |
| sticky | 最近滾動祖先的內容區 / 視口 |
opacity < 1, transform ≠ none, filter ≠ none, will-change, isolation: isolate/* Flex 容器:建立 Flex 格式化上下文 (FFC) */
.flex { display: flex; }
/* 子元素:Flex 項目,不再遵循正常流規則 */
/* 支援:主軸/交叉軸對齊、伸縮、排序、換行 */
/* Grid 容器:建立 Grid 格式化上下文 (GFC) */
.grid { display: grid; grid-template-columns: repeat(3, 1fr); }
/* 子元素:Grid 項目,二維精確定位 */
/* 支援:區域命名、隱式/顯式軌道、對齊、層疊控制 */
/* 多欄佈局 */
.multi-col { column-count: 3; column-gap: 2rem; }
/* 內容自動流入欄,類似報紙排版 */
深度理解正常流機制,不是為了回歸過去,而是為了在現代工具失效時,能精準診斷根因、做出正確架構決策。