Detailed explanation of BFC mode
This time I will bring you a detailed explanation of the BFC mode. What are the precautions when using the BFC mode. The following is a practical case, let’s take a look.
Detailed explanation of BFC【block formatting context】
You may not think that BFC is a common thing, but you You will definitely use it frequently, maybe you didn’t think of BFC when you used it. So what exactly is it? Let’s take a look.
The official BFC explanation is this, floating elements and absolutely positioned elements, block-level containers of non-block-level boxes (such as inline-blocks, table-cells
, and table-captions
), and block-level boxes with an overflow value other than "visiable" will create new block-level formatting contexts for their contents.
In a block-level formatting context, boxes are arranged vertically one after another starting from the top of the containing block. The vertical gap between two boxes is determined by their margin value. The vertical margins of two adjacent block-level boxes overlap.
In a block-level formatting context, the left outer edge of each box (margin-left) will touch the left edge (border-left) of the container (for right to For left formatting, it touches the right edge), even if there is a float, unless the box creates a new block-level formatting context.
The above mentioned repeatedly "block formatting context
", obviously BFC is their abbreviation, the official also has "inline formatting context
", that means"formatting context
"We need to take a closer look at what it means, so let's break it down and take a look:
formatting: n, the meaning of formatting, v, To format (the present participle of format); to specify the format (or shape, size, proportion, etc.) of...;
context: context; context; background; environment;
Put together, it means "formatting context". It is a rendering area in the page and has a set of rendering rules, which determines how its sub-elements will be positioned, as well as their relationship and interaction with other elements.
The most common Formatting contexts are Block fomatting context
(BFC for short) and Inline formatting context
(IFC for short). There are only BFC and IFC in CSS2.1, and GFC and FFC are also added in CSS3.
Box is also an important concept. Box is the object and basic unit of CSS layout. From a visual point of view, it is a page. It is composed of many Boxes. The type of element and display attribute determine the type of this Box. Different types of Box will participate in different Formatting context
(a container that determines how to render the document), so the elements within the Box will be rendered in different ways.
For example, if the display attribute value is block, list-item, or table, a block-level box will be generated. And participate in block fomatting context. inline-level box: Elements whose display attributes are inline, inline-block, and inline-table will generate an inline-level box. and participate in inline formatting context
.
So let’s understand it in a layman’s terms, BFC means “block-level formatting context”. The element that creates the BFC is an independent box, but only the Block-level box can participate in creating the BFC. It stipulates How the internal Block-level Box is laid out, and the layout in this independent box is not affected by the outside. Of course, it will not affect the outside elements.
So let’s take a look at some features of BFC boxes:
1. The internal Boxes will be placed one after another in the vertical direction, starting from the top.
2. The vertical distance of the Box is determined by margin. The margins of two adjacent boxes belonging to the same BFC will overlap.
3. The left side of the margin box of each element is in contact with the left side of the containing block border box (for left-to-right formatting, otherwise the opposite is true). This is true even if there is float.
4. The BFC area will not overlap with the float box.
5. BFC is an isolated independent container on the page. The sub-elements inside the container will not affect the outside elements, and vice versa.
6. When calculating the height of BFC, floating elements also participate in the calculation.
In which scenarios BFC can be used
1. Solve the problem of margin overlay
When the boxes are arranged up and down, the upper box margin-bottom:50px; the lower box margin-top:50; then something magical happens , if the two boxes are calculated according to superposition, the distance should be 100px, but we found that the two margin values are actually superimposed, leaving only 50px. Then at this time we can trigger the BFC mode and add a parent to one of the boxes. Level element;
2. Used for layout
The left margin of the element will touch the outer border of the containing block container, even if there is a float, then we can Use this method to create a two-column layout, with the first box floating and the second box margin-left assigned;
3. Used toclear the float and calculate the BFC height
We found that due to the floating relationship between the two child elements inside, the two boxes have separated from the containing block of the parent element, and the height of the parent element has collapsed. We need to make the parent element contain two boxes. Child elements, when calculating the height, two floating child elements will participate, so we need to close the float and trigger the BFC of the parent element, such as overflow:hidden;
I believe you have mastered the method after reading the case in this article , for more exciting content, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use attribute value inheritance in css
CSS3 attributes transition, animation, transform
The above is the detailed content of Detailed explanation of BFC mode. For more information, please follow other related articles on the PHP Chinese website!

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











What does WeChat Do Not Disturb mode mean? Nowadays, with the popularity of smartphones and the rapid development of mobile Internet, social media platforms have become an indispensable part of people's daily lives. WeChat is one of the most popular social media platforms in China, and almost everyone has a WeChat account. We can communicate with friends, family, and colleagues in real time through WeChat, share moments in our lives, and understand each other’s current situation. However, in this era, we are also inevitably faced with the problems of information overload and privacy leakage, especially for those who need to focus or

Detailed explanation of the mode function in C++ In statistics, the mode refers to the value that appears most frequently in a set of data. In C++ language, we can find the mode in any set of data by writing a mode function. The mode function can be implemented in many different ways, two of the commonly used methods will be introduced in detail below. The first method is to use a hash table to count the number of occurrences of each number. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for a given data set, we run

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

Detailed explanation of the remainder function in C++ In C++, the remainder operator (%) is used to calculate the remainder of the division of two numbers. It is a binary operator whose operands can be any integer type (including char, short, int, long, etc.) or a floating-point number type (such as float, double). The remainder operator returns a result with the same sign as the dividend. For example, for the remainder operation of integers, we can use the following code to implement: inta=10;intb=3;

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus
