【前端面经】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.
相关推荐
Setsuna_F_Seiei6 小时前
前端的 AI 学习之路 01 之 Agent API 调用 - 和 Agent 的基础对话
前端·人工智能·ai编程
threerocks6 小时前
AI 原生软件开发生命周期手册 - 如何借助 AI,逐阶段改造软件开发生命周期
前端·javascript·后端
徐小夕6 小时前
3分钟从想法到Agent上线:我们开源了一款AI可视化工作流“IDE”
前端·算法·github
why技术8 小时前
eli5,我觉得这个全网在吹的技能,使用体验真的很一般啊。
前端·人工智能·后端
excel8 小时前
记录升级 nuxt3 到 nuxt4 记录
前端
剑胆琴心静水深流9 小时前
全栈之路6---web集成与呈现
前端·vue.js·spring boot·分布式·spring·前端框架·npm
前端snow9 小时前
ai agent -- Memory汇总
前端
ITresearchGuest9 小时前
AI 焦虑下,前端该何去何从
前端·人工智能
Codiggerworld10 小时前
Chrome DevTools 隐藏神技:这 5 个调试技巧你可能从来没用过
前端·chrome·chrome devtools
旋生万物10 小时前
【终极实战】用Python从零“生成“一个宇宙:螺旋干涉模型的代码实现
开发语言·前端·人工智能·react.js·php·wpf