網(wǎng)站首頁 編程語言 正文
目錄
CSS中上下margin的傳遞和折疊
1.上下margin傳遞
1.1.margin-top傳遞
為什么會(huì)產(chǎn)生上邊距傳遞?
塊級元素的頂部線和父元素的頂部線重疊,那么這個(gè)塊級元素的margin-top值會(huì)傳遞給父元素。
示例代碼:給inner盒子設(shè)置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>
運(yùn)行結(jié)果:inner的margin-top
的值傳遞給了box。
1.2.margin-bottom傳遞
為什么會(huì)產(chǎn)生下邊距傳遞?
塊級元素的底部線和父元素的底部線重疊,并且父元素的高度是auto,那么這個(gè)塊級元素的margin-bottom值會(huì)傳遞給父元素。
示例代碼:給inner盒子設(shè)置margin-bottom: 20px;
,并且給父元素設(shè)置height: auto;
。
.box {
width: 200px;
height: auto; /* 給父元素高度設(shè)置auto,或者不設(shè)置高度,默認(rèn)為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>
運(yùn)行結(jié)果:inner的margin-bottom
的值傳遞給了box。
1.3.如何防止出現(xiàn)傳遞問題?
-
給父元素設(shè)置
padding-top
或padding-bottom
,防止頂部線或底部線重疊即可; -
給父元素設(shè)置
border
,可以解決邊距傳遞的問題; -
觸發(fā)BFC(Block Format Context,塊級格式化上下文),簡單理解就是給父元素設(shè)置一個(gè)結(jié)界,防止上下邊距傳遞出去(最優(yōu)解決方案)。觸發(fā)BFC有以下方式:
- 添加浮動(dòng)
float
(float的值不能是none); - 設(shè)置一個(gè)非
visible
的overflow
屬性(除了visible,其他屬性值都可以,像hidden、auto、scroll等); - 設(shè)置定位
position
(position的值不能是static或relative); - 設(shè)置
display
的值為inline-block、table-cell、flex、table-caption或inline-flex
;
- 添加浮動(dòng)
2.上下margin折疊
上下margin折疊(collapse),也稱作外邊距塌陷。垂直方向上相鄰的2個(gè)margin(margin-top、margin-bottom)有可能會(huì)合并為一個(gè)margin。但是水平方向上的margin(margin-left、margin-right)永遠(yuǎn)不會(huì)折疊。
2.1.兄弟塊級元素之間上下margin折疊
示例代碼:給box1設(shè)置下邊距40px,給box2設(shè)置上邊距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>
運(yùn)行結(jié)果:兩個(gè)盒子間距為40px。
2.2.父子塊級元素之間上下margin折疊
示例代碼:inner設(shè)置上邊距為40px,父元素box設(shè)置上邊距為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>
運(yùn)行結(jié)果:上邊距為40px。
2.3.總結(jié)
- 父子塊級元素之間上下margin折疊的原因是inner將上邊距傳遞給了父元素box,然后父元素box再與自己的上邊距進(jìn)行比較;
- 折疊后最終計(jì)算規(guī)則:兩個(gè)值進(jìn)行比較,取較大值;
- 如果想防止上下邊距折疊,只設(shè)置其中一個(gè)即可;
原文鏈接:https://blog.csdn.net/qq_52732369/article/details/122095839
相關(guān)推薦
- 2022-07-29 pytest?fixtures函數(shù)及測試函數(shù)的參數(shù)化解讀_python
- 2022-08-06 Go語言開發(fā)編程規(guī)范命令風(fēng)格代碼格式_Golang
- 2021-12-14 linux下多線程中的fork介紹_Linux
- 2022-09-08 深入了解Go語言的基本語法與常用函數(shù)_Golang
- 2023-04-14 jupyter-lab設(shè)置自啟動(dòng)及遠(yuǎn)程連接開發(fā)環(huán)境_python
- 2021-12-05 解析Redis?數(shù)據(jù)結(jié)構(gòu)之簡單動(dòng)態(tài)字符串sds_Redis
- 2022-03-20 關(guān)于數(shù)據(jù)庫系統(tǒng)的概述_數(shù)據(jù)庫其它
- 2022-04-11 解決react中l(wèi)abel標(biāo)簽for報(bào)錯(cuò)問題_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支