当前位置:网站首页>What is BFC? What problems can BFC solve
What is BFC? What problems can BFC solve
2022-06-23 09:26:00 【Companion 993】
One 、 Normative interpretation : Block formatting context (Block Formatting Context,BFC) yes Web Visualization of the page CSS Part of the rendering , It's the area where block level boxes are generated during layout , It is also a restricted area for interaction between floating elements and other elements
Two 、 Easy to understand :BFC It's a Independent layout environment , It can be understood as a container , Place items in this container according to certain rules , And it doesn't affect things in other environments
3、 ... and 、BFC It's a css Render a special part , need Certain conditions can Trigger
- The root element or other element that contains it
- Floating elements ( Elemental
floatNonone) - Absolute positioning elements ( The element has
positionbyabsoluteorfixed) - Inline block ( The element has
display: inline-block) - Table cells ( The element has
display: table-cell,HTML Table cell default properties ) - Table title ( The element has
display: table-caption, HTML Table title default properties ) - have
overflowAnd it's not worth itvisibleThe block element of
Four 、BFC Problems that can be solved
- Vertical outer margin overlap problem
- Remove the float
- Self applicable two column layout
1、 Solve the problem of collapse of outer margin
1)、 The collapse of outer margins of parent-child elements
We have subelements within an element , Set up a margin-top:20px; The resulting parent element has a concrete top 20px Distance of , Obviously not in line with our expectations
* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
height: 400px;
background-color: skyblue;
}
.box1 {
width: 100px;
height: 100px;
background-color: red;
margin-top: 30px;
}
<div class="box">
<div class="box1"></div>
</div>The effect of setting our page is like this

box A certain distance from the top
If we want to solve the problem of parent-child element margin collapse , We give box add overflow:hidden; attribute

2、 Solve the collapse of the outer margin between the second sibling elements
Two sibling element settings margin:20px; As we think, the two elements should be separated 40px That's right, isn't it , But actually the two elements are far apart 20px
.box1 {
width: 100px;
height: 100px;
background-color: red;
margin: 100px;
}
.box2 {
width: 100px;
height: 100px;
background-color: blue;
margin: 100px;
}
<div class="box1"></div>
<div class="box2"></div>
At this time, there will be the problem of collapse of the outer margin :
At this time, how to calculate the outer margin ?
- 1. Both are positive numbers , Take the larger value
- 2. Both are negative numbers , Take the higher absolute value
- 3. One is one minus one. , Take two worth and
Let's revise
.box1 {
width: 100px;
height: 100px;
background-color: red;
margin: 100px;
}
.box2 {
width: 100px;
height: 100px;
background-color: blue;
margin: 100px;
}
.box{
overflow:hidden;
}
<div class="box1"></div>
<div class="box">
<div class="box2"></div>
</div>So the margin is 200px 了

3、 Self applicable two column layout
.box {
width: 300px;
height: 400px;
background-color: skyblue;
}
.box1 {
float: left;
width: 100px;
height: 100px;
background-color: red;
}
.box2 {
height: 200px;
background-color: blue;
}
<div class="box">
<div class="box1"></div>
<div class="box2"></div>
</div>
We can see , The blue part is covered , Nor does it automatically adapt to the rest of the layout
If we want to achieve , How to write it?
to box2 Add one more overflow: hidden; Let's see the effect

Automatically adapt to layout
This actually uses bfc characteristic bfc Area will not be associated with float-box Area overlap
BFC Of course, there are other uses Such as the use of overflow:hidden Remove floats, etc
边栏推荐
- 使用base64,展示图片
- Redis learning notes master-slave copy
- Redis learning notes - client communication protocol resp
- Chain representation and implementation of linklist ---- linear structure
- map的下标操作符
- [GYCTF2020]Blacklist
- Redis学习笔记—发布订阅
- 玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
- A 32KB cache with direct mapping Memory exercises after class
- 'coach, I want to play basketball!'—— AI Learning Series booklet for system students
猜你喜欢
随机推荐
[SUCTF 2019]CheckIn
Redis learning notes - detailed explanation of redis benchmark
Custom tags - JSP tag enhancements
4sum of leetcode topic analysis
多线程初学
UCOSII (learning notes)
Neither rain nor sunshine
Correspondence between three-tier architecture and SSM
Redis学习笔记—客户端通讯协议RESP
[CISCN2019 华北赛区 Day2 Web1]Hack World
全局快门和卷帘快门的区别
A 32KB cache with direct mapping Memory exercises after class
cooding代码库的使用笔记
C#之Lambda不得不说的用法
Redis学习笔记—数据类型:集合(set)
UEFI source code learning 3.7 - norflashdxe
Redis learning notes - AOF of persistence mechanism
ARM中常见的英文解释
GPIO novice
MySQL fault case | mysqldump: couldn't execute 'select column_ NAME






![[ciscn2019 North China Day2 web1]hack world](/img/bf/51a24fd2f9f0e13dcd821b327b5a00.png)
