美文网首页
BFC,IFC学习笔记

BFC,IFC学习笔记

作者: wonderwander | 来源:发表于2017-08-19 17:04 被阅读0次

    概念

    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-level elements: display 为 block, list-item 或 table 元素
    • Block-level boxes:参与BFC的Block-level elements
    • Block container box:一个容器,只包含Block-level boxes或只包含inline-level boxes(见后文)

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

    创建BFC

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

    • 根元素或其它包含它的元素
    • 浮动元素 (元素的 float 不是 none)
    • 绝对定位元素 (元素具有 position 为 absolute 或 fixed)
    • 内联块 (元素具有 display: inline-block)
    • 表格单元格 (元素具有 display: table-cell,HTML表格单元格默认属性)
    • 表格标题 (元素具有 display: table-caption, HTML表格标题默认属性)
    • 具有overflow 且值不是 visible 的块元素,
    • display: flow-root
    • column-span: all 应当总是会创建一个新的格式化上下文,即便具有 column-span: all 的元素并不被包裹在一个多列容器中

    布局规则

    • 内部的Box会在垂直方向,一个接一个地放置。
    • Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠
    • 每个元素的margin box的左边, 与包含块border - box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
    • BFC的区域不会与float box重叠。
    • 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
    • inline-element:以下'display'为'inline','inline-table'和'inline-block'的元素时内联级元素
    • inline-level box:内联级元素生成行级盒
    • inline box: 参与包含他的IFC的inline-level是inline-box,所有display:inline的非替换元素时inline box
    • atomic inline-level boxes:不参与IFC的inline box,同时这些元素必须是非替换元素或者display:inline-block|inline-table
    • anonymous inline boxes: 直接包含在块级盒的文本
    • line box: 由IFC产生的盒,用于表示一行。在块盒里面,行盒从块盒一边排版到另一边。 当有浮动时, 行盒从左浮动的最右边排版到右浮动的最左边。
    image.png

    布局规则

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

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

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

    • 在IFC中,内联元素在水平方向上一个接一个的排布
    • line box的宽度由该盒子包含的块填充同时会受浮动(float:left|right)块影响。行盒的高度由行盒内最高的inline boxes决定
    • 在IFC中, 内联元素排列方式可以是基于顶部,底部,baseline
    • 当内部的容器盒子太多了一个line box装不下来,他们折行之后会变成两个或者多个line box, line box们垂直方向无间隔地堆叠,但不能重叠
    • 被分裂的inline box的分裂处的margins, borders 和 padding不会有任何视觉效果

    解释

    <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演示,但是简书不能显示,差评,再见。

    相关文章

      网友评论

          本文标题:BFC,IFC学习笔记

          本文链接:https://www.haomeiwen.com/subject/zowtdxtx.html