• 技术文章 >web前端 >css教程

    什么是BFC?深入了解BFC,聊聊作用

    青灯夜游青灯夜游2022-07-08 11:29:00转载182
    什么是BFC?下面本篇文章带大家了解一下BFC,并聊聊BFC的作用。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

    之前在面试字节的时候,面试官问了我有了解BFC吗,我当时其实有看很多文章,但是总是记不住,感觉每个文章讲的都差不多,然后面试时候也没答出来,但是在听了王红元老师讲解的之后,感觉茅塞顿开,所以想分享给大家。以下内容都是根据王红元老师的前端系统课学习的总结,觉得讲的非常清晰,非常感谢王红元老师

    在了解BFC(Block Formatting Context)之前,我们先来看看FC(Formatting Context)是什么:

    1.png

    这段话来自W3C官网,我们可以得到如下信息:

    简单理解:块级元素所在的布局和上下文就是BFC,行内级元素所在的布局和上下文就是IFC

    问题1.块级元素都是在BFC中布局的,那么这个BFC在哪里呢

    首先我们先看一下W3C的官方解释:

    2.png

    MDN上整理出的下了情况会创建BFC:

    由此可见

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
        <div></div>
        <div></div>
      </body>
    </html>

    这段代码中的box1和box2都是在html根元素的BFC中布局的

    问题2.BFC的作用

    首先先看一下官方文档对BFC作用的描述:

    2.png

    我们可以得到的信息:

    作用1:利用BFC解决margin重叠问题

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          .box1 {
            height: 200px;
            width: 400px;
            background-color: orange;
    
            margin-bottom: 30px;
          }
          .box2 {
            height: 150px;
            background-color: purple;
    
            margin-top: 50px;
          }
        </style>
      </head>
      <body>
        <div></div>
        <div></div>
      </body>
    </html>

    此时box1和box1此时同处于html的BFC中,并且box1和box2在垂直方向上相邻,所以会出现margin重叠,取两者其中较大的值50px

    4.png

    如何解决呢?可能很多人会想到直接给box1添加一个BFC,所以直接给box1添加个overflow:hidden属性

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          .box1 {
            height: 200px;
            width: 400px;
            background-color: orange;
    
            margin-bottom: 30px;
    
            overflow: hidden;
          }
          .box2 {
            height: 150px;
            background-color: purple;
    
            margin-top: 50px;
          }
        </style>
      </head>
      <body>
        <div></div>
        <div></div>
      </body>
    </html>

    结果呢?

    5.png

    发现并不起作用。可能此时很多人就懵了,box1此时不是明明已经形成了BFC了嘛,为什么还会发生重叠?让我来给你解释一下,首先此时box1确实是已经形成了BFC(可以理解成box1内部形成了BFC),但是对于box1这个元素的本身,还是和box2同属于html的BFC中,所以还是会发生margin重叠

    所以我们要给box1套一层,然后给box1外层的盒子设置BFC

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          /* 给box1外层增加一个container盒子,并设置BFC */
          .container {
            overflow: hidden;
          }
          .box1 {
            height: 200px;
            width: 400px;
            background-color: orange;
    
            margin-bottom: 30px;
          }
          .box2 {
            height: 150px;
            background-color: purple;
    
            margin-top: 50px;
          }
        </style>
      </head>
      <body>
        <div>
          <div></div>
        </div>
        <div></div>
      </body>
    </html>

    此时box1属于container的BFC,而box2属于html的BFC,不属于同一个BFC,所以就能解决margin重叠问题

    6.png

    作用2:解决浮动高度塌陷问题

    当我们给container里面的四个item设置浮动的时候,很明显,这几个元素都会脱离文档流,此时container不会有高度

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          .container {
            background-color: orange;
          }
    
          .item {
            width: 400px;
            height: 200px;
            box-sizing: border-box;
            border: 1px solid #000;
            float: left;
            background-color: #f00;
          }
        </style>
      </head>
      <body>
        <div>
          <div></div>
          <div></div>
          <div></div>
          <div></div>
        </div>
      </body>
    </html>

    7.png

    很多网上博主说,通过给container设置一个BFC,内部的浮动元素就会向其汇报高度,然后container就能解决浮动高度塌陷问题,这个做法是正确的,但是这个说法其实是错误,并不是因为其内部的浮动元素向其汇报了高度

    事实上,想通过BFC解决高度塌陷问题需要满足两个条件:

    首先我们先看一段W3C的描述

    8.png

    大致意思为BFC的高度是auto情况下,高度是这样计算:

    所以我们直接给container通过添加overflow属性设置BFC,则由于上述第四条4特性,container会增加高度来包括内部四个item浮动元素下边缘,所以解决了浮动塌陷问题

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          .container {
            background-color: orange;
            /* 通过overflow形成BFC */
            overflow: hidden;
          }
    
          .item {
            width: 400px;
            height: 200px;
            box-sizing: border-box;
            border: 1px solid #000;
            float: left;
            background-color: #f00;
          }
        </style>
      </head>
      <body>
        <div>
          <div></div>
          <div></div>
          <div></div>
          <div></div>
        </div>
      </body>
    </html>

    9.png

    (学习视频分享:web前端入门

    以上就是什么是BFC?深入了解BFC,聊聊作用的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:掘金社区,如有侵犯,请联系admin@php.cn删除
    专题推荐:BFC css
    上一篇:CSS3动画实战之:超酷炫的粘性气泡效果 下一篇:利用纯CSS如何在滚动时自动添加头部阴影
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• css3的新增属性有没有设置文本溢出的样式• css3中什么是用来设置文本高度的• 深入探究CSS鼠标指针交互效果• 纯CSS3怎么实现波浪效果?(代码示例)• CSS3动画实战之:超酷炫的粘性气泡效果
    1/1

    PHP中文网