黑松山资源网 Design By www.paidiu.com
首先我们先看出现外边距塌陷的三种情况:
1.当上下相邻的两个块级元素相遇,上面的元素有下边距margin-bottom,下面的元素有上边距margin-top,则它们之间的垂直距离取两个值中的较大者。
<style> .box1 { width: 150px; height: 100px; margin-bottom: 20px; background-color: rgb(201, 239, 98); } .box2 { width: 100px; height: 100px; background-color: rebeccapurple; margin-top: 10px; } </style> <div class="box1"></div> <div class="box2"></div>
这时候两个盒子之间的垂直距离不是30px,而是20px。
解决方法:
尽量只给一个盒子添加margin值
2.对于两个嵌套关系的块元素,如果父元素没有上内边距及边框,父元素的上外边距会与子元素的上外边距发生合并,合并后的外边距为两者中的较大者。
<style> .box1 { width: 150px; height: 100px; margin-top: 20px; /* border: 1px solid #000000; */ background-color: red; } .box2 { width: 100px; height: 100px; /* border: 1px solid #000000; */ background-color: rebeccapurple; margin-top: 10px; } </style> </head> <body> <div class="box1"> <div class="box2"></div> </div> </body>
这时候两个盒子会发生合并,上外边距为20px。
解决办法:
①给父元素定义上边框
②给父元素定义上内边距
③给父元素添加 overflow:hidden;
④添加浮动
⑤添加绝对定位
3.如果存在一个空的块级元素,border、padding、inline content、height、min-height都不存在,那么上下边距中间将没有任何阻隔,上下外边距将会合并。
<p style="margin-bottom: 0px;">这个段落的和下面段落的距离将为20px</p> <div style="margin-top: 20px; margin-bottom: 20px;"></div> <p style="margin-top: 0px;">这个段落的和上面段落的距离将为20px</p>
可以理解成中间div宽度为0且上下边距融合,只取margin的最大值。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 《神话时代:重述版》评测:粉丝们的朝花夕拾
- 战锤40K星际战士2先锋职业介绍|先锋职业技能效果一览
- 战锤40K星际战士2重装职业介绍|重装职业技能效果一览
- 交错战线能源发电站推荐角色一览
- 阿波罗乐神之音《第4集出埃及记》[WAV+CUE]
- JohnWrightTrio-SouthSideSoul-1960-2024(24-192)
- AnthonyBruno-Cefalu(2024)[24-96]
- 《地狱潜者2》玩家人数飙升:终于不再削弱玩家了
- 黑神话引力!外媒分享孙悟空英文书籍:美生中国人等
- 《荒野大镖客》PC版曝光!《GTA6》疑似也有PC版
- 林宥嘉.2008-神秘嘉宾(引进版)【华研国际】【WAV+CUE】
- 徐小凤.1997-金曲精选(康艺经典珍藏系列)【康艺成音】【WAV+CUE】
- 群星.1994-非常精丫宝丽金】【WAV+CUE】
- 《赛马娘 热血喧闹大感谢祭!》评测:现代复古派对
- 《星球大战:亡命之徒》评测:还是看看远方的光剑吧