什么是BFC?看这一篇就够了
原文:Floats, absolutely positioned elements, block containers (such as inline-blocks, tablecells, and table-captions) that are not block boxes, and block boxes with ‘overflow’ other than ‘visible’ (except when that value has been propagated to the viewport) establish new block formatting contexts for their contents.
译文:浮动
、绝对定位元素
、不是块盒子的块容器
(如 inline-blocks 、 table-cells 和table-captions ),以及 overflow 属性的值除 visible 以外的块盒
,将为其内容建立新的块格式化上下文
。
块格式化上下文(Block Formatting Context,BFC) 是 Web 页面的可视 CSS 渲染的一部分,是块盒子的布局过程发生的区域,也是浮动元素与其他元素交互的区域。
BFC 是 Block Formatting Context (块级格式上下文),可以理解成元素的一个“特异功能
”。
该 “特异功能
”,在默认的情况下处于关闭状态
;当元素满足了某些条件后,该“特异功能
”被激活
。
所谓激活“特异功能”,专业点说就是:该元素创建了 BFC
(又称:开启了 BFC )。
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>01_BFC_演示1title>
<style>
* {
margin: 0;
padding: 0;
}
/* outer仅设置宽度 */
.outer {
width: 400px;
background-color: #888;
}
/* inner设置宽度和高度,以及margin */
.inner {
width: 100px;
height: 100px;
margin: 20px;
}
.inner1 {
background-color: orange;
}
.inner2 {
background-color: green;
}
.inner3 {
background-color: deepskyblue;
}
style>
head>
<body>
<div class="outer">
<div class="inner inner1">div>
<div class="inner inner2">div>
<div class="inner inner3">div>
div>
<hr style="height: 50px; background-color: red;">
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>01_BFC_演示1title>
<style>
* {
margin: 0;
padding: 0;
}
body {
/* display: flex; */
}
.outer {
width: 400px;
background-color: #888;
float: left;
/* position: absolute; */
/* display: inline-block; */
/* display: table; */
/* overflow: auto; */
/* column-count: 1; */
/* display: flow-root; */
}
.inner {
width: 100px;
height: 100px;
margin: 20px;
}
.inner1 {
background-color: orange;
}
.inner2 {
background-color: green;
}
.inner3 {
background-color: deepskyblue;
}
style>
head>
<body>
<div class="outer">
<div class="inner inner1">div>
<div class="inner inner2">div>
<div class="inner inner3">div>
div>
<hr style="height: 50px; background-color: red;">
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>01_BFC_演示1title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
width: 400px;
background-color: #888;
}
/* 既能解决父子元素相邻外边距问题,
又能解决子元素浮动造成父元素高度的塌陷问题 */
.clearfix::before,
.clearfix::after {
content: "";
display: table;
clear: both;
}
.inner {
width: 100px;
height: 100px;
margin: 20px;
}
.inner1 {
background-color: orange;
}
.inner2 {
background-color: green;
}
.inner3 {
background-color: deepskyblue;
}
style>
head>
<body>
<div class="outer clearfix">
<div class="inner inner1">div>
<div class="inner inner2">div>
<div class="inner inner3">div>
div>
<hr style="height: 50px; background-color: red;">
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>02_BFC_演示2title>
<style>
body {
margin: 0;
}
.outer {
border: 2px solid red;
}
.box1 {
width: 100px;
height: 100px;
background-color: orange;
float: left;
}
.box2 {
width: 120px;
height: 120px;
background-color: green;
}
style>
head>
<body>
<div class="outer">
<div class="box1">div>
<div class="box2">div>
div>
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>02_BFC_演示2title>
<style>
body {
margin: 0;
}
.outer {
border: 2px solid red;
}
.box1 {
width: 100px;
height: 100px;
background-color: orange;
float: left;
}
.box2 {
width: 120px;
height: 120px;
background-color: green;
float: left;
}
style>
head>
<body>
<div class="outer">
<div class="box1">div>
<div class="box2">div>
div>
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>02_BFC_演示2title>
<style>
body {
margin: 0;
}
.outer {
border: 2px solid red;
}
.box1 {
width: 100px;
height: 100px;
background-color: orange;
float: left;
}
.box2 {
width: 120px;
height: 120px;
background-color: green;
position: absolute;
opacity: 0.6;
}
style>
head>
<body>
<div class="outer">
<div class="box1">div>
<div class="box2">div>
div>
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>02_BFC_演示2title>
<style>
body {
margin: 0;
}
.outer {
border: 2px solid red;
}
.box1 {
width: 100px;
height: 100px;
background-color: orange;
float: left;
}
.box2 {
width: 120px;
height: 120px;
background-color: green;
display: inline-block;
}
style>
head>
<body>
<div class="outer">
<div class="box1">div>
<div class="box2">div>
div>
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>02_BFC_演示2title>
<style>
body {
margin: 0;
}
.outer {
border: 2px solid red;
}
.box1 {
width: 100px;
height: 100px;
background-color: orange;
float: left;
}
.box2 {
width: 120px;
height: 120px;
background-color: green;
overflow: auto;
}
style>
head>
<body>
<div class="outer">
<div class="box1">div>
<div class="box2">div>
div>
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>02_BFC_演示2title>
<style>
body {
margin: 0;
}
.outer {
border: 2px solid red;
}
.box1 {
width: 100px;
height: 100px;
background-color: orange;
float: left;
}
.box2 {
height: 120px;
background-color: green;
overflow: auto;
}
style>
head>
<body>
<div class="outer">
<div class="box1">div>
<div class="box2">div>
div>
body>
html>
DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>03_BFC_演示3title>
<style>
body {
margin: 0;
}
.outer {
width: 400px;
background-color: #888;
/* float: left; */
/* position: absolute; */
/* display: inline-block; */
/* display: table; */
/* overflow: auto; */
/* column-count: 1; */
/* display: flow-root; */
}
.inner {
width: 100px;
height: 100px;
float: left;
}
.inner1 {
background-color: orange;
}
.inner2 {
background-color: green;
}
style>
head>
<body>
<div class="outer">
<div class="inner inner1">div>
<div class="inner inner2">div>
div>
body>
html>