[深入]BFC--Block Formatting Context

全称:Block Formatting Context(块级格式化上下文)

里面有一些概念:

1. Block Container

里面有BFC的盒

那么有哪些盒属于Block Container呢?

  • block
  • inline-block
  • table-cell
  • table-caption(表格标题)
  • flex item (父元素被设为display:flex,子元素如果没有其他的设置,就是Block Container, 下同)
  • grid cell

2. Block-level Box

外面有BFC

  • block
  • flex
  • table
  • grid

3. Block box

里外都有BFC

Block box = Block-level Box + Block Container

4. 什么属性会产生BFC

定义
  • The root element of the document (<html>).
  • Floats (elements where float isn’t none).
  • Absolutely positioned elements (elements where position is absolute or fixed).
  • Inline-blocks (elements with display: inline-block).
  • Table cells (elements with display: table-cell, which is the default for HTML table cells).
  • Table captions (elements with display: table-caption, which is the default for HTML table captions).
  • Anonymous table cells implicitly created by the elements with display: table, table-row, table-row-group, table-header-group, table-footer-group (which is the default for HTML tables, table rows, table bodies, table headers, and table footers, respectively), or inline-table.
  • Block elements where overflow has a value other than visible.
  • display: flow-root.
  • Elements with contain: layout, content, or paint.
  • Flex items (direct children of the element with display: flex or inline-flex) if they are neither flex nor grid nor table containers themselves.
  • Grid items (direct children of the element with display: grid or inline-grid) if they are neither flex nor grid nor table containers themselves.
  • Multicol containers (elements where column-count or column-width isn’t auto, including elements with column-count: 1).
  • column-span: all should always create a new formatting context, even when the column-span: all element isn’t contained by a multicol container (Spec change, Chrome bug).
翻译
  • 根元素(<html>)
  • 浮动元素(元素的 float 不是 none
  • 绝对定位元素(元素的 positionabsolutefixed
  • 行内块元素(元素的 displayinline-block
  • 表格单元格(元素的 displaytable-cell,HTML表格单元格默认为该值)
  • 表格标题(元素的 displaytable-caption,HTML表格标题默认为该值)
  • 匿名表格单元格元素(元素的 displaytable、``table-rowtable-row-group、``table-header-group、``table-footer-group(分别是HTML table、row、tbody、thead、tfoot 的默认属性)或 inline-table
  • overflow 值不为 visible 的块元素
  • display 值为 flow-root 的元素
  • contain 值为 layoutcontent或 paint 的元素
  • 弹性元素(displayflexinline-flex元素的直接子元素)
  • 网格元素(displaygridinline-grid 元素的直接子元素)
  • 多列容器(元素的 column-countcolumn-width 不为 auto,包括 ``column-count1
  • column-spanall 的元素始终会创建一个新的BFC,即使该元素没有包裹在一个多列容器中(标准变更Chrome bug)。
整理

因为不是很好记,所以可以这样定义:

默认这些能容纳正常流的盒,都认为会创建BFC,除一种情况之外:

里外都是BFC(Block Box),并且overflow:visible,相当于没有BFC,会产生BFC合并

BFC 合并

float
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>BFC合并与float</title>
  <style>
  </style>
</head>
<body style="height: 500px;background-color: blanchedalmond">
<!--BFC-->
  <div id="id1" style="float:right;width: 100px;height: 100px;background-color: aqua"></div>
<!--overflow:visible/hidden-->
  <div id="id2" style="background-color: pink;overflow: visible;margin: 30px;">
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字
    文字文字文字文字文字文字

  </div>
</body>
</html>

效果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S5ebKyJu-1607418117472)(https://i.loli.net/2020/09/04/sAkTPfwRKV3buaZ.png)]

当id2为overflow: visible;时,id1的浮动与id2内文字形成了浮动关系,直接就不管id2了。但是当改成overflow: hidden后,来看看效果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mfV0EOKV-1607418117474)(https://i.loli.net/2020/09/04/1MHZDIKda2qrOGk.png)]

id1与id2形成了浮动关系,id2围绕id1了。这就是BFC合并与float

边距重叠
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <div id="id1" style="width: 100px;height: 100px;background-color: pink;margin: 20px"></div>
  <div id="id2" style="overflow: visible;background-color: aqua;margin: 30px;">
    <div id="id2-1" style="width: 100px;height: 100px;background-color: pink;margin: 20px;"></div>
  </div>
</body>
</html>

效果:

BFC__margin1.png

Id1 与id2 id2-1都发生了边距重叠,中间的marigin30px,相当于id2不存在一样。当把id2overflow改为hidden后来看看效果:

BFC__margin2.png

id2与id1发生了正常的BFC内的边距重叠,id2-1的margin回来了。

这就是BFC合并与margin。

参考

简述你对BFC规范的理解

块格式化上下文

总结

BFC真是个令人头疼的知识点,它就相当于一个盒子,盒子内外互不影响。这是我对BFC最初的理解吧,然后到现在工作中也没有碰到很复杂的BFC,所以有什么写的不对的地方,欢迎指出更正。当然如果觉得不错并且有帮助的话,欢迎点赞👍转发哦,转发注明一下来源就行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值