外边距合并

顶部外边距和底部外边距有时被组合(折叠)为单个外边距,其大小是组合到其中的最大外边距

发生外边距合并的三种基本情况

1. 相邻的兄弟姐妹元素

<div id="margin_parent">
<div><b>父margin-top: 80px,子margin-top: 50px,则最后margin-top为80px</b></div>
</div>

CSS

#margin_parent{
width: 200px;
height: 200px;
background-color: green;
margin-top:80px;
}
#margin_parent div{
width: 200px;
height: 200px;
background-color: yellow;
margin-top:50px;
opacity: 0.5;
}

2. 块级父元素与其第一个/最后一个子元素

margin-top:块级父元素和其第一个子元素会发生上外边距合并

margin-bottom:块级父元素与它的最后一个子元素会发生下边距合并,要求:父元素没有border、padding、inline content、height、min-height、max-height等

<div id="margin_parent">
<div><b>父margin-top: 80px,子margin-top: 50px,则最后margin-top为80px</b></div>
</div>

CSS

#margin_parent{
width: 200px;
height: 200px;
background-color: green;
margin-top:80px;
}
#margin_parent div{
width: 200px;
height: 200px;
background-color: yellow;
margin-top:50px;
opacity: 0.5;
}

3. 空块元素

如果存在一个空的块级元素,其border、padding、inline content、height、min-height都不存在。那么此时它的上下边距中间将没有任何阻隔,此时它的上下外边距将会合并

<p style="margin-bottom: 0px;background-color:green;">这个段落的和下面段落的距离将为50px</p>
<div style="margin-top: 50px; margin-bottom: 50px;"></div>
<p style="margin-top: 0px;background-color:green;">中间div的设置margin-top: 50px; margin-bottom: 50px;<br/>
两个会折叠成一个50px</p>
 

参考

https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing

最新文章

  1. app使用微信支付成功后,点击返回到该app却跳到另外一个app去了
  2. CodeForces Round#313
  3. Linux启用和配置Java
  4. [ 转 ] scrapy 中解决 xpath 中的中文编码问题
  5. [Android Pro] Test win
  6. ember.js:使用笔记2-数据删除与存储
  7. ajax教程完整版
  8. sql compact 使用EF无法更新的问题?
  9. Linux内存点滴:用户进程内存空间
  10. 奇妙的算法之LCS妙解
  11. Java基础----Java---集合框架---泛型、泛型方法、静态方法泛型、泛型接口、泛型限定、泛型类
  12. python3+selenium入门06-浏览器操作
  13. day 27 Python中进程的操作
  14. 适用于 Windows 的自定义脚本扩展
  15. 洛谷P3302 森林
  16. 丑女贝蒂第一至四季/全集Ugly Betty迅雷下载
  17. 7个华丽的基于Canvas的HTML5动画
  18. 安装Android模拟器
  19. #leetcode刷题之路31-下一个排列
  20. 初探C#

热门文章

  1. datatable转换为list&lt;model&gt; 映射
  2. Git配置信息相关命令
  3. Codeforces 1043F(容斥+dp)
  4. steps/train_sat.sh
  5. 网易云课堂《JS原创视频教程-知识点类》
  6. tmux 简单介绍
  7. sonar排除实体类配置
  8. Python os.removedirs() 和shutil.rmtree() 用于删除文件夹
  9. centos6.9安装crontab
  10. (转!)利用Keras实现图像分类与颜色分类