浅谈CSS的BFC

什么是BFC

BFC是块级格式上下文,它是指一个独立的块级渲染区域。只有Block-level BOX参与,该区域拥有一套渲染规则来约束块级盒子的布局,且与外部区域无关。

从一个现象说起

  • 一个父盒子如果不设置height,当内容子元素都浮动时,父盒子的高度是0,子盒子不会撑起父盒子的高度。出现上面的问题的原因是因为父盒子没有形成BFC。

image.png

image.png

解决方法

  1. float的值不是none,可以是left/right
  2. position的值不是static/relative
  3. display的值是inline-block、flex或者inline-flex
  4. overflow:hidden 四种方法中之后最后这一种方法比较实用,效果是最好的,这个不会影响其他布局
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BFC</title>
    <style>
    想要解决上面的现象,我们就要创建/让父盒子形成一个BFC。需要给fater添加如下属性:
        1. float的值不是none,可以是left/right
        2. position的值不是static/relative
        3. display的值是inline-block、flex或者inline-flex
        4. overflow:hidden
        .father {
            /* float: left;
            position: fixed;
            display: inline-block; */
            overflow: hidden;
        }
        .son {
            width: 100px;
            height: 100px;
            background-color: blue;
            float: left;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
        <div class="son"></div>
        <div class="son"></div>
    </div>
</body>
</html>

复制代码

BFC的其他作用

  • 可以取消盒子的margin塌陷问题 这里父亲是给了高度的

image.png

image.png

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BFC</title>
    <style>
        .father {
            /* float: left;
            position: fixed;
            display: inline-block; */
            overflow: hidden;
            
            
            width: 500px;
            height: 300px;
            background-color: red;
        }
        .son {
            width: 100px;
            height: 100px;
            background-color: blue;
            margin-top: 30px;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

复制代码
  • 可以组织元素被浮动元素覆盖 这里父亲也给了高度

image.png

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BFC</title>
    <style>
        .son {
            float: left;
            width: 200px;
            height: 250px;
            background-color: red;
        }

        .son2 {
            /*float: left;*/
            /*position: fixed;*/
            /*display: inline-block;*/
            overflow: hidden;

            width: 200px;
            height: 300px;
            background-color: blue;
        }
    </style>
</head>
<body>
<div>
    <div class="son"></div>
    <div class="son2"></div>
</div>
</body>
</html>

复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享