--- title: 块格式化上下文 slug: Web/Guide/CSS/Block_formatting_context tags: - BFC - Block Formatting Context - CSS - 块格式化上下文 translation_of: Web/Guide/CSS/Block_formatting_context ---
{{ CSSRef() }}
块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视CSS渲染的一部分,是块盒子的布局过程发生的区域,也是浮动元素与其他元素交互的区域。
下列方式会创建块格式化上下文:
<html>)none)absolute 或 fixed)inline-block)table-cell,HTML表格单元格默认为该值)table-caption,HTML表格标题默认为该值)table、table-row、 table-row-group、table-header-group、table-footer-group(分别是HTML table、row、tbody、thead、tfoot 的默认属性)或 inline-table)visible 的块元素flow-root 的元素layout、content 或 paint 的元素flex 或 inline-flex 元素的直接子元素)grid 或 inline-grid 元素的直接子元素)auto,包括 column-count 为 1)column-span 为 all 的元素始终会创建一个新的BFC,即使该元素没有包裹在一个多列容器中(标准变更,Chrome bug)。块格式化上下文包含创建它的元素内部的所有内容.
块格式化上下文对浮动定位(参见 {{ cssxref("float") }})与清除浮动(参见 {{ cssxref("clear") }})都很重要。浮动定位和清除浮动时只会应用于同一个BFC内的元素。浮动不会影响其它BFC中元素的布局,而清除浮动只能清除同一BFC中在它前面的元素的浮动。外边距折叠(Margin collapsing)也只会发生在属于同一BFC的块级元素之间。
为了更好的理解BFC,我们先看看下面这些。
在下面的例子中,我们让 <div> 元素浮动,并给它一个边框效果。<div> 里的内容现在已经在浮动元素周围浮动起来了。由于浮动的元素比它旁边的元素高,所以 <div> 的边框穿出了浮动。正如我们在这篇文章中 guide to in-flow and out of flow elements 解释的,浮动脱离了文档流,所以 <div> 的 background 和 border 仅仅包含了内容,不包含浮动。
overflow: auto创建一个会包含这个浮动的 BFC,通常的做法是设置父元素 overflow: auto 或者设置其他的非默认的 overflow: visible 的值。
设置 overflow: auto 创建一个新的BFC来包含这个浮动。我们的 <div> 元素现在变成布局中的迷你布局。任何子元素都会被包含进去。
使用 overflow 来创建一个新的 BFC,是因为 overflow 属性告诉浏览器你想要怎样处理溢出的内容。当你使用这个属性只是为了创建 BFC 的时候,你可能会发现一些不想要的问题,比如滚动条或者一些剪切的阴影,需要注意。另外,对于后续的开发,可能不是很清楚当时为什么使用 overflow。所以你最好添加一些注释来解释为什么这样做。
display: flow-root一个新的 display 属性的值,它可以创建无副作用的 BFC。在父级块中使用 display: flow-root 可以创建新的 BFC。
给 <div> display: flow-root 属性后,<div> 中的所有内容都会参与 BFC,浮动的内容不会从底部溢出。
关于值 flow-root的这个名字,当你明白你实际上是在创建一个行为类似于根元素 (浏览器中的<html>元素) 的东西时,就能发现这个名字的意义了——即创建一个上下文,里面将进行 flow layout。
<div class="blue"></div>
<div class="red-outer">
<div class="red-inner">red inner</div>
</div>
.blue, .red-inner {
height: 50px;
margin: 10px 0;
}
.blue {
background: blue;
}
.red-outer {
overflow: hidden;
background: red;
}
{{EmbedGHLiveSample("css-examples/flow/formatting-contexts/float.html", '100%', 720)}}
{{EmbedGHLiveSample("css-examples/flow/formatting-contexts/bfc-overflow.html", '100%', 720)}}
{{EmbedGHLiveSample("css-examples/flow/formatting-contexts/bfc-flow-root.html", '100%', 620)}}
In the following example, we are using display:flow-root and floats to implement double columns layout, beacuse an element in the normal flow that establishes a new BFC must not overlap the margin box of any floats in the same block formatting context as the element itself.
<section> <div class="float">Try to resize this outer float</div> <div class="box"><p>Normal</p></div> </section> <section> <div class="float">Try to resize this outer float</div> <div class="box" style="display:flow-root"><p><code>display:flow-root</code><p></div> </section>
section {
height:150px;
}
.box {
background-color: rgb(224, 206, 247);
border: 5px solid rebeccapurple;
}
.box[style] {
background-color: aliceblue;
border: 5px solid steelblue;
}
.float {
float: left;
overflow: hidden; /* required by resize:both */
resize: both;
margin-right:25px;
width: 200px;
height: 100px;
background-color: rgba(255, 255, 255, .75);
border: 1px solid black;
padding: 10px;
}
{{EmbedLiveSample("example2", 200, 300)}}
Rather than inline-blocks with width:<percentage>, in this case we don't have to specify the width of the right div.
Note that flexbox is a more efficient way to implement muti columns layout in morden CSS.
创建新的 BFC 避免两个相邻 <div> 之间的 外边距合并 问题
<div class="blue"></div> <div class="red-outer"> <div class="red-inner">red inner</div> </div>
.blue, .red-inner {
height: 50px;
margin: 10px 0;
}
.blue {
background: blue;
}
.red-outer {
overflow: hidden;
background: red;
}
{{EmbedLiveSample("Margin_collapsing", 120, 120)}}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('CSS3 Display', '#block-formatting-context', 'Block Formatting Context')}} | {{Spec2('CSS3 Display')}} | define BFC(abbr) etc. |
| {{SpecName('CSS2.1', 'visuren.html#block-formatting', 'Block Formatting Context')}} | {{Spec2('CSS2.1')}} | Initial definition. |