全称: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’tnone
).- Absolutely positioned elements (elements where
position
isabsolute
orfixed
).- 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), orinline-table
.- Block elements where
overflow
has a value other thanvisible
.display
: flow-root
.- Elements with
contain
: layout
,content
, orpaint
.- Flex items (direct children of the element with
display
: flex
orinline-flex
) if they are neither flex nor grid nor table containers themselves.- Grid items (direct children of the element with
display
: grid
orinline-grid
) if they are neither flex nor grid nor table containers themselves.- Multicol containers (elements where
column-count
orcolumn-width
isn’tauto
, including elements withcolumn-count: 1
).column-span
: all
should always create a new formatting context, even when thecolumn-span: all
element isn’t contained by a multicol container (Spec change, Chrome bug).
翻译
- 根元素(
<html>)
- 浮动元素(元素的
float
不是none
) - 绝对定位元素(元素的
position
为absolute
或fixed
) - 行内块元素(元素的
display
为inline-block
) - 表格单元格(元素的
display
为table-cell
,HTML表格单元格默认为该值) - 表格标题(元素的
display
为table-caption
,HTML表格标题默认为该值) - 匿名表格单元格元素(元素的
display
为table、``table-row
、table-row-group、``table-header-group、``table-footer-group
(分别是HTML table、row、tbody、thead、tfoot 的默认属性)或inline-table
) overflow
值不为visible
的块元素display
值为flow-root
的元素contain
值为layout
、content
或 paint 的元素- 弹性元素(
display
为flex
或inline-flex
元素的直接子元素) - 网格元素(
display
为grid
或inline-grid
元素的直接子元素) - 多列容器(元素的
column-count
或column-width
不为auto,包括 ``column-count
为1
) column-span
为all
的元素始终会创建一个新的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>
效果:
Id1 与id2 id2-1都发生了边距重叠,中间的marigin
为30px
,相当于id2不存在一样。当把id2overflow
改为hidden
后来看看效果:
id2与id1发生了正常的BFC内的边距重叠,id2-1的margin回来了。
这就是BFC合并与margin。
参考
总结
BFC真是个令人头疼的知识点,它就相当于一个盒子,盒子内外互不影响。这是我对BFC最初的理解吧,然后到现在工作中也没有碰到很复杂的BFC,所以有什么写的不对的地方,欢迎指出更正。当然如果觉得不错并且有帮助的话,欢迎点赞👍转发
哦,转发注明一下来源就行。