坑爹的BFC;块格式上下文

时间:2022-06-24 21:32:02

  Formatting context(FC)

  Formatting context 是 W3C CSS2.1 规范中的一个概念。它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用。最常见的 Formatting context 有 Block fomatting context (简称BFC)和 Inline formatting context (简称IFC), IE浏览器中没有BFC的概念,但是有个差不多的东东叫做hasLayout 。

  BFC(Block Formatting Context 块格式化上下文):是W3C CSS 2.1 规范中的一个概念,在CSS3中被修改为flow root。格式化则表明了在这个环境中,元素处于此环境中应当被初始化,即元素在此环境中应当如何布局等。元素如果创建了BFC,那么BFC决定了如何内容进行定位,以及它与其他元素的关系和相互作用。

  BFC的用处是:如清浮动,防止 margin 重叠等, 自适应的布局;

  BFC的区域的元素是一块独立渲染区域, 不会受到其他块元素的影响;

  如何触发BFC呢;

根元素html(quirk的body)默认就是BFC元素;
浮动元素(float:left 或者 float:right);
绝对得的元素(absolute; fixed);
display取值为inline-block,table-cell, table-caption,flex, inline-flex之一的元素;
overflow不是visible的元素;

  BFC有哪些布局规则呢:

1:内部的box是一个一个垂直放置(按照标准文档流放置);
2:同一个BFC中的两个Box的margin会发生折叠; IFC的垂直方向会发生折叠;
3:每个元素的margin box的左边, 与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
4:BFC的区域不会与float box重叠(利用这点可以做自动适应的布局,下面有例子);
5:BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
6:计算BFC的高度时,浮动元素也参与计算(利用这点可以用来清除浮动);

  利用BFC解决垂直方向margin叠加的问题;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BFC</title>
</head>
<body>
<style>
body{
margin:0px;
}
.bfc{
overflow:hidden;
}
.box{
width:400px;
height:400px;
margin:100px;
background:#666;
}
.child{
width:100px;
height:100px;
margin:100px;
background:#f00;
}
.fl{
float:left;
}
</style>
<div class="box">
<div class="child"></div>
</div>
<div class="box bfc">
<div class="child fl"></div>
</div>
</body>
</html>

  BFC就是一写正常流,浮动流布局的一写规则,比如(BFC会自动计算元素内部的高度适应到父级,PS{ 可以用来清浮动} ):

<!doctype html>
<head>
<meta charset="utf-8" />
<title>Clear float</title>
<style>
.container{
margin: 30px auto;
width:600px;
height: 300px;
}
.wrapper{
border:solid 3px #a33;
}
.main{
width: 100px;
height: 100px;
background-color: #060;
margin: 10px;
float: left;
}
</style>
</head>
<body>
<div class="container">
<div class="wrapper">
<div class="main"></div>
<div class="main"></div>
<div class="main"></div>
</div>
</div> <div class="container">
<div class="wrapper" style="float:left;">
<div class="main">1</div>
<div class="main">2</div>
<div class="main">3</div>
</div>
</div>
</body>
</html>

  2:BFC的另一个规则,(BFC不会与浮动的元素发生叠加):

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
body {
position: relative;
} .aside {
width: 100px;
height: 150px;
float: left;
background: #f66;
} .main {
height: 200px;
background: #fcc;
}
.hd{
overflow:hidden;
}
</style>
<div class="aside"></div>
<div class="main"></div>
<br>
<div class="aside"></div>
<div class="main hd"></div>
</body> </html>

  

  IFC布局规则:

  1. 框会从包含块的顶部开始,一个接一个地水平摆放。
  2. 摆放这些框的时候,它们在水平方向上的外边距、边框、内边距所占用的空间都会被考虑在内。在垂直方向上,这些框可能会以不同形式来对齐:它们可能会把底部或顶部对齐,也可能把其内部的文本基线对齐。能把在一行上的框都完全包含进去的一个矩形区域,被称为该行的行框。水平的margin、padding、border有效,垂直无效。不能指定宽高。
  3. 行框的宽度是由包含块和存在的浮动来决定。行框的高度由行高计算这一章所描述的规则来决定。

  Containing  block的概念:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BFC</title>
</head>
<body>
<p style="border:1px solid red; width:200px; padding:20px;">
T
<span style="background-color:#C0C0C0; position:relative;">
这段文字从左向右排列,红 XX 和 蓝 XX 和黄 XX 都是绝对定位元素,它的包含块是相对定位的SPAN。
可以通过它们绝对定位的位置来判断它们包含块的边缘。
<em style="position:absolute; color:red; top:0; left:0;">XX</em>
<em style="position:absolute; color:yellow; top:20px; left:0;">XX</em>
<em style="position:absolute; color:blue; bottom:0; right:0;">XX</em>
</span>
</p> <p style="border:1px solid red; width:200px; padding:20px;">
TEXT TEXT TEXT
<span style="background-color:#C0C0C0; position:relative;">
这段文字从左向右排列,红 XX 和 蓝 XX 和黄 XX 都是绝对定位元素,它的包含块是相对定位的SPAN。
可以通过它们绝对定位的位置来判断它们包含块的边缘。
<em style="position:absolute; color:red; top:0; left:0;">XX</em>
<em style="position:absolute; color:yellow; top:20px; left:0;">XX</em>
<em style="position:absolute; color:blue; bottom:0; right:0;">XX</em>
</span>
</p> <p style="border:1px solid red; width:200px; padding:20px; direction:rtl;">
T
<span style="background-color:#C0C0C0; position:relative;">
这段文字从右向左排列,红 XX 和 蓝 XX 和黄 XX 都是绝对定位元素,它的包含块是相对定位的SPAN。
可以通过它们绝对定位的位置来判断它们……
<em style="position:absolute; color:red; top:0; left:0;">XX</em>
<em style="position:absolute; color:yellow; top:20px; left:0;">XX</em>
<em style="position:absolute; color:blue; bottom:0; right:0;">XX</em>
</span>
</p> <p style="border:1px solid red; width:200px; padding:20px; direction:rtl;">
TEXT TEXT TEXT<span style="background-color:#C0C0C0; position:relative;">
这段文字从右向左排列,红 XX 和 蓝 XX 和黄 XX 都是绝对定位元素,它的包含块是相对定位的SPAN。
可以通过它们绝对定位的位置来判断它们……
<em style="position:absolute; color:red; top:0; left:0;">XX</em>
<em style="position:absolute; color:yellow; top:20px; left:0;">XX</em>
<em style="position:absolute; color:blue; bottom:0; right:0;">XX</em> </span>
</p> <div id="container" style="padding:50px; background-color:#c0c0c0; position:relative; width:200px; height:200px;">
<div id="div1" style="width:100%; height:100%; border:2px solid blue;">
<div id="content" style="border:1px solid red; position:absolute; left:0; top:0;">absolute element</div>
</div>
</div>
</body>
</html>

  相关资料:

   CSS包含块containing block详解

   css2-bfc模型和ifc模型

   由position属性引申的关于css的进阶讨论(包含块、BFC、margin collapse)

  前端精选文摘:BFC 神奇背后的原理

   BFC 、IFC