preface

I have been hearing about BFC in CSS. I happened to encounter the problem of margin folding in IFE exercises, so I happened to understand the mechanism of BFC.

What is BFC

Like all previous posts, I will start from the perspective of What to understand the BFC from the simple to the deep. 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. It determines how its children will be positioned and how they will relate and interact with other elements.

Block Formatting Contexts (BFC) is the rendering and display rule for Block level elements. In layman’s terms, the BFC can be thought of as a large, closed box in which children do not affect the outside elements and vice versa.

The layout rules of BFC are as follows: 1. The internal boxes are placed vertically one by one. The BFC is an isolated container on the page. 3. The upper and lower margins of two adjacent boxes belonging to the same BFC will overlap; 5 The left side of each element touches the left side of the contained box, even if there is a float; 6 BFC areas do not overlap with float;

So how do you trigger a BFC? The BFC feature can be triggered if an element meets any of the following criteria:

  • Body root element;

  • Float element: float is an attribute value that is not None;

  • Absolute position elements: Position (Absolute, fixed)

  • Display: inline-block, table-cells, flex

  • Overflow values other than visible (hidden, auto, Scroll)

BFC features and applications

Next, we will introduce the common features and applications of BFC. In this part, we will use the layout rules and trigger conditions mentioned above to explain the reasons. Therefore, we need to pay attention to them.

1 The vertical margins of block elements in two adjacent normal streams will collapse


     
  1. <head>

  2. .p {  

  3.  width:200px;  

  4.  height:50px;  

  5.  margin:50px 0;  

  6.  background-color:red;  

  7. }  

  8. </head>

  9. <body>

  10.   <div class="p"></div>  

  11.   <div class="p"></div>  

  12. </body>

Copy the code

The renderings are:

According to article 3 of the BFC rules:

The vertical distance of the Box is determined by margin. The upper and lower margins of the + of two adjacent boxes belonging to the same BFC will overlap.

Margin folding occurs in the previous example because they both belong to the root element body, so we need to make them not belong to the same BFC to avoid margin folding:


     
  1. <div class="p"></div>  

  2. <div class="wrap">  

  3.  <div class="p"></div>  

  4. </div>  

Copy the code

     
  1. .wrap {  

  2.  overflow:hidden;  

  3. .p {  

  4.  width:200px;  

  5.  height:50px;  

  6.  margin:50px 0;  

  7.  background-color:red;  

Copy the code

The renderings are:

2 BFC can contain floating elements (clear floating)

Normally, floating elements leave the normal document flow, so in the following code:


     
  1. <div style="border: 1px solid #000;" >

  2.    <div style="width: 50px; height: 50px; background: #eee;

  3. float: left;" >

  4.    </div>

  5. </div>

Copy the code

The outer div will not be able to contain the inner floating div.

But if we fire the BFC of the external container, the external div container can be wrapped around the floating element, according to article 4 of the BFC specification: when calculating the height of the BFC, the floating element is also computed, so just change the code to look like this:


     
  1. <div style="border: 1px solid #000; overflow: hidden">

  2. <div style="width: 100px; height: 100px; background: #eee; float: left;" ></div>

  3. </div>

Copy the code

The following effects can be achieved:

3 BFC prevents elements from being overwritten by floating elements

Let’s start with an example:


     
  1. <div class="aside"></div>  

  2. <div class="main"></div>  

  3. div {  

  4.  width:300px;  

  5. }  

  6. .aside {  

  7.  width: 100px;  

  8.  height: 150px;  

  9.  float: left;  

  10.  background: black;  

  11. }  

  12. .main {  

  13.  height:200px;  

  14.  background-color:red;  

  15. }  

Copy the code

The renderings are:This is because of rule 5 above:The left side of each element touches the left side of the contained box, even if there is a float;

                                                
     
  1. .main {  

  2.  overflow:hidden;  

  3.  height:200px;  

  4.  background-color:red;  

  5. }  

Copy the code

This approach can be used to achieve an adaptive layout of two columns.

Iii. Brief introduction of IFC

IFC layout rules:

  1. The boxes are placed horizontally, one after the other, starting at the top of the containing block.

  2. When placing these boxes, the space they occupy in the horizontal direction of the inside and outside margin + border is taken into account; In the vertical direction, these boxes may be aligned in different ways: horizontal margin, padding, and border are valid, but not vertical. Cannot specify width and height.

  3. The width of the line box is determined by the inclusion block and the float that exists; The height of the line box is determined by the height of the line

Original: https://segmentfault.com/a/1190000012993668