设置外层元素overflow:hidden/auto触发BFC清除浮动,而display:bloclk没有作用,怎么理解BFC?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .out{
            background-color: blue;
        }
        .in-one{
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }
        .in-two{
            width: 200px;
            height: 100px;
            background-color: yellow;
            float: right;
        }
    </style>
</head>
<body>
    <div class="out">
        <div class="in-one"></div>
        <div class="in-two"></div>
        <div class="clear"></div>
    </div>    


</body>
</html>

结构大概如此,如果要清除浮动,可以设置一个同等级的空标签利用clear:both;也可以将class为out的元素设置为overflow:hidden或overflow:auto
经过查找,之所有overflow的操作可以清除元素,是因为设置overflow会触发BFC(Block Formatting Context),
那么问题是,为什么设置out为display:block没有作用呢?

阅读 4.4k
1 个回答

设置为 overflow:hidden.
你可以直接在out 用伪类来清楚
clearfix:after{content:'.';display:block;clear:both;overflow:hidden;height:0;}
清除浮动有好多种,你这不是最好的一种,你可以总结一下,还有bfc的理解

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题