日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

BFC与IFC概念理解+布局规则+形成方法+用处

發(fā)布時(shí)間:2025/3/19 68 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BFC与IFC概念理解+布局规则+形成方法+用处 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

想要理解BFC與IFC,首先要理解另外兩個(gè)概念:Box 和 FC(即 formatting context)。

Box

一個(gè)頁(yè)面是由很多個(gè) Box 組成的,元素的類型和 display 屬性決定了這個(gè) Box 的類型。不同類型的 Box,會(huì)參與不同的 Formatting Context。

Block level的box會(huì)參與形成BFC,比如display值為block,list-item,table的元素。

Inline level的box會(huì)參與形成IFC,比如display值為inline,inline-table,inline-block的元素。

參考:W3C文檔block-level

FC(Formatting Context)

它是W3C CSS2.1規(guī)范中的一個(gè)概念,定義的是頁(yè)面中的一塊渲染區(qū)域,并且有一套渲染規(guī)則,它決定了其子元素將如何定位,以及和其他元素的關(guān)系和相互作用

常見的Formatting Context 有:Block Formatting Context(BFC | 塊級(jí)格式化上下文) 和 Inline Formatting Context(IFC |行內(nèi)格式化上下文)。

下面就來(lái)介紹IFC和BFC的布局規(guī)則。

IFC布局規(guī)則:

在行內(nèi)格式化上下文中,框(boxes)一個(gè)接一個(gè)地水平排列,起點(diǎn)是包含塊的頂部。水平方向上的 margin,border 和 padding在框之間得到保留。框在垂直方向上可以以不同的方式對(duì)齊:它們的頂部或底部對(duì)齊,或根據(jù)其中文字的基線對(duì)齊。包含那些框的長(zhǎng)方形區(qū)域,會(huì)形成一行,叫做行框。

BFC布局規(guī)則:

W3C原文:

Floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport) establish new block formatting contexts for their contents.

In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block. The vertical distance between two sibling boxes is determined by the 'margin' properties. Vertical margins between adjacent block-level boxes in a block formatting context collapse.

In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box's line boxes may shrink due to the floats), unless the box establishes a new block formatting context (in which case the box itself may become narrower due to the floats).

整理為中文:

  • 內(nèi)部的Box會(huì)在垂直方向,一個(gè)接一個(gè)地放置。

  • Box垂直方向的距離由margin決定。屬于同一個(gè)BFC的兩個(gè)相鄰Box的margin會(huì)發(fā)生重疊

  • 每個(gè)元素的左外邊緣(margin-left), 與包含塊的左邊(contain box left)相接觸(對(duì)于從左往右的格式化,否則相反)。即使存在浮動(dòng)也是如此。除非這個(gè)元素自己形成了一個(gè)新的BFC。

  • BFC的區(qū)域不會(huì)與float box重疊。

  • BFC就是頁(yè)面上的一個(gè)隔離的獨(dú)立容器,容器里面的子元素不會(huì)影響到外面的元素。反之也如此。

  • 計(jì)算BFC的高度時(shí),浮動(dòng)元素也參與計(jì)算

  • 參考:
    W3C文檔inline-formatting
    W3C文檔block-formatting

    怎樣形成一個(gè)BFC?

    塊級(jí)格式化上下文由以下之一創(chuàng)建:

  • 根元素或其它包含它的元素

  • 浮動(dòng) (元素的 float 不是 none)

  • 絕對(duì)定位的元素 (元素具有 position 為 absolute 或 fixed)

  • 非塊級(jí)元素具有 display: inline-block,table-cell, table-caption, flex, inline-flex

  • 塊級(jí)元素具有overflow ,且值不是 visible

  • 整理到這兒,對(duì)于上面第4條產(chǎn)生了一個(gè)small small的疑問:為什么display: inline-block;的元素是inline level 的元素,參與形成IFC,卻能創(chuàng)建BFC?

    后來(lái)覺得答案是這樣的:inline-block的元素的內(nèi)部是一個(gè)BFC,但是它本身可以和其它inline元素一起形成IFC。

    BFC用處

    1. 清除浮動(dòng)

    <div class="wrap"> <section>1</section> <section>2</section> </div> .wrap {border: 2px solid yellow;width: 250px; } section {background-color: pink;float: left;width: 100px;height: 100px; }

    可以看到,由于子元素都是浮動(dòng)的,受浮動(dòng)影響,邊框?yàn)辄S色的父元素的高度塌陷了。

    解決方案:為 .wrap 加上 overflow: hidden;使其形成BFC,根據(jù)BFC規(guī)則第六條,計(jì)算高度時(shí)就會(huì)計(jì)算float的元素的高度,達(dá)到清除浮動(dòng)影響的效果。

    2. 布局:自適應(yīng)兩欄布局

    <div> <aside></aside> <main>我是好多好多文字會(huì)換行的那種蛤蛤蛤蛤蛤蛤蛤蛤蛤蛤蛤蛤蛤</main> </div> div {width: 200px;} aside {background-color: yellow;float: left;width: 100px;height: 50px; } main {background-color: pink; }

    可以看到右側(cè)元素的一部分跑到了左側(cè)元素下方。

    解決方案:為main設(shè)置 overflow: hidden; 觸發(fā)main元素的BFC,根據(jù)規(guī)則第4、5條,BFC的區(qū)域是獨(dú)立的,不會(huì)與頁(yè)面其他元素相互影響,且不會(huì)與float元素重疊,因此就可以形成兩列自適應(yīng)布局

    3. 防止垂直margin合并

    <section class="top">1</section> <section class="bottom">2</section> section {background-color: pink;margin-bottom: 100px;width: 100px;height: 100px; } .bottom {margin-top: 100px; }

    可以看到,明明.top和.bottom中間加起來(lái)有200px的margin值,但是我們只能看到100px。這是因?yàn)樗麄兊耐膺吘嘞嘤霭l(fā)生了合并。

    怎樣解決:為其中一個(gè)元素的外面包裹一層元素。并為這個(gè)外層元素設(shè)置 overflow: hidden;,使其形成BFC。因?yàn)锽FC內(nèi)部是一個(gè)獨(dú)立的容器,所以不會(huì)與外部相互影響,可以防止margin合并。

    <section class="top">1</section> <div class="wrap"> <section class="bottom">2</section> </div> .wrap {overflow: hidden; }

    總結(jié)

    以上是生活随笔為你收集整理的BFC与IFC概念理解+布局规则+形成方法+用处的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。