什么是 BFC?
一、常见定位方案
在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案:
- 普通流 (normal flow)
在普通流中,元素按照其在 HTML 中的先后位置至上而下布局,在这个过程中,行内元素水平排列,直到当行被占满然后换行,块级元素则会被渲染为完整的一个新行,除非另外指定,否则所有元素默认都是普通流定位,也可以说,普通流中元素的位置由该元素在 HTML 文档中的位置决定。
- 浮动 (float)
在浮动布局中,元素首先按照普通流的位置出现,然后根据浮动的方向尽可能的向左边或右边偏移,其效果与印刷排版中的文本环绕相似。
- 绝对定位 (absolute positioning)
在绝对定位布局中,元素会整体脱离普通流,因此绝对定位元素不会对其兄弟元素造成影响,而元素具体的位置由绝对定位的坐标决定。
二、BFC 概念
Formatting context(格式化上下文) 是 W3C CSS2.1 规范中的一个概念。它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用。
那么 BFC 是什么呢?
BFC 即 Block Formatting Contexts (块级格式化上下文),它属于上述定位方案的普通流。
具有 BFC 特性的元素可以看作是隔离了的独立容器,容器里面的元素不会在布局上影响到外面的元素,并且 BFC 具有普通容器所没有的一些特性。
通俗一点来讲,可以把 BFC 理解为一个封闭的大箱子,箱子内部的元素无论如何翻江倒海,都不会影响到外部。
三、触发 BFC
只要元素满足下面任一条件即可触发 BFC 特性:
- 根元素,即 html 元素
- 浮动元素:float 除 none 以外的值
- position 的值为 absolute 或 fixed
- display 为 inline-block | table-cell | table-caption | flex | inline-flex
- overflow 除了 visible 以外的值 (hidden、auto、scroll)
四、BFC 特性及应用
BFC 可以解决下面的问题
- 如何阻止外边距重叠(Collapsing Margins)
- 让一个没有设置高度的容器包含浮动元素
- 阻止文字环绕
阻止外边距重叠
外边距重叠有很多种情况,最简单的就是上下两个盒子,上面的设置了 margin-bottom,下面的设置了 margin-top,这时候总的外边距并不是两者相加,而是取最大的外边距作为总的外边距。(假设外边距的设置为正值)
举个例子🌰下面的代码发生了外边距重叠。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>外边距重叠</title>
<style>
.container {
background-color: pink;
width: 200px;
}
p {
background-color:lightgreen;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="container">
<p>Sibling 1</p>
<p>Sibling 2</p>
</div>
</body>
</html>
效果图:
下面的代码通过使用 overflow: hidden; 新创建一个 BFC,从而实现阻止外边距重叠。
(overflow: hidden;本来的含义是:如果盒子的内容超出盒子,则这部分内容隐藏。)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>外边距重叠</title>
<style>
.container {
background-color: pink;
width: 200px;
}
p {
background-color:lightgreen;
margin: 10px 0;
}
.box {
overflow: hidden;
}
</style>
</head>
<body>
<div class="container">
<p>Sibling 1</p>
<div class="box">
<p>Sibling 2</p>
</div>
</div>
</body>
</html>
修改后是下面这个效果,外边距没有发生重叠。是不是很神奇😀
容器无高度包含浮动元素
先看以下无BFC,容器无高度包含浮动元素会发生什么。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.a{
width: 300px;
background-color: pink;
display: inline-flex;
outline: 1px dashed red;
}
.a div{
width: 100px;
height: 100px;
background-color: black;
margin: 5px;
float: left;
}
</style>
</head>
<body>
<div class="a">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
效果图:
可以看到,由于浮动元素是脱离普通文档流的,所以 .a 盒子发生了高度塌陷,高度变为0。如何阻止这种情况发生呢,请看下面的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.a{
width: 300px;
background-color: pink;
position: absolute;
outline: 1px dashed red;
}
.a div{
width: 100px;
height: 100px;
background-color: black;
margin: 5px;
float: left;
}
</style>
</head>
<body>
<div class="a">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
效果图:
可以看到上述代码解决了容器盒子塌陷的问题。
阻止文字环绕
有如下代码👇
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
background-color: pink;
width: 550px;
height: 500px;
outline: 1px dashed red;
}
.pic {
width: 370px;
height: 322px;
float: left;
background-image: url('img/1.gif');
background-size: contain;
}
</style>
</head>
<body>
<div class="container">
<div class="pic"></div>
<div class="text">
我们常说的文档流其实分为定位流、浮动流和普通流三种。而普通流其实就是指BFC中的FC。FC是formatting
context的首字母缩写,直译过来是格式化上下文,它是页面中的一块渲染区域,有一套渲染规则,决定了其子元素如何布局,以及和其他元素之间的关系和作用。常见的FC有BFC、IFC,还有GFC和FFC。BFC是block
formatting context,也就是块级格式化上下文,是用于布局块级盒子的一块渲染区域
</div>
</div>
</body>
</html>
效果图:
有时候我们不需要文字环绕,就想让文字老老实实待在右边。左图右文,文字不环绕图片。
实现代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.container {
background-color: pink;
width: 550px;
height: 500px;
outline: 1px dashed red;
}
.pic {
width: 370px;
height: 322px;
float: left;
background-image: url('img/1.gif');
background-size: contain;
}
.text {
overflow: hidden;
}
</style>
</head>
<body>
<div class="container">
<div class="pic"></div>
<div class="text">
我们常说的文档流其实分为定位流、浮动流和普通流三种。而普通流其实就是指BFC中的FC。FC是formatting
context的首字母缩写,直译过来是格式化上下文,它是页面中的一块渲染区域,有一套渲染规则,决定了其子元素如何布局,以及和其他元素之间的关系和作用。常见的FC有BFC、IFC,还有GFC和FFC。BFC是block
formatting context,也就是块级格式化上下文,是用于布局块级盒子的一块渲染区域
</div>
</div>
</body>
</html>
效果图:
可以看到文字已经显示在图片右方。
总结
- BFC 就是页面上的一个隔离的独立容器,容器里面的元素不会影响到外面的元素。
- BFC 的区域不会与其它 float 的元素区域重叠。
- 计算 BFC 的高度时,浮动子元素也参与计算。