详解CSS外边距折叠引发的问题
第一种:两个块级元素的上下边距折叠 第二种:父元素和子元素(或者最后一个元素)的外边距 第三种:空的块级元素的上下外边距 折叠的根本原因: margin之间直接接触,没有阻隔 折叠后外边距的计算: 1.如果两个外边距都是正值,折叠后的外边距取较大的一个 解决方案:解决方法实际上也就是阻止外边距直接接触 第一种、第三种:只有静态流的元素才会发生外边距合并故设置float position inline-block都可以 <style> .bother{ width: 50px; height: 50px; margin: 50px; background-color: #44cc00; /*1.float: left;*/ /*2.position: absolute;*/ <!--3.display: inline-block;--> } /*.father{*/ /*2.position: relative;*/ /*background:#cccdd1;*/ /*}*/ /*.bother1{*/ /*2.top:50px;*/ /*}*/ /*.bother2{*/ /*2.top:250px;*/ /*}*/ </style> <body> <div class="father"> <div class="bother1 bother"></div> <div class="bother2 bother"></div> </div> </body> 第二种(嵌套的情况)只要border padding非0或者有inline元素隔开,比如在父元素里加一行文字也可以 <style> .margin-box{ width: 50px; height: 50px; /*margin: 50px;设置了上下左右的外边距*/ margin: 50px; /*margin-left: 50px;*/ /*margin-right: 50px;*/ /*div是块级元素,所以设置左右外边距也不会使父元素有左右外边距*/ background-color: #fae900; /*5.2 display: inline-block;*/ } .father{ <!--3.overflow: hidden;--> background:#cccdd1; /*1.border: 1px solid;*/ /*2.padding: 20px;*/ /*5.1 display: inline-block;*/ /*如果没有border和padding只有测试这个字,那么子元素的外边距不会在父元素里显示*/ /*而仅仅只有上外边距显示,下外边距不显示*/ /*而如果在子元素下面同样写一个测试,那么下外边距也会显示*/ } </style> </head> <body> <div class="father"> <!--4.<span>测试</span>--> <div class="margin-box"></div> <!--4.<span>测试</span>--> </div> </body> (编辑:92站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 数据中心机房监控系统的重要性
- 使用Flex布局实现头部固定内容区域滚动的方法
- 服务器日志Tencent Virtual Printer Driver未知的问题解析
- 想在Ubuntu中使用深度桌面?UbuntuDDE Remix21.10来了!
- DedeCMS织梦首页缩略图变形的原由及解决方法
- CSS3中动画属性transform、transition和animation属性的区别
- 用Windows Media Service建设的流媒体直播系统
- 详解CentOS 7迅速安装Mongodb
- 对标Chrome OS 关于Windows 11 SE系统的重要情节
- 谷歌 Chrome浏览器对 Windows 7的支持会延长到 2023 年