关于BFC

107 阅读1分钟

通俗来说就是脱离了正常的排版,比如用了浮动,下方的内容,就会自动网上排

生成bfc的五种方式

1、根元素

2、float属性不为none

3、 position为absolute或fixed

4、display为inline-block, table-cell, table-caption, flex, inline-flex

5、overflow不为visible

作用说白了就是用来实现各种布局

解决的办法可以是给父级加样式

 .box1::after {
        content: "";
        clear: left;
        display: block;
      }

全部代码


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 500px;
        height: 500px;
        border: 1px solid black;
      }

      .box2 {
        border: 1px solid rgb(0, 0, 0);
        width: 150px;
        height: 100px;
        float: left;
      }
      .box3 {
        border: 10px solid rgb(162, 63, 63);
        width: 100px;
        height: 200px;
      }

      .box1::after {
        content: "";
        clear: left;
        display: block;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="box1">
        <div class="box2">111111111</div>
      </div>
      <div class="box3">111111111</div>
    </div>
  </body>
</html>