CSS基礎(chǔ)學(xué)習(xí)(二):BFC

什么是BFC#

bfc的全稱為Block Formatting Contexts,即塊級(jí)格式化上下文,W3C是這么定義BFC的
https://www.w3.org/TR/CSS2/visuren.html#block-formatting

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).

簡(jiǎn)單解釋就是,BFC在頁(yè)面上創(chuàng)建了一個(gè)獨(dú)立的單元,內(nèi)部元素不會(huì)影響外部元素的,而外部元素也不會(huì)影響到內(nèi)部元素

觸發(fā)BFC的方法:###

  1. 浮動(dòng)元素和position屬性為absolute的元素
  2. 非塊級(jí)盒子的塊級(jí)容器,如display屬性為inline-block,table-cells和table-captions的元素
  3. overflow屬性不為visible的塊級(jí)盒子(最常用的是overflow: hidden)

BFC的特性:###

  1. 在一個(gè)BFC中的塊級(jí)元素會(huì)從垂直的從上到下排列
  2. 相鄰的兩個(gè)盒子的間隙是由margin屬性決定的
  3. 相鄰的盒子垂直方向的margin會(huì)產(chǎn)生重疊現(xiàn)象
  4. 每個(gè)盒子的左邊緣會(huì)觸碰到容器的左邊緣(自右向左的除外)

BFC的應(yīng)用:###

  • 消除float的文字環(huán)繞效果

這里圖片左浮動(dòng),文字內(nèi)容自動(dòng)環(huán)繞

我們給包裹文字的div添加屬性overflow: hidden

  • 消除margin重疊效果

這里每個(gè)紅色的div都設(shè)置了上下各20px的外邊距,可以看出在中間處發(fā)生了margin重疊

而我們給下面這個(gè)紅色的div外面加個(gè)div并設(shè)置overflow: hidden屬性

重疊效果被消除了

結(jié)束之言:#

關(guān)于BFC的總結(jié),小生就說(shuō)這么多吧。本文僅供參考,歡迎大家一起來(lái)交流

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問(wèn)答題47 /72 常見瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,193評(píng)論 1 92
  • 先前在學(xué)習(xí)CSS float時(shí),有同學(xué)提到了BFC這個(gè)詞,作為求知好問(wèn)的好學(xué)生,哪里不懂查哪里,那么今天就來(lái)研究一...
    這名字真不對(duì)閱讀 6,706評(píng)論 3 19
  • 什么是BFC BFC全稱是Block Formatting Context,即塊格式化上下文。它是CSS2.1規(guī)范...
    陌客百里閱讀 573評(píng)論 3 4
  • BFC全稱是Block Formatting Context,即塊格式化上下文。它是CSS2.1規(guī)范定義的,關(guān)于C...
    xf0128閱讀 464評(píng)論 0 0
  • 轉(zhuǎn)載自(http://web.jobbole.com/83274/) BFC BFC全稱是Block Format...
    居客俠閱讀 2,208評(píng)論 0 20

友情鏈接更多精彩內(nèi)容