Detailed explanation of important BFC in CSS
In this article, we mainly share with you a detailed explanation of the important BFC in CSS. There is an important concept BFC in CSS. Understanding BFC can help us understand some of the originally weird (??) places in CSS.
1. Introduction
Before explaining BFC, let’s talk about document flow. The document streams we often talk about are actually divided into three types: Positioned stream , Floating stream , and Ordinary stream . The ordinary flow actually refers to the FC in BFC. FC (Formatting Context), literally translated as formatting context, is a rendering area in the page. It has a set of rendering rules that determine how its sub-elements are laid out and their relationship and role with other elements. Common FCs include BFC, IFC, GFC and FFC.
BFC(Block Formatting Context) Block-level formatting context is a rendering area used to layout block-level boxes, or a rendering rule under certain conditions.
Explanation on MDN: BFC is the visual CSS rendering part of the Web page. It is the area where block-level box layout occurs, and it is also the area where floating elements interact with other elements.
2. BFC triggering method
Root element, that is, HTML tag
The value of float is not none, it is
left
,right
- ##overflow value is not visible, it is
auto
,
scroll,
hidden - display values are
inline-block
,
table-cell,
table-caption,
flex,
inline-flex,
grid,
inline-grid - position value For
absolute
,
fixed
Note: display:table can also generate BFC because Table will be generated by default An anonymous table-cell, it is this anonymous table-ccell that generates BFC.
3. Constraint rulesThe browser’s constraint rules for the BFC area:- The child elements of the generated BFC element will be placed one after another. Their starting point in the vertical direction is the top of a containing block, and the vertical distance between two adjacent child elements depends on the element's margin property. In BFC, the margins of adjacent block-level elements will be collapsed (Mastering margin collapsing).
- In the child elements of the generated BFC element, the left margin of each child element touches the left border of the containing block (for right-to-left formatting, the right margin touches the right border), even if the element is floated (although the content area of the child element will be compressed due to floating), unless the child element also creates a new BFC (such as it is itself a floated element).
- The internal Boxes will be placed one after another in the vertical direction
- The vertical distance of the internal Box is determined by margin. (The complete statement is: the margins of two adjacent boxes belonging to the same BFC will be folded, but different BFCs will not be folded.)
- The left margin of each element is the same as The left edges of the containing blocks touch (from left to right), even for floated elements. (This shows that the sub-element in BFC will not exceed its containing block, while the element with absolute position can exceed the boundary of its containing block)
- The area of BFC will not overlap with the float element Area overlap
- When calculating the height of BFC, floating child elements also participate in the calculation
View DEMO
View DEMO
Here, wrap a p around the outside of any adjacent block-level box, and change the attributes of this p to make the two original boxes belong to two different BFCs to prevent margin overlap.
But here is a question: If it is wrapped with a layer of p, setting any attribute that can trigger BFC can prevent adjacent elements from being Margins merge. Because they belong to different BFCs, margin merging will not occur.
If you do not include a p outside, setting display to inline-block, inline-flex, table-captain, position to absolute, fixed, and float to left or right can prevent margin merging. Here comes the question:
We know that setting position and float will make the element break away from the document flow and create a new BFC, so the two elements are not adjacent elements, so it can prevent the margins of adjacent elements from merging, but inline-block and inline-flex , inline-grid, table-captain why? If anyone knows why, please let me know~
Related recommendations:
What is BFC? A simple understanding of bfc
The definition of floating and the clearing of floating (BFC)
The above is the detailed content of Detailed explanation of important BFC in CSS. 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

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

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.

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

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
