concept
Block Formatting Context (BFC) is part of the visual CSS rendering of a Web page. It is the area where the layout process of Block boxes takes place and where floating elements interact with other elements
What the hell? What to do? I used?
In fact is your page, do not believe you have a look
BFC creation method
- The root element (
<html>
) - Float element (element’s
float
notnone
) - Absolutely locate the element (element’s
position
为absolute
或fixed
) - Inline block elements (of elements
display
为inline-block
) - Table cells (element
display
为table-cell
, HTML table cells default to this value) - Table title (element’s
display
为table-caption
, the HTML table title defaults to this value.) - Anonymous table cell element (element’s
display
为table
,table-row
,table-row-group
,table-header-group
,table-footer-group
(HTML, respectivelytable
,row
,tbody
,thead
,tfoot
Default property) orinline-table
) overflow
Values are not forvisible
The block elementdisplay
A value offlow-root
The elements of thecontain
A value oflayout
,content
orpaint
The elements of the- Elastic element (
display
forflex
或inline-flex
The immediate child of the element) - Grid elements (
display
forgrid
或inline-grid
The immediate child of the element) - Multi-column containers (of elements
column-count
或column-width
Don’t forauto
, includingcolumn-count
1) column-span
为all
The element always creates a new BFC, even if the element is not wrapped in a multi-column container
If we look at the first root HTML element, then our entire page is a BFC;
The BFC contains everything inside the element that created it, but not inside the child element that created the new BFC
BFC layout rules:
- The inner boxes are arranged vertically one after the other (the usual arrangement of block-level elements);
- The floating box area does not overlap with the BFC;
- The left side of each element’s margin touches the left side of the container block border (for left-to-right formatting, otherwise the opposite). This is true even if there is a float;
- A BFC is a separate container on a page, and the child elements inside the container do not affect the outside elements;
- The elements in the same BFC influence each other, and margin overlap may occur.
- When calculating the height of the BFC, consider all elements contained in the BFC, including floating elements;
Rule 1 the inner boxes are arranged vertically, one after the other
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
Copy the code
div {
height: 50px;
}
.box1 {
width: 400px;
background-color: #82A6F5;
}
.box2 {
width: 300px;
background-color: #EAF048;
}
.box3 {
width: 100px;
background-color: #9FF048;
}
.box4 {
width: 200px;
height: 30px;
background-color: #2A5200;
}
Copy the code
Rule 2 The floating box area does not overlap the BFC
The blue box is set to float, and the yellow box will overlap with it
<div class="col1"></div>
<div class="col2"></div>
Copy the code
div {
height: 400px;
}
.col1 {
width: 400px;
background-color: #82A6F5;
float: left;
}
.col2 {
height: 500px;
background-color: #EAF048;
}
Copy the code
Create the landing
Create the BFC using Overflow: Hidden
.col2 {/** create BFC**/ overflow: hidden; height: 500px; background-color:#EAF048;
}
Copy the code
Rule 3 the left side of each element’s margin touches the left side of the container block border (for left-to-right formatting, otherwise the opposite). This is true even if there is a float
<div class="col1"></div>
<div class="col2"></div>
Copy the code
body{
border: 10px solid # 000000;
}
.col1 {
width: 400px;
background-color: #82A6F5;
float: left;
}
.col2 {
height: 500px;
background-color: #EAF048;
}
Copy the code
Rule 5 elements in the same BFC influence each other, and margin overlap may occur (the largest margin shall prevail);
<div class="container">
<div class="box"></div>
<div class="box"></div>
</div>
Copy the code
.col1 {
width: 400px;
height: 400px;
background-color: #82A6F5;
margin-bottom: 50px;
}
.col2 {
height: 500px;
width: 400px;
background-color: #EAF048;
margin-bottom: 60px;
}
Copy the code
When calculating the height of the BFC, consider all elements contained in the BFC, including floating elements
<div class="col">
<div class="col1"></div>
<div class="col2"></div>
</div>
Copy the code
.col{
border: 1px solid #2A5200;
overflow: hidden;
}
.col1 {
width: 400px;
height: 400px;
background-color: #82A6F5;
}
.col2 {
height: 500px;
width: 400px;
background-color: #EAF048;
float: left;
}
Copy the code
Increase the div. Col overfloe: hidden; Create a new BFC height increment
.col{
border: 1px solid #2A5200;
overflow: hidden;
}
Copy the code
Yi? The BFC is a separate container on the page. The child elements in the container do not affect the outside elements.
What does the BFC solve?
Since the elements inside and outside the BFC absolutely do not affect each other, when there is a float outside the BFC, it should not affect the layout of the Box inside the BFC, and the BFC will narrow without overlapping with the float. Similarly, when there is a float inside the BFC, the BFC calculates the height of the float so as not to affect the layout of the external elements. The same goes for avoiding margin collapse.
conclusion
Some of the things mentioned above, in fact, in our ordinary layout, there are already in the use of these rules, but not summed up, if the writing is not good welcome criticism and guidance.
If you think you know it, give it a thumbs up!!
Reference links:
Block Formatting Context
Detailed description of the BFC in the CSS