【前端面经】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.
相关推荐
lichenyang45342 分钟前
React ajax中的跨域以及代理服务器
前端·react.js·ajax
呆呆的小草44 分钟前
Cesium距离测量、角度测量、面积测量
开发语言·前端·javascript
一 乐2 小时前
民宿|基于java的民宿推荐系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·源码
testleaf2 小时前
前端面经整理【1】
前端·面试
好了来看下一题2 小时前
使用 React+Vite+Electron 搭建桌面应用
前端·react.js·electron
啃火龙果的兔子2 小时前
前端八股文-react篇
前端·react.js·前端框架
小前端大牛马2 小时前
react中hook和高阶组件的选型
前端·javascript·vue.js
刺客-Andy2 小时前
React第六十二节 Router中 createStaticRouter 的使用详解
前端·javascript·react.js
萌萌哒草头将军4 小时前
🚀🚀🚀VSCode 发布 1.101 版本,Copilot 更全能!
前端·vue.js·react.js
GIS之路4 小时前
OpenLayers 图层叠加控制
前端·信息可视化