Home Web Front-end CSS Tutorial CSS之BFC(Block Formatting Context)

CSS之BFC(Block Formatting Context)

Feb 17, 2017 pm 01:22 PM

I didn’t know what BFC was before. After reading it today, I realized that I had been exposed to it often before, but I just didn’t know the professional name. Just like closures and inheritance, they were often used before, but I just didn’t know it. Today I will talk about BFC of CSS positioning.

BFC is related to clearing floats, so let’s talk about how to clear floats. There are two main ways to clear floats:

①Use the clear attribute to clear floats

② Make the parent container form a BFC

Before talking about BCF, I want to say that there are three main positioning schemes for controlling the layout of elements: Normal Flow and Floats. and Absolute Positioning

***Normal Flow

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

***Floats

In a float layout, elements first appear according to the position of the normal flow, and then are offset to the left or right as much as possible according to the direction of the float.

***Absolute Positioning

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

BFC belongs to the ordinary stream, so it will not have any impact on sibling elements.

&&--Definition of BFC--&&

When laying out box elements, BFC provides an environment in which layout according to certain rules will not affect to the layout in other environments. For example, floating elements will form a BFC. The sub-elements within the floating element are mainly affected by the floating element, and the two floating elements have no influence on each other. In other words, if an element meets the conditions for becoming a BFC, the layout and positioning of the internal elements of the element will not affect each other (unless the internal box creates a new BFC), and it will be an isolated independent container.

&&--What exactly is BFC--&&

When it comes to visual layout, Block Formatting Context provides an environment in which HTML elements are laid out according to certain rules. Elements in one environment do not affect the layout of other environments. For example, floating elements will form a BFC. The sub-elements within the floating element are mainly affected by the floating element, and the two floating elements do not affect each other. This is somewhat similar to the idea that a BFC is an independent administrative unit.

&&--How to form BFC--&&

①The value of float is not none.

②The value of overflow is not visible.

③The value of display is any one of table-cell, table-caption, and inline-block.

④The value of position is not relative or static.

⑤Flex boxes in css3

&&--The role of BFC--&&

①Contains floating elements (clear floats)

BFC will be based on child elements Adaptive height to the situation. This feature is to use overflow:hidden/auto/scroll, float:left/right styles on the parent element to close the float.

②Not covered by floating elements
Floating elements will be ignored The existence of sibling elements covers the sibling elements. Creating a BFC for the sibling elements can prevent this situation from happening

&&--BFC mainly has three characteristics--&&

①BFC Will prevent the margins from folding
The vertical margins of two connected p's will overlap. In actual development, we may sometimes not need this kind of folding. In this case, we can use one of the features of BFC - prevent Margins overlap. Prevents margin collapse of parent and child elements. Only when two block-level elements are adjacent and in the same block-level formatting context, the vertical margins between them will overlap. That is, even if two block-level elements are adjacent, their margins will not collapse when they are not in the same block-level formatting context. At the same time, the BFC element does not have margins collapsed with its child elements.

②BFC can contain floating elements
This is exactly the principle of using the overflow: hidden and overflow: auto methods to close the float above. Use overflow: hidden or overflow: auto to trigger the BFC feature of the parent element of the floating element. , so that you can contain floating elements and close the float.

The original text of W3C is "'Auto' heights for block formatting context roots", that is, BFC will automatically adapt to the height according to the situation of the child elements, even if its child elements include floating elements.

But IE6-7 does not support W3C's BFC, but uses its own hasLayout. In terms of performance, it is very similar to BFC, except that hasLayout itself has many problems, leading to a series of bugs in IE6-7. The conditions for triggering hasLayout are somewhat similar to triggering BFC. Kayo will write another article to introduce the specific situation. Here Kayo recommends setting the IE-specific CSS property zoom: 1 to trigger hasLayout. zoom is used to set or retrieve the zoom ratio of the element. A value of "1" means using the actual size of the element. Using zoom: 1 can trigger hasLayout without It will have other effects on the elements, which is relatively more convenient.

③BFC can prevent elements from being covered by floating elements
As mentioned above, the block sibling elements of the floating element will ignore the position of the floating element and try to occupy the entire row, so that they will be covered by the floating element. , triggering BFC for this sibling element can prevent this from happening.

For more CSS BFC (Block Formatting Context) related articles, please pay attention to the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week's roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

Paperform Paperform Apr 16, 2025 am 11:24 AM

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

It's All In the Head: Managing the Document Head of a React Powered Site With React Helmet It's All In the Head: Managing the Document Head of a React Powered Site With React Helmet Apr 15, 2025 am 11:01 AM

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

Options for Hosting Your Own Non-JavaScript-Based Analytics Options for Hosting Your Own Non-JavaScript-Based Analytics Apr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

See all articles