BFC 的原理和功能

BFC 的含义

  1. BFCblock formatting context)块级格式化上下文,它是页面中的一块渲染区域,并且有一套属于自己的渲染规则,它决定了元素如何对齐内容进行布局,以及与其他元素的关系和相互作用,当涉及到可视化布局的时候,BFC 提供了一个环境,HTML 元素在这个环境中按照一定规则进行布局;

  2. 具有 BFC 特性的元素可以看做是隔离了的独立容器,容器里面的元素不会在布局上影响到外面的元素,并且 BFC 具有普通容器所没有的的一些特性;

  3. BFC 是一个独立的布局环境,内部的元素布局与外部互不影响;

BFC 的布局规则

  1. 内部的 Box 会在垂直方向,一个个地放置,Box 垂直方向的距离由 margin 决定,属于同一个 BFC 的两个相邻 Box 的上下 margin 会发生重叠,每个元素的左边,与包含的盒子的左边相接触,即使存在浮动也是如此;

  2. BFC 的区域不会与浮动 Box 重叠;

  3. BFC 就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素,反之也如此;

  4. 计算 BFC 的高度时,浮动元素也参与计算;

触发 BFC 的条件

  1. 常见的方式:

    元素或属性 属性值
    float left、right
    postion absolute、fixed
    overflow auto、scroll、hidden
    display inline-block、table-cell、flex、grid、flow-root、…
  2. 完整的 BFC 触发方式

BFC 的用处

让浮动内容和周围的内容等高

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>让浮动内容和周围的内容等高</title>
    <style>
      .box1 {
          background-color: rgb(224, 206, 247);
          border: 5px solid rebeccapurple;
          /* 
          * BFC 的区域不会与 float box 重叠;
          * 计算 BFC 的高度时,浮动元素也参与计算; 
          */
          overflow: hidden;
      }
      .float1 {
          width: 200px;
          height: 150px;
          background-color: white;
          border: 1px solid black;
          padding: 10px;
          float: left;
      }
    </style>
</head>
<body>
  <div class="box1">
    <div class="float1">I am a floated box!</div>
    <p>I am content inside the container.</p>
  </div>
</body>
</html>
让浮动内容和周围的内容等高
I am a floated box!

I am content inside the container.

外边距折叠

  1. 原因:Box 垂直方向的距离由 margin 决定;属于同一个 BFC 的两个相邻 Boxmargin 会发生重叠;

  2. 解决方法:给上 box 或者下 box 任意一个包裹新的 box 并开启 BFC

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>外边距折叠</title>
  <style>
    .p1,.p2 {
        color: #f55 !important;
        background: #fcc !important;
        width: 200px !important;
        line-height: 100px !important;
        text-align: center !important;
    }
    .p1 {
        margin-bottom: 20px !important;
    }
    .p2 {
        margin-top: 30px !important;
    }
    .box {
        /* 
        * 盒子垂直方向的距离由 margin 决定,属于同一个BFC的两个相邻Box的上下margin会发生重叠; 
        * 使用box 将同处在同一个bfc下的两个 p标签 分开,破坏原来的bfc
        */
        overflow: hidden !important;
    }
  </style>
</head>
<body>
  <p class="p1">你好</p>
  <div class="box">
    <p class="p2">你好</p>
  </div>
</body>
</html>
外边距折叠

你好

你好

清除浮动

  1. 解决方法:给父元素开启 BFC

  2. 原理:计算 BFC 的高度时,浮动子元素也参与计算;

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>清除浮动</title>
  <style>
    .container33 {
      width: 300px;
      border: 5px solid #fcc;
      overflow: hidden;
    }
    .box33 {
      width: 100px;
      height: 100px;
      float: left;
      border: 5px solid #f66;
    }
    .test{
      padding: 10px 0;
      background: #eee;
    }
  </style>
</head>
<body>
  <div class="container33">
    <div class="box33"></div>
    <div class="box33"></div>
  </div>
  <div class="test">用来测试是否清除了浮动</div>
</body>
</html>
清除浮动
用来测试是否清除了浮动

自适应的两列布局

  1. 解决方法:给父元素开启 BFC

  2. 原理:BFC 的区域不会与浮动 box 重叠;

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>自适应两栏布局</title>
  <style>
    /* 
    * 两大css原理:
    *  1.层叠上下文 - 层叠顺序  浮动 > 块状
    *  2.BFC - 元素不会与浮动元素产生重叠
    */
    .aside11 {
      float: left;
      width: 100px;
      height: 150px;
      background: #f66;
    }
    .main11 {
      overflow: auto;
      height: 200px;
      background: #fcc;
    }
  </style>
</head>
<body>
  <div class="aside11"></div>
  <div class="main11"></div>
</body>
</html>
自适应两栏布局

IFC 的原理和功能

IFC 的含义

  1. IFC(inline Formatting Context)叫做 内联格式化上下文

  2. 内部的元素从包含块的顶部开始,从左至右(默认)排列成一行形成的一个矩形盒子叫做 line box

IFC 的布局规则

  1. box 是水平一个接一个的排列,水平的 marginpaddingborder 是可以有的;

  2. 垂直方向的对齐,可能是底部对齐,顶部对齐,也可能是基线对齐(这个是默认的);

  3. 行框中的内联 box 的高度小于行框的高度时,内联盒子的垂直方向的对齐方式取决于 vertical-align 属性;

  4. 当一个行框水平不能容纳内联 box 时,将会在垂直方向上产生多个行框,上下一个挨着一个,但是不会重叠;

  5. 多个内联盒子的宽度小于包含他们的行框时,在水平方向的分布取决于 text-align 属性;

IFC 的作用

水平居中

<style>
  .box_0 {
    width: 500px;
    height: 100px;
    background-color: rosybrown;
    text-align: center;
  }
  .child_0 {
    display: inline-block;
    width: 80px;
    height: 50px;
    background-color: seagreen;
  }
</style>
<div class="box_0">
    <div class="child_0"></div>
    <div class="child_0"></div>
</div>

垂直居中

<style>
  .box_1 {
    width: 500px;
    background-color: rosybrown;
    display: inline-block;
  }
  .child_1 {
    display: inline-block;
    width: 80px;
    height: 50px;
    background-color: seagreen;
    vertical-align: middle;
  }
</style>
<div class="box_1">
  <div class="child_1"></div>
  <div class="child_1" style="height: 100px;"></div>
</div>
打赏作者
您的打赏是我前进的动力
微信
支付宝
评论

中午好👏🏻,我是 ✍🏻   疯狂 codding 中...

粽子

这有关于前端开发的技术文档和你分享。

相信你可以在这里找到对你有用的知识和教程。

了解更多

目录

  1. 1. BFC 的原理和功能
    1. 1.1. BFC 的含义
    2. 1.2. BFC 的布局规则
    3. 1.3. 触发 BFC 的条件
    4. 1.4. BFC 的用处
      1. 1.4.1. 让浮动内容和周围的内容等高
      2. 1.4.2. 外边距折叠
      3. 1.4.3. 清除浮动
      4. 1.4.4. 自适应的两列布局
  2. 2. IFC 的原理和功能
    1. 2.1. IFC 的含义
    2. 2.2. IFC 的布局规则
    3. 2.3. IFC 的作用
      1. 2.3.1. 水平居中
      2. 2.3.2. 垂直居中