css - 关于margin折叠的一个问题。

查看:133
本文介绍了css - 关于margin折叠的一个问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我在阅读CSS2.2,但是我真的无法理解这句话的意思,以及它所适合的场景:

If the top and bottom margins of an element with clearance p. 157 are adjoining, its margins collapse with the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block.

StackOverflow上有个家伙也提了一样的问题,回答者的答案我也不理解。。。


借用采纳者的知乎链接,这句的意思是:

If the top and bottom margins of an element with clearance are adjoining, its margins collapse with the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block.(有间隙不折叠)

哪,什么才是有间隙呢?这里的间隙clearance在css2.2中有定义。来个例子,先有个直观的理解吧:

<html>
<head>
    <style>
        * {padding: 0; margin: 0;}
        #b1 {
            width: 300px;
            height: 100px;
            margin-bottom: 4em;
            background-color: #0d8ba1;
        }
        #F {
            float: left;
            width: 100px;
            height: 70px;
            background-color: yellow;
        }
        #b2 {
            /*clear: both;*/
            width: 300px;
            height: 100px;
            background-color: red;
            margin-top: 2em;
        }
    </style>
</head>
<body>
<div id="b1"></div>
<div id="F"></div>
<div id="b2"></div>
</body>
</html>

运行时,对比#b2有clear:both与没clear:both时,b1与b2margin的折叠情况。

设置clear:both之后,#b2为了避开浮动的F,往下移动,出现了clearance也就是空隙,b1与b2不再折叠!

另外,stack overflow那个家伙的答案我没再细究。pass。

解决方案

可以看这里:https://www.zhihu.com/question/24563593

这篇关于css - 关于margin折叠的一个问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆