BFC的理解

BFC的理解

BFC是什么?

BFC是块级格式上下文(Block Formatting Context),是CSS布局的一个概念,在BFC布局里面的元素不受外面元素影响。

BFC如何触发?

  • 设置浮动:float有值并不为空 float:left | right;

  • overflow:hidden | scroll | auto; (不是visible)

  • display:inline-block | table-cell | table-caption | flex | grid;( 非none 非inline 非block)

  • 设置绝对定位: position: absolute | fiexed;( 非relative)

BFC的作用

  • 解决margin重叠问题:由于BFC是一个独立的区域,内部元素和外部元素互不影响,将两个元素变为BFC,就解决了margin重叠问题
  • 创建自适应两栏布局:可以用来创建自适应两栏布局,左边宽高固定,右边宽度自适应。
  • 解决高度塌陷问题:在子元素设置浮动后,父元素会发生高度的塌陷,也就是父元素的高度为0解决这个问题,只需要将父元素变成一个BFC。

问题解决

Margin重叠问题

html 复制代码
<div class="div1"></div>
<div class="div1"></div>
css 复制代码
.div1 {
  margin-bottom: 20px;
  width: 100px;
  height: 100px;
  background-color: red;
}
.div2 {
  margin-top: 20px;
  width: 100px;
  height: 100px;
  background-color: blue;
}

会导致上下两个div的margin重合:

html 复制代码
<style>
  .container {
    overflow: hidden;//将它变成BFC
  }
  .div1 {
    margin-bottom: 20px;
    width: 100px;
    height: 100px;
    background-color: red;
  }
  .div2 {
    margin-top: 20px;
    width: 100px;
    height: 100px;
    background-color: blue;
  }
</style>
<body>
  <div class="container">
    <div class="div1"></div>
  </div>
  <div class="container">
    <div class="div2"></div>
  </div>
</body>

Margin塌陷问题

html 复制代码
<style>
  .box {
    width: 100px;
    height: 100px;
    background-color: pink;
  }
  .box1 {
    width: 40px;
    height: 40px;
    margin-top: 50px;
    background-color: lightgreen;
  }
</style>

<body>
  <div class="box">
    <div class="box1"></div>
  </div>
</body>

父元素margin-top:50px,子元素没有margin-top,造成margin塌陷

给父元素变成BFC即可;

html 复制代码
<style>
  .box {
    width: 100px;
    height: 100px;
    background-color: pink;
    overflow: hidden;
  }
  .box1 {
    width: 40px;
    height: 40px;
    margin-top: 50px;
    background-color: lightgreen;
  }
</style>

<body>
  <div class="box">
    <div class="box1"></div>
  </div>
</body>

高度塌陷

html 复制代码
<style>
  .father {
    width: 200px;
    background-color: lightseagreen;
  }
  .child1 {
    width: 200px;
    height: 200px;
    float: left;
    background-color: red;
  }
  .child2 {
    width: 200px;
    height: 200px;
    float: left;
    background-color: pink;
  }
</style>
<div class="father">
  <div class="child1"></div>
  <div class="child2"></div>
</div>

会发现父元素的高度为0,造成了高度塌陷;

html 复制代码
<style>
  .father {
    width: 200px;
    height: 500px;
    background-color: lightseagreen;
    overflow: hidden;
  }
  .child1 {
    width: 200px;
    height: 200px;
    float: left;
    background-color: red;
  }
  .child2 {
    width: 200px;
    height: 200px;
    float: left;
    background-color: pink;
  }
</style>
<div class="father">
  <div class="child1"></div>
  <div class="child2"></div>
</div>
相关推荐
MiyueFE15 分钟前
14 个逻辑驱动的 UI 设计技巧,助您改善任何界面
前端·设计
啃火龙果的兔子19 分钟前
前端单元测试覆盖率工具有哪些,分别有什么优缺点
前端·单元测试
「、皓子~1 小时前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了1 小时前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_1 小时前
Ajax 入门
前端·javascript·ajax
京东零售技术1 小时前
京东小程序JS API仓颉改造实践
前端
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游1 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟2 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计