BFC
- BFC
- 什么是 BFC?
- 元素開啟 BDC 后的特殊布局效果
- 元素開啟 BFC 的方式
BFC
什么是 BFC?
- 官方解釋:A block formatting context (BFC) is a part of a visual CSS rendering of a web page. It’s the region in which the layout of block boxes occurs and in which floats interact with other elements.
- 通俗理解:BFC(塊級格式化上下文)是一種在布局上具有特定效果的渲染區域。創建/開啟 BFC 意味著元素被激活了這種特殊的布局效果,從而使其內部的元素布局和浮動元素的交互方式與默認情況有所不同。
元素開啟 BDC 后的特殊布局效果
- 官方解釋
- contain internal floats.
- suppress margin collapsing.
- exclude external floats.
- 通俗理解
- 元素中的子元素浮動時,自身高度不會塌陷
- 元素中的子元素不會產生 margin 塌陷問題
- 元素不會被外部的浮動元素所覆蓋
元素開啟 BFC 的方式
常見的元素開啟 BFC 的方式如下,
- 根元素
<html>
==> The root element of the document (). - 浮動元素 ==> Floats (elements where float isn’t none).
- 絕對定位或固定定位的元素 ==> 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).
- overflow: hidden/scroll/auto 的元素 ==> Block elements where overflow has a value other than visible and clip.
- 伸縮項目 ==> Flex items (direct children of the element with display: flex or inline-flex) 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 的元素 ==> column-span: all should always create a new formatting context, even when the column-span: all element isn’t contained by a multicol container
- display: flow-root 的元素,副作用最低! ==> display: flow-root.