再解释以前须要了解下面的一些概念有一个认识:html
Box(框)chrome
Box它是组成页面的基本单元,一个界面能够由多个Box元素组成。元素的类型(块级与行级)和display属性决定了这个Box的类型,不一样类型的 Box, 会参与不一样的 Formatting Context(一个决定如何渲染文档的容器),所以Box内的元素会以不一样的方式渲染。
Box目前分为一下几种:浏览器
block-levelide
box:display 属性为 block, list-item, table 的元素,会生成 block-level box。而且参与 block Formatting context;
run-in
box:display 属性为 run-in的元素,会生成run-in-level box.
run-in框的行为以下:
一、若是run-in框包含一个块框,那么run-in框变为块框。
二、若是run-in框的后继兄弟元素为块框(非浮动,非绝对定位),那么run-in框变为该块框的第一个行内框。run-in不能插入自己为run-in的块中,也不能插入块中已有run-in的块中。
三、不然,run-in框变为块框。布局
浏览器支持:IE8+,chrome(unsupport)flex
style
.run-in-box {display: run-in;}spa
html:code
<h2>case 1</h2> <div class="run-in-box"> <p>some text...</p> </div> <div>some other text....</div> <h2>case 2</h2> <h3 class="run-in-box">A run-in heading.</h3> <p>And a paragraph of text that follows it.</p> <h2>case 3</h2> <h3 class="run-in-box">A run-in heading.</h3> <span>And a span of text that follows it.</span>
Formatting contextorm
Formatting context 是 W3C CSS2.1 规范中的一个概念。它是页面中的一块渲染区域,而且有一套渲染规则,它决定了其子元素将如何定位,以及和其余元素的关系和相互做用。最多见的 Formatting context 有 Block fomatting context (简称BFC)和 Inline formatting context (简称IFC)。htm
BFC 定义
BFC(Block formatting context)直译为"块级格式化上下文"。它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Block-level Box如何布局,而且与这个区域外部绝不相干。
BFC布局规则:
2、哪些元素会生成BFC?
3、BFC的做用及原理
1. 自适应两栏布局
代码:
<style> body { width: 300px; position: relative; } .aside { width: 100px; height: 150px; float: left; background: #f66; } .main { height: 200px; background: #fcc; } </style> <body> <div class="aside"></div> <div class="main"></div> </body>
页面
根据BFC布局规则第3条:
每一个元素的margin box的左边, 与包含块border box的左边相接触(对于从左往右的格式化,不然相反)。即便存在浮动也是如此。
所以,虽然存在浮动的元素aslide,但main的左边依然会与包含块的左边相接触。
根据BFC布局规则第四条:
BFC的区域不会与float box重叠。
咱们能够经过经过触发main生成BFC, 来实现自适应两栏布局。
.main { overflow: hidden; }
当触发main生成BFC后,这个新的BFC不会与浮动的aside重叠。所以会根据包含块的宽度,和aside的宽度,自动变窄。效果以下:
2. 清除内部浮动
<style> .par { border: 5px solid #fcc; width: 300px; } .child { border: 5px solid #f66; width:100px; height: 100px; float: left; } </style> <body> <div class="par"> <div class="child"></div> <div class="child"></div> </div> </body>
页面
根据BFC布局规则第六条:
计算BFC的高度时,浮动元素也参与计算
为达到清除内部浮动,咱们能够触发par生成BFC,那么par在计算高度时,par内部的浮动元素child也会参与计算。
代码:
.par { overflow: hidden; }
效果图:
3. 防止垂直 margin 重叠
<style> p { color: #f55; background: #fcc; width: 200px; line-height: 100px; text-align:center; margin: 100px; } </style> <body> <p>Haha</p> <p>Hehe</p> </body>
页面
两个p之间的距离为100px,发送了margin重叠。
根据BFC布局规则第二条:
Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠
咱们能够在p外面包裹一层容器,并触发该容器生成一个BFC。那么两个P便不属于同一个BFC,就不会发生margin重叠了。
代码
<style> .wrap { overflow: hidden; } p { color: #f55; background: #fcc; width: 200px; line-height: 100px; text-align:center; margin: 100px; } </style> <body> <p>Haha</p> <div class="wrap"> <p>Hehe</p> </div> </body>
效果以下:
另外一种状况
<style> .box6{ background: orange; height: 200px; width: 600px; } .box6-c{ height: 100px; width: 100px; background: black; margin-top: 80px; } </style> <body> <div class="box6"> <div class="box6-c"></div> </div> </body>
效果图以下:
内部的子元素的maring距父级的外边距为0;
根据BFC规则第五条:
BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
咱们能够将父级盒子触发BFC,那么父级则属于独立容器,不受外面元素的影响;
代码:
.box6 { overflow: hidden; }
效果图以下:
其实以上的几个例子都体现了BFC布局规则第五条:
BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
由于BFC内部的元素和外部的元素绝对不会互相影响,所以, 当BFC外部存在浮动时,它不该该影响BFC内部Box的布局,BFC会经过变窄,而不与浮动有重叠(两端布局)。一样的,当BFC内部有浮动时,为了避免影响外部元素的布局,BFC计算高度时会包括浮动的高度。避免margin重叠也是这样的一个道理。
参考:BFC 神奇背后的原理