BFC,IFC学习笔记

2017-08-19  本文已影响0人  wonderwander

概念

Formatting context

Formatting context 是 W3C CSS2.1 规范中的一个概念。它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用。最常见的 Formatting context 有 Block fomatting context (简称BFC)和 Inline formatting context (简称IFC),CSS3还新增了GFC 和 FFC。

BFC(Block formatting context)

直译为"块级格式化上下文"。它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干

IFC(inline formatting contenxt)
类似地,只有inline-level box参与,规定了内部的inline-level box如何布局

BFC

名词解释

image.png

注意,Block container box不一定是Block-level boxes,但如果的确都是则称之为block boxes

创建BFC

满足以下条件之一即可创建BFC

布局规则

应用

清除浮动造成的文字环绕

<div name="mydemocontainer" style="overflow:hidden;">
    <div style="float:left">
        ![man jumping](https://img.haomeiwen.com/i1753979/69f5b77ce3709111.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
      </div>
      <p class="oh">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae hic ut ab perferendis sit quod architecto, dolor debitis quam rem provident aspernatur tempora expedita perspiciatis suscipit ipsam voluptas tenetur. Nostrum? Quae hic ut ab perferendis sit quod architecto. Quae hic ut ab perferendis sit quod architecto.
    </p>
</div>

效果:

<iframe width="100%" height="300" src="http://jsrun.net/KBYKp/embedded/all/light/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

给p标签添加overflow:auto使得成为新的BFC,由于BFC的规则BFC的区域不会与float box重叠

清除浮动

<div class="par" style="border: 5px solid #fcc;width: 300px;">
    <div class="child" style=" border: 5px solid #f66;width:100px;height: 100px;float: left;"></div>
    <div class="child"></div>
</div>

效果:

<iframe width="100%" height="300" src="http://jsrun.net/pBYKp/embedded/all/light/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

给容器添加overflow:auto;后,使之成为BFC,根据规则计算BFC的高度时,浮动元素也参与计算,容器高度被float块撑起,浮动的影响被清除

清除margin重叠

<div name="mydemocontainer" style="overflow:hidden;">
    <div style="border: 5px solid #f66;width:100px;height: 100px;margin:10px;"></div>
    <div style="border: 5px solid #f66;width:100px;height: 100px;margin:10px;"></div>
</div>

效果

<iframe width="100%" height="300" src="http://jsrun.net/kBYKp/embedded/all/light/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>


两个div在同一个BFC中,第一个div的下边界margin与第二个上边界margin重合,间距10px。给其中一个div再包裹一个div<div style="overflow:auto;">...</div>。根据规则属于同一个BFC的两个相邻Box的margin会发生重叠,现在有margin的div的其中一个被包裹,那么两个div不属于同一个BFC下则margin不会重叠

资料

IFC

名词解释

image.png image.png

布局规则

IFC是布局特性,并不是实际存在于Dom中的东西(即别想看见他)
IFC由不包含块级盒的块容器盒建立(块容器盒中只有内联级盒子)

内联元素(inline-elemet)很容易理解(display:inline),它能生成inline-level boxes,但只有在IFC内的才称之为inline box

一句话很难说清楚IFC,他是布局特性,那么就应该了解该布局特性的规则

解释

<iframe width="100%" height="300" src="http://jsrun.net/RjYKp/embedded/all/light/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>

其他

目前还不知道如何演示规则line box的宽度由该盒子包含的块填充同时会受浮动(float:left|right)块影响。
但我从这里偷来的图能很好的描述这个规则

image.png

如图,float元素不再属于line box,他处于包裹line box的容器和line box之间,但是它的存在挤压line box,这就是float元素能做到文字环绕的原因

资料

后记

简书发的第一篇,本文摘抄至各个文档博客,链接都已给出。
使用JSRun演示,但是简书不能显示,差评,再见。

上一篇 下一篇

猜你喜欢

热点阅读