外邊距合併是 CSS 盒模型中最反直覺、最容易造成版面錯亂的機制。理解「只要接觸,就合併」的核心邏輯,掌握建立「邊距隔離上下文」的各種方法,是 CSS 佈局進階的必修課。
當兩個或多個垂直方向的外邊距(margin-top/margin-bottom)相接觸時,它們會合併為單一外邊距,大小取最大值(正數取最大、負數取絕對值最大、正負混合取代數和)。
/* 經典範例:相鄰兄弟元素 */
.box-1 { margin-bottom: 30px; }
.box-2 { margin-top: 20px; }
/* 實際間距 = max(30px, 20px) = 30px,而非 50px */
/* 父子元素首尾相接 */
.parent { /* 無 padding、無 border、無 overflow:hidden 等 */ }
.child { margin-top: 30px; }
/* 子元素 margin-top 會「穿透」父元素,作用於父元素外部 */
| 場景 | 條件 | 結果 |
|---|---|---|
| 相鄰兄弟元素 | 同層級、垂直相鄰、無內容/邊框/間距隔開 | 合併為單一 margin,取最大值 |
| 父子元素 (首尾) | 父元素無 padding-top/border-top/內容,子元素有 margin-top 父元素無 padding-bottom/border-bottom/內容,子元素有 margin-bottom |
子元素 margin 「穿透」父元素,作用於父元素外部 |
| 空元素 | 元素無內容、無 padding、無 border、無 height/min-height、無建立 BFC | 自身 margin-top 與 margin-bottom 合併 |
以下任一條件皆可阻斷外邊距合併:
| 方法 | CSS 宣告 | 原理 | 副作用/注意 |
|---|---|---|---|
| 父元素加 Border | border: 1px solid transparent 或實際顏色 | Border 物理隔開邊距 | 影響尺寸、需配合 box-sizing |
| 父元素加 Padding | padding: 1px 或實際值 | Padding 物理隔開邊距 | 增加內部空間 |
| 父元素建立 BFC | overflow: hidden/auto/scrolldisplay: flow-root (現代首選)display: flex/gridfloat: left/rightposition: absolute/fixed | BFC 內部 margin 與外部隔離 | 可能影響子元素定位、浮動清除 |
| 元素設為 Flex/Grid 子項 | 父元素 display: flex/grid | Flex/Grid 子項不參與外部 margin 合併 | 改變佈局模式 |
| 元素設為絕對/固定定位 | position: absolute/fixed | 脫離文檔流,不參與合併 | 脫離文檔流 |
| 元素設為浮動 | float: left/right | 浮動元素不參與合併 | 需清除浮動 |
| 內容隔開 | 父元素有文字內容、<br>、內聯元素等 | 內容物理隔開首尾邊距 | 可能引入額外空白 |
<section class="hero">
<h1 class="title">標題</h1>
</section>
.hero { background: #0066cc; padding: 2rem 0; } /* 想要上下各 2rem 內距 */
.title { margin-top: 1.5rem; } /* 實際上 .hero 上方會多出 1.5rem 空白 */
/* 解法:任選一 */
.hero { overflow: hidden; } /* BFC */
.hero { display: flow-root; } /* 現代 BFC,無副作用 */
.hero { padding-top: 1px; } /* 微小 padding */
.hero { border-top: 1px solid transparent; } /* 透明邊框 */
.title { margin-top: 0; padding-top: 1.5rem; } /* 改用 padding */
<div class="spacer"></div> /* 只想佔 20px 高度 */
.spacer { margin: 20px 0; } /* 實際可能合併為 20px,或與鄰居再合併 */
/* 解法:用 height 或 padding */
.spacer { height: 20px; } /* 或 min-height */
.spacer { padding: 20px 0; } /* padding 不合併 */
.box-1 { margin-bottom: 30px; }
.box-2 { margin-top: -20px; }
/* 合併結果 = 30 + (-20) = 10px */
.box-1 { margin-bottom: -30px; }
.box-2 { margin-top: -20px; }
/* 合併結果 = max(-30, -20) 取絕對值大者 = -30px */
<article>
<header><h1>標題</h1></header>
<div class="content"><p>內文</p></div>
</article>
/* h1 margin-top → 穿透 header → 穿透 article → 作用於 article 上方
p margin-bottom → 穿透 content → 穿透 article → 作用於 article 下方
結果:article 邊緣產生不可控空白 */
/* 解法:article 建立 BFC */
article { display: flow-root; }
/* 只用 margin-bottom 或只用 margin-top,避免相鄰合併 */
.flow > * + * { margin-top: 1.5rem; } /* 相鄰兄弟元素間距,無合併問題 */
/* 或統一用 margin-bottom */
.flow > * { margin-bottom: 1.5rem; }
.flow > *:last-child { margin-bottom: 0; }
/* 間距由父容器控制,子元素無 margin */
.stack { display: flex; flex-direction: column; gap: 1.5rem; }
.inline-cluster { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; }
/* 完全避開 margin 合併問題 */
.section {
margin-block-start: 2rem; /* 邏輯屬性:塊級開始邊距 */
margin-block-end: 2rem; /* 邏輯屬性:塊級結束邊距 */
/* 自動適配 horizontal-tb / vertical-rl 等書寫模式 */
}
/* 開發階段暫時加入,視覺化所有 margin */
* {
outline: 1px solid rgba(255,0,0,0.1);
background: rgba(0,255,0,0.02);
}
*::before { content: "m-t: " attr(style); } /* 需配合 JS 注入 */
/* 或使用瀏覽器擴充:CSS Margin Visualizer, Layout Shift Visualizer */
外邊距合併遵循「垂直相接觸即合併,取最大值」核心規則。關鍵在於識別三大觸發場景(相鄰兄弟、父子首尾、空元素自我),並掌握七種隔離方法(Border、Padding、BFC、Flex/Grid 子項、絕對定位、浮動、內容隔開)。現代最佳實踐推薦:單向邊距 + Flex/Grid gap + display: flow-root + 邏輯屬性,從根源規避合併困擾,建立可預測、可維護的間距系統。