網站首頁 編程語言 正文
目錄
CSS中上下margin的傳遞和折疊
1.上下margin傳遞
1.1.margin-top傳遞
為什么會產生上邊距傳遞?
塊級元素的頂部線和父元素的頂部線重疊,那么這個塊級元素的margin-top值會傳遞給父元素。
示例代碼:給inner盒子設置margin-top: 20px;
。
.reference {
width: 100px;
height: 100px;
background-color: #f00;
color: #fff;
}
.box {
width: 200px;
height: 200px;
background-color: #0f0;
}
.inner {
width: 100px;
height: 100px;
background-color: #00f;
margin-top: 20px;
}
<div class="reference">參考盒子</div>
<div class="box">
<div class="inner"></div>
</div>
運行結果:inner的margin-top
的值傳遞給了box。
1.2.margin-bottom傳遞
為什么會產生下邊距傳遞?
塊級元素的底部線和父元素的底部線重疊,并且父元素的高度是auto,那么這個塊級元素的margin-bottom值會傳遞給父元素。
示例代碼:給inner盒子設置margin-bottom: 20px;
,并且給父元素設置height: auto;
。
.box {
width: 200px;
height: auto; /* 給父元素高度設置auto,或者不設置高度,默認為auto */
background-color: #0f0;
}
.inner {
width: 100px;
height: 100px;
background-color: #00f;
margin-bottom: 20px;
color: #fff;
}
.reference {
width: 100px;
height: 100px;
background-color: #f00;
color: #fff;
}
<div class="box">
<div class="inner">inner</div>
</div>
<div class="reference">參考盒子</div>
運行結果:inner的margin-bottom
的值傳遞給了box。
1.3.如何防止出現傳遞問題?
-
給父元素設置
padding-top
或padding-bottom
,防止頂部線或底部線重疊即可; -
給父元素設置
border
,可以解決邊距傳遞的問題; -
觸發BFC(Block Format Context,塊級格式化上下文),簡單理解就是給父元素設置一個結界,防止上下邊距傳遞出去(最優解決方案)。觸發BFC有以下方式:
- 添加浮動
float
(float的值不能是none); - 設置一個非
visible
的overflow
屬性(除了visible,其他屬性值都可以,像hidden、auto、scroll等); - 設置定位
position
(position的值不能是static或relative); - 設置
display
的值為inline-block、table-cell、flex、table-caption或inline-flex
;
- 添加浮動
2.上下margin折疊
上下margin折疊(collapse),也稱作外邊距塌陷。垂直方向上相鄰的2個margin(margin-top、margin-bottom)有可能會合并為一個margin。但是水平方向上的margin(margin-left、margin-right)永遠不會折疊。
2.1.兄弟塊級元素之間上下margin折疊
示例代碼:給box1設置下邊距40px,給box2設置上邊距20px。
.box1 {
width: 100px;
height: 100px;
background-color: #f00;
margin-bottom: 40px;
}
.box2 {
width: 100px;
height: 100px;
background-color: #0f0;
margin-top: 20px;
}
<div class="box1">box1</div>
<div class="box2">box2</div>
運行結果:兩個盒子間距為40px。
2.2.父子塊級元素之間上下margin折疊
示例代碼:inner設置上邊距為40px,父元素box設置上邊距為20px。
.reference {
width: 100px;
height: 100px;
background-color: #00f;
color: #fff;
}
.box {
width: 200px;
height: 200px;
background-color: #f00;
margin-top: 20px;
}
.inner {
width: 100px;
height: 100px;
background-color: #0f0;
margin-top: 40px;
}
<div class="reference">參考盒子</div>
<div class="box">
<div class="inner">inner</div>
</div>
運行結果:上邊距為40px。
2.3.總結
- 父子塊級元素之間上下margin折疊的原因是inner將上邊距傳遞給了父元素box,然后父元素box再與自己的上邊距進行比較;
- 折疊后最終計算規則:兩個值進行比較,取較大值;
- 如果想防止上下邊距折疊,只設置其中一個即可;
原文鏈接:https://blog.csdn.net/qq_52732369/article/details/122095839
相關推薦
- 2022-01-05 npm ERR! code ENOENT npm ERR! syscall open npm ERR
- 2021-12-03 Apache?Log4j2?報核彈級漏洞快速修復方法_Linux
- 2022-12-02 C語言學習之指針的使用詳解_C 語言
- 2022-12-01 解決k8s?namespace?一直處于?Terminating?狀態的問題_云其它
- 2022-04-15 python實現購物車功能_python
- 2023-03-16 ProxyWidget和Element更新的正確方式詳解_Android
- 2022-11-04 Android自定義View實現時鐘功能_Android
- 2022-06-17 docker上快速搭建gitlab、gitlab-runer及實現CI/CD功能_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支