子元素設定 `margin-top`,父元素跟著向下移動,而非子元素在父元素內向下移動。
當**父子元素垂直外邊距相鄰**、且父元素**沒有 BFC (塊級格式化上下文) 觸發條件**時,子元素的 `margin-top` 會「傳遞」給父元素,導致父元素產生相同外邊距。
| 方法 | CSS | 副作用 |
|---|---|---|
| overflow 隱藏/自動 | overflow: hidden / auto | 可能剪裁內容/出現滾動條 |
| Flex/Grid 容器 | display: flex / grid | 現代佈局首選,無副作用 |
| 浮動 | float: left / right | 需清除浮動,影響佈局 |
| 絕對/固定定位 | position: absolute / fixed | 脫離文檔流 |
| 邊框/內邊距 | border: 1px solid transparent / padding-top: 1px | 視覺無差異,最輕量 |
.parent {
/* 方案一:最輕量 */
padding-top: 1px; /* 或 border-top: 1px solid transparent */
/* 方案二:現代佈局 */
display: flow-root; /* 專門建立 BFC,無副作用 (現代瀏覽器支援) */
/* 方案三:Flex */
display: flex; flex-direction: column;
}
張鑫旭博客詳細圖解:https://www.cnblogs.com/hejia/archive/2013/05/26/3099697.html