BFC Introduction
Box Box model
Formatting Context
BFC definition
BFC Layout Rules:
How to generate BFC
BFC applications
1 Adaptive two-column layout
2 Clear Float
3 Clear Float
BFC Introduction Box Box model
box is the object and the basic unit of the CSS layout, and the straight point of view is that a page is made up of a lot of Box. The type of the element and the display property determine the type of the Box. Different types of boxes are involved in different formatting Context (a container that determines how the document is rendered), so the elements inside the box are rendered in different ways. Let's see what the boxes are:
- block-level Box:d The Isplay property is the block, list-item, table element that generates Block-level box. and participate in block fomatting context;
- inline-level Box:d The Isplay property is an inline, inline-block, inline-table element that generates Inline-level box. and participate in the inline formatting context;
- run-in box: CSS3, according to the following sibling elements to decide that they are block or inline here do not say first.
Formatting Context
Formatting context is a concept in the CSS2.1 specification. It is a rendered area of the page and has a set of rendering rules that determine how its child elements will be positioned, and the relationships and interactions with other elements. The most common formatting context is the Block fomatting context (abbreviated BFC) and the Inline formatting context (IFC).
BFC definition
BFC (block formatting context) literal translation is "chunk-level formatting context". It is a separate rendering area that only block-level box participates in, which specifies how the internal block-level box is laid out and has no effect on the outside of the area . BFC is a separate, isolated container on the page, and the child elements inside the container do not affect the outer elements. The reverse is true.
BFC Layout Rules:
- The inner box is placed vertically, one by one.
- The left side of the margin box for each element in the interior touches the left side of the containing block border box (or vice versa for formatting from left to right). This is true even if there is a float.
- The BFC box does not overlap with the float box.
- When the BFC box calculates the height, the internal floating elements are also involved in the calculation
- Vertical margin does not overlap between adjacent two BFC box. Vertical margin of two adjacent boxes within the same BFC overlap
How to generate BFC
- root element
- The Float property is not none
- Position for absolute or fixed
- Display for Inline-block, Table-cell, Table-caption, Flex, Inline-flex
- The overflow is not visible
BFC Application 1 Adaptive two-column layout
<style>
body {
width: 300px;
position: relative;
}
.aside {
width: 100px;
height: 150px;
float: left;
background: #f66;
}
.main {
height: 200px;
background: #fcc;
}
</style>
<body>
<div class="aside"></div>
<div class="main"></div>
</body>
According to the BFC layout rule 2nd:
The left side of each element's margin box is in contact with the left side of the containing block border box (or vice versa for formatting from left to right). This is true even if there is a float.
Therefore, although there is a floating element aslide, the left side of main will still touch the left side of the containing block.
According to the BFC layout rule 3rd:
The BFC area does not overlap with the float box.
We can implement an adaptive two-column layout by triggering main to generate BFC.
.main {
overflow: hidden;
}
This new BFC does not overlap with floating aside when the main generator BFC is triggered. As a result, the width of the containing block, and the width of the aside, automatically narrows. The effect is as follows:
2 Clear Float
<style>
.par {
border: 5px solid #fcc;
width: 300px;
}
.child {
border: 5px solid #f66;
width:100px;
height: 100px;
float: left;
}
</style>
<body>
<div class="par">
<div class="child"></div>
<div class="child"></div>
</div>
</body>
According to the BFC layout rule 4th:
When calculating the height of the BFC, floating elements are also involved in the calculation
In order to clear the internal float, we can trigger par to generate BFC, then par in the calculation of height, the floating element inside par will also participate in the calculation.
Code:
.par {
overflow: hidden;
}
The effect is as follows:
3 Clear Float
<style>
p {
color: #f55;
background: #fcc;
width: 200px;
line-height: 100px;
text-align:center;
margin: 100px;
}
</style>
<body>
<p>Haha</p>
<p>Hehe</p>
</body>
The distance between two P is 100px, and margin overlap is sent.
According to the BFC layout rule 5th:
Vertical margin does not overlap between adjacent two BFC box. Vertical margin of two adjacent boxes within the same BFC overlap
We can wrap a layer of containers outside p and trigger the container to generate a BFC. Then two P will not belong to the same BFC, there will be no margin overlap.
Code:
<style>
.wrap {
overflow: hidden;
}
p {
color: #f55;
background: #fcc;
width: 200px;
line-height: 100px;
text-align:center;
margin: 100px;
}
</style>
<body>
<p>Haha</p>
<div class="wrap">
<p>Hehe</p>
</div>
</body>
The effect is as follows
From for notes (Wiz)
What is BFC