注意:
BFC
首先是块,其次须要具有下面的条件之一才能够(通俗来讲,BFC
就比如一所985或者211的高校,想要成为985或者211的高校,它是有前提条件的,首先他得是一所大学,不能拿一个小学来讲,就比如BFC
得先是块,下一步才能是BFC
;当知足了前提条件,再须要具有其余的条件才能够。)css
在正式说BFC
具体内容以前,先说一下Box
和Formatting Context
是个什么东西:html
Box浏览器
你们应该都不陌生,Box
是CSS布局的对象和基本单位,直观点说就是一个页面是由不少个Box
组成的。不一样类型的Box
,会参与不一样的Formatting Context
(一个决定如何渲染文档的容器),所以Box
内的元素会以不一样的方式来渲染。markdown
常见盒子类型:布局
block-level box
:display
属性为block
, list-item
, table
的元素,会生成block-level box
。而且参与block fomatting context
。inline-level box
:display
属性为 inline
, inline-block
, inline-table
的元素,会生成inline-level box
。而且参与inline fomatting context
。run-in box
:CSS3中新增的,是一个块/行内元素混合,可使某些块级元素成为下一个元素的行内部分,目前不多有浏览器支持该元素。Formatting contextflex
Formatting context
是 W3C CSS2.1 规范中的一个概念。它是页面中的一块渲染区域,而且有一套渲染规则,它决定了其子元素将如何定位,以及和其余元素的关系和相互做用。spa
常见的Formatting context
:3d
Block Formatting Context
(简称BFC
)Inline Formatting Context
(简称IFC
)BFC
和 IFC
, CSS3中还增长了 GFC
和 FFC
。
BFC
(Block formatting context
)直译为“块级格式化上下文”。它是一个独立的渲染区域,只有Block-level box
参与建立BFC
, 它规定了内部的Block-level Box
如何布局,而且与这个独立盒子里的布局不受外部影响,固然也不会影响到外面的元素。code
BFC
);float
的值不为 none
的其余属性值;overflow
的值不为 visible
的其余属性值(有 hidden
,auto
,scroll
);display
的值为 inline-block
/ table-cell
/ table-caption
/ flex
/ inline-flex
;position
的值为 absolute
或 fixed
;box
垂直方向的距离由 margin
决定,属于同一个 BFC
的两个相邻box
的margin
会发生重叠应用场景:能够解释为何margin
上下会重叠,以及解决方法的缘由orm
问题分析:默认状况下,两个相邻的box
的margin
会发生重叠的缘由,是由于box1和box2都属于html
,而html
是BFC
。由于BFC
里面规定了,属于同一个BFC
的两个相邻box
的margin
会发生重叠,因此box1和box2重叠了,也就是外边距重叠。
默认状况下,两个相邻的box的margin会发生重叠:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> body{margin: 0;padding: 0;} .box1{width: 300px;height: 300px;background: red;margin-bottom: 50px;} .box2{width: 400px;height: 400px;background: yellow;margin-top: 100px;} </style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
复制代码
如图所示:
解决方案:
overflow: hidden;
来解决(添加其余声明也能够,只要能够触发BFC
便可)overflow:hidden;
以后,这个父元素boxs
就是BFC
了,当前的结构里面box1属于BFC html
,box2属于BFC boxs
,这个时候box1和box2就不属于同一个BFC
了,那么也就不会发生重叠了。具体代码以下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> body{margin: 0;padding: 0;} .box1{width: 300px;height: 300px;background: red;margin-bottom: 50px;} .box2{width: 400px;height: 400px;background: yellow;margin-top: 100px;} .boxs{overflow: hidden;} </style>
</head>
<body>
<div class="box1"></div>
<div class="boxs">
<div class="box2"></div>
</div>
</body>
</html>
复制代码
如图所示:实现效果(给box2添加父元素并声明触发BFC便可解决)
应用场景:能够解释为何高度塌陷能够用 overflow:hidden
等方法解决
问题分析:为何添加了 overflow:hidden;
就能够解决高度塌陷,是由于添加了以后,就触发ul
为BFC
,而BFC
里面规定了,计算BFC
高度的时候,浮动元素也参与计算。
ul
未触发BFC时,li
浮动时,ul
高度塌陷:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> body,ul{margin: 0;padding: 0;} ul{border: 3px solid red;} ul li{float: left;list-style: none;border: 1px solid yellow;} </style>
</head>
<body>
<ul>
<li>浮动元素</li>
<li>浮动元素</li>
<li>浮动元素</li>
<li>浮动元素</li>
</ul>
</body>
</html>
复制代码
如图所示:
当给ul
添加了overflow:hidden;
以后,就触发了ul
为BFC
,而计算BFC
高度的时候,浮动元素也参与计算:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> body{margin: 0;padding: 0;} ul{border: 3px solid red;overflow: hidden;} ul li{float: left;list-style: none;border: 1px solid yellow;} </style>
</head>
<body>
<ul>
<li>浮动元素</li>
<li>浮动元素</li>
<li>浮动元素</li>
<li>浮动元素</li>
</ul>
</body>
</html>
复制代码
如图所示:
overflow:auto/scroll
或者display:flex/inline-flex/table
等,均可以解决高度塌陷,也是由于触发了ul
为BFC
float box
发生重叠应用场景:自适应两栏布局或者三栏布局
问题分析:
float
、overflow
、display
的时候就不重叠了,缘由是由于给了这些声明以后,就触发了下面的元素为BFC
,而BFC
里面规定了,BFC
区域不会与浮动盒子发生重叠。BFC
区域不会与float box
发生重叠,又要右边的容器自适应:
float
不能够;overflow
:hidden
、auto
、scroll
;能够;display
:flex
、inline-flex
;能够。当上面元素设置浮动,下面元素没有浮动,那么就会发生重叠:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> .left {width: 300px;height: 300px;background: red;float: left;} .right {height: 400px;background: yellow;} </style>
</head>
<body>
<div class="left">left</div>
<div class="right">right</div>
</body>
</html>
复制代码
如图所示:
当下面元素添加声明,触发BFC
,就不会与浮动盒子发生重叠:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> .left {width: 300px;height: 300px;background: red;float: left;} .right {height: 400px;background: yellow;overflow: hidden;} </style>
</head>
<body>
<div class="left">left</div>
<div class="right">right</div>
</body>
</html>
复制代码
如图所示:
应用案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> body,ul,ol{ margin: 0; padding: 0;} html,body{height: 100%;} .left{width: 300px;height:500px;background: #0000FF;float: left;} .right{height: 600px;background: yellow;overflow: hidden;} </style>
</head>
<body>
<div class="left">left固定</div>
<div class="right">right自适应</div>
</body>
</html>
复制代码
实现效果如图所示:
能够实现右侧自适应的方法属性有: overflow: hidden、auto、scroll; display: flex、inline-flex;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css"> body{ margin: 0; padding: 0;} html,body{height: 100%;} .left{ width: 100px;height: 200px;background: red;float: left; } .center{ height: 400px;background: #00FFFF;overflow: hidden;margin-right: 200px; } .right{ width: 200px;height: 300px;background: #008000;float: left;position: absolute;top: 0;right: 0; } </style>
</head>
<body>
<div class="left">left</div>
<div class="center">center</div>
<div class="right">right</div>
</body>
</html>
复制代码
实现效果如图所示:
不一样分辨率下左右两侧内容固定不变,中间内容自适应(下面由于图片放大了因此左右两侧显得变大了,实际上并无改变大小):
到这里 BFC 就结束了,总结一下 BFC 就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。