BFC(Block formatting contexts)初探_html/css_WEB-ITnose
前端工程师求职面试的时候,或多或少都会问到一点BFC(Block formatting contexts)相关的知识。在平时的开发中我也遇到过BFC方面的问题,但是没有好好整理过这方面的知识,所以别人问我的时候,我都不敢说自己知道这么个概念o(╯□╰)o。这两天,我仔细阅读了一些文章来填补这块知识漏洞。
什么是BFC?
在理解BFC是什么之前,需要了解Box、Formatting Context的概念
Box:CSS布局的基本单位
Box是CSS布局的对象和基本单位,直观点来说,就是一个页面是由很多个Box组成的。元素的类型和display属性,决定了这个Box的类型。不同类型的Box,会参与不同的Formatting Context(一个决定如何渲染文档的容器),因此Box内的元素会以不同的方式渲染。盒子分类:
block-level box:display属性为block, list-item, table的元素,会生成block-level box。并且参与block formatting context;
inline-level box:display属性为inline, inline-block, inline-table的元素,会生成inline-level box。并且参与inline formatting context;
run-in box:display属性为run-in的元素,根据上下文决定对象是内联对象还是块级对象。CSS3属性!
Formatting Context
Formatting Context是W3C CSS2.1规范中的一个概念。它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用。最常见的Formatting Context有Block Formatting Context(BFC)和Inline Formatting Context(IFC)。
BFC定义
BFC(Block Formatting Context)块级格式化上下文,它是一个独立的渲染区域,只有Block-level box参与,它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干。
BFC布局规则
内部的Box会在垂直方向,一个接一个地放置
Box垂直方向的距离由margin决定,属于同一个BFC的两个相邻Box的margin会发生重叠
每个元素的margin box的左边,与包含块border box的左边相接触(对于从左往右的格式化,否则相反)
BFC的区域不会与float box重叠
BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此
计算BFC的高度时,浮动元素也参与计算
哪些元素会生成BFC
根元素
float属性不为none
position为absolute或fixed
display为inline-block, table-cell, table-caption, flex, inline-flex
overflow不为visible
BFC作用与原理
1. 自适应两栏布局
1 |
|
显示效果:
根据BFC布局规则第3条:
1 |
|
因此,虽然存在浮动的元素aside,但main的左边依然会与包含块的左边相接触
根据BFC布局规则第四条:
1 |
|
我们可以通过通过触发main生成BFC, 来实现自适应两栏布局:
1 |
|
当触发main生成BFC后,这个新的BFC不会与浮动的aside重叠。因此会根据包含块的宽度,和aside的宽度,自动变窄。效果如下:
2. 清除内部浮动
1 |
|
显示效果:
根据BFC布局规则第六条:
1 |
|
为达到清除内部浮动,我们可以触发par生成BFC,那么par在计算高度时,par内部的浮动元素child也会参与计算
1 |
|
效果如下:
3. 防止垂直margin重叠
1 |
|
显示效果
两个p之间的距离为100px,发生了margin重叠
根据BFC布局规则第二条:
1 |
|
我们可以在p外面包裹一层容器,并触发该容器生成一个BFC。那么两个P便不属于同一个BFC,就不会发生margin重叠了
1 |
|
显示效果:
总结
其实以上的几个例子都体现了BFC布局规则第五条:
1 |
|
因为BFC内部的元素和外部的元素绝对不会互相影响,因此, 当BFC外部存在浮动时,它不应该影响BFC内部Box的布局,BFC会通过变窄,而不与浮动有重叠。同样的,当BFC内部有浮动时,为了不影响外部元素的布局,BFC计算高度时会包括浮动的高度。避免margin重叠也是这样的一个道理。
参考
前端精选文摘:BFC 神奇背后的原理
我对BFC的理解
谈外margin collapsing(外边距叠加)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...
