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!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


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

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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