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

什么是BFC#

bfc的全稱為Block Formatting Contexts,即塊級格式化上下文,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).

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

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

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

BFC的特性:###

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

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

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

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

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

  • 消除margin重疊效果

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

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

重疊效果被消除了

結(jié)束之言:#

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

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

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

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

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