search
HomeWeb Front-endCSS TutorialDetailed explanation of CSS BFC principle and its application
Detailed explanation of CSS BFC principle and its applicationFeb 03, 2018 am 09:48 AM
cssDetailed explanation

This article mainly introduces to you the relevant information for understanding the BFC principle and its application in 10 minutes. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

1. Common positioning solutions

Before talking about BFC, let’s first understand the common positioning solutions. The positioning solution is to control the layout of elements. There are three common solutions:

Normal flow(normal flow)

In normal flow, elements are laid out from top to bottom according to their position in HTML. In this process, in-line elements are arranged horizontally until the line is full and then the line breaks. Block-level elements will be rendered as a complete new line. Unless otherwise specified, all elements default to normal flow positioning. It can also be said that the position of an element in a normal flow is determined by the position of the element in the HTML document.

Float(float)

In the floating layout, the element first appears according to the position of the ordinary flow, and then is offset to the left or right as much as possible according to the direction of the floating. The effect is the same as that of printing and typesetting. Text wrapping in is similar.

Absolute positioning(absolute positioning)

In an absolute positioning layout, the element will be separated from the normal flow as a whole, so the absolutely positioned element will not affect its sibling elements, and the specific position of the element is determined by Absolute positioning is determined by coordinates.

2. BFC concept

Formatting context is a concept in the W3C CSS2.1 specification. It is a rendering area on the page and has a set of rendering rules that determine how its sub-elements will be positioned, as well as their relationship and interaction with other elements.

So what is BFC?

BFC is Block Formatting Contexts (block-level formatting context), which belongs to the ordinary flow of the above positioning scheme.

Elements with BFC characteristics can be regarded as isolated independent containers. The elements inside the container will not affect the layout of the outside elements, and BFC has some characteristics that ordinary containers do not have.

In layman terms, BFC can be understood as a large closed box. No matter how much the elements inside the box go through, they will not affect the outside.

3. Trigger BFC

As long as the element meets any of the following conditions, the BFC feature can be triggered:

  1. body root element

  2. Floating elements: float values ​​other than none

  3. Absolutely positioned elements: position (absolute, fixed)

  4. display For inline-block, table-cells, flex

  5. overflow values ​​other than visible (hidden, auto, scroll)

4. BFC Features and applications

1. The margins under the same BFC will be collapsed

p{
    width: 100px;
    height: 100px;
    background: lightblue;
    margin: 100px;
}


    <p></p>
    <p></p>

From the effect point of view, because the two p elements are in the same Under the BFC container (referring to the body element here), the bottom margin of the first p overlaps with the top margin of the second p, so the distance between the two boxes is only 100px, not 200px.

First of all, this is not a CSS bug. We can understand it as a specification. If you want to avoid overlapping margins, you can place them in different BFC containers.

<p>
    </p><p></p>

<p>
    </p><p></p>
.container {
    overflow: hidden;
}
p {
    width: 100px;
    height: 100px;
    background: lightblue;
    margin: 100px;
}

At this time, the margins of the two boxes become 200px

2. BFC can contain floating elements (clear floats)

We all know that floating elements will break away from the ordinary document flow. Let’s take a look at the following example.

<p>
    </p><p></p>

Because the elements in the container float and break away from the document flow, so The container only has a 2px margin left. If the container's BFC is triggered, the container will wrap the floated element.

<p>
    </p><p></p>

The effect is as shown:

3. BFC can prevent elements from being covered by floating elements

Let’s first look at a text wrapping effect:

<p>我是一个左浮动的元素</p>
<p>我是一个没有设置浮动, 
也没有触发 BFC 元素, width: 200px; height:200px; background: #eee;</p>

At this time, the second element is actually partially covered by the floating element (but the text information will not be covered by the floating element). If you want to avoid the element being covered, The BFC feature of the second element can be touched. Add overflow: hidden to the second element, and it will become:

This method can be used to implement two columns of automatic Adapting to the layout works well. At this time, the width on the left is fixed, and the content on the right adapts to the width (removing the width of the content on the right above).

Related recommendations:

Analysis of usage examples of BFC and IFC in CSS

Detailed explanation of the important BFC in CSS

About BFC and height collapse

The above is the detailed content of Detailed explanation of CSS BFC principle and its application. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)