【前端面经】BFC

BFC

  • BFC
    • [什么是 BFC?](#什么是 BFC?)
    • [元素开启 BDC 后的特殊布局效果](#元素开启 BDC 后的特殊布局效果)
    • [元素开启 BFC 的方式](#元素开启 BFC 的方式)

BFC

什么是 BFC?

  1. 官方解释: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.
  2. 通俗理解 :BFC(块级格式化上下文)是一种在布局上具有特定效果的渲染区域 。创建/开启 BFC 意味着元素被激活了这种特殊的布局效果,从而使其内部的元素布局和浮动元素的交互方式与默认情况有所不同。

元素开启 BDC 后的特殊布局效果

  1. 官方解释
    • contain internal floats.
    • suppress margin collapsing.
    • exclude external floats.
  2. 通俗理解
    • 元素中的子元素浮动时,自身高度不会塌陷
    • 元素中的子元素不会产生 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.
相关推荐
Bigger2 小时前
Tauri (26)——托盘图标总对不上系统主题?一行 Template Image 搞定
前端·rust·app
kyriewen4 小时前
面试官问你:“AI 能写 80% 的代码了,公司为什么还需要你?”
前端·javascript·面试
甲维斯5 小时前
又升级咯!坦克大战2026,科技与复古并存!
前端·人工智能·游戏开发
搬砖的码农7 小时前
(08)为什么我的 Agent 一跑后台服务就卡死
前端·agent·ai编程
飘尘7 小时前
前端转全栈(Java 后端)必须要知道的:开发中的锁机制与分布式并发控制
前端·后端·全栈
亲亲小宝宝鸭7 小时前
前端性能监控:web-vitals
前端·性能优化·监控
陆枫Larry8 小时前
可滚动页面背景填不满:`height: 100vh` vs `min-height: 100vh`
前端
Patrick_Wilson8 小时前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员
kyriewen8 小时前
今天的科技圈,全在抢英伟达的饭碗
前端·面试·ai编程