Heim  >  Artikel  >  Web-Frontend  >  深入理解BFC和外边距合并(Margin Collapse)_html/css_WEB-ITnose

深入理解BFC和外边距合并(Margin Collapse)_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:33:271360Durchsuche

一、什么是BFC?

1.BFC的概念

BFC全称Block Formatting Context ,直译“块级格式化上下文”,也有译作“块级格式化范围”。它是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用。通俗的讲,就是一个div内部,我们用float和margin布局元素。

2.BFC布局规则

  1. 内部的Box会在垂直方向,一个接一个地放置。
  2. Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠
  3. 每个元素的margin box的左边, 与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
  4. BFC的区域不会与float box重叠。
  5. BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
  6. 计算BFC的高度时,浮动元素也参与计算

二、怎么产生BFC?

  1. 根元素
  2. float属性不为none
  3. position为absolute或fixed
  4. display为inline-block, table-cell, table-caption, flex, inline-flex
  5. overflow不为visible

三、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生成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>

效果如下:

四、总结

BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。

  因为BFC内部的元素和外部的元素绝对不会互相影响,因此, 当BFC外部存在浮动时,它不应该影响BFC内部Box的布局,BFC会通过变窄,而不与浮动有重叠。同样的,当BFC内部有浮动时,为了不影响外部元素的布局,BFC计算高度时会包括浮动的高度。避免margin重叠也是这样的一个道理。

 

摘抄自:

参考文章:

http://www.zhangxinxu.com/wordpress/2015/02/css-deep-understand-flow-bfc-column-two-auto-layout/

http://www.w3cplus.com/css/understanding-bfc-and-margin-collapse.html

http://cssor.com/css-bfc-block-formatting-context.html

http://www.haorooms.com/post/css_BFC_bgdiv

http://caibaojian.com/bfc.html

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn