BFC

BFC即 Block Fomatting Context = block-level box + Formatting Context。css

block-level box即塊級元素:display屬性爲block, list-item, table的元素,會生成block-level box。而且參與 block fomatting context;html

inline-level box即行內元素:display 屬性爲 inline, inline-block, inline-table的元素,會生成inline-level box。而且參與 inline formatting context;瀏覽器

Formatting contextide

Formatting context是W3C CSS2.1規範中的一個概念。它是頁面中的一塊渲染區域,而且有一套渲染規則,它決定了其子元素將如何定位,以及和其餘元素的關係、相互做用。最多見的 Formatting context 有 Block fomatting context (簡稱BFC)和 Inline formatting context(簡稱IFC)。佈局

CSS2.1 中只有BFC和IFC, CSS3中還增長了G(grid)FC和F(flex)FC。flex

BFC 定義ui

BFC(Block formatting context)直譯爲"塊級格式化上下文"。它是一個獨立的渲染區域,只有Block-level box參與, 它規定了內部的Block-level Box如何佈局,而且與這個區域外部絕不相干。 spa

BFC的生成scala

上邊提到BFC是一塊渲染區域,那這塊渲染區域在哪?它有多大?這些由生成BFC的元素決定,CSS2.1中規定知足下列CSS聲明之一的元素便會生成BFC。code

1 根元素

2 float的值不爲none

3 overflow的值不爲visible

4 display的值爲inline-block、table-cell、table-caption

5 position的值爲absolute或fixed

BFC的約束規則

1 內部的Box會在垂直方向上一個接一個的放置

2 垂直方向上的距離由margin決定。(完整的說法是:屬於同一個BFC的兩個相鄰Box的margin會發生重疊(塌陷),與方向無關。)

3 每一個元素的左外邊距與包含塊的左邊界相接觸(從左向右),即便浮動元素也是如此。(這說明BFC中子元素不會超出他的包含塊,而position爲absolute的元素能夠超出他的包含塊邊界)

4 BFC的區域不會與float的元素區域重疊

5 計算BFC的高度時,浮動子元素也參與計算

6 BFC就是頁面上的一個隔離的獨立容器,容器裏面的子元素不會影響到外面元素,反之亦然

margin重疊

兩個元素之間發生了margin重疊(塌陷),以最大的值爲準。

<style>

    p {

        color: #f55;

        background: #fcc;

        width: 200px;

        line-height: 100px;

        text-align:center;

        margin: 100px;

    }

</style>

<body>

    <p>Haha</p>

    <p>Hehe</p>

</body>

以上代碼margin會發生重疊。咱們能夠在p外面包裹一層容器,並觸發該容器生成一個新BFC。那麼兩個P便不屬於同一個BFC,就不會發生margin重疊了。

<style>

    .wrap {

        overflow: hidden;// 新的BFC

    }

    p {

        color: #f55;

        background: #fcc;

        width: 200px;

        line-height: 100px;

        text-align:center;

        margin: 100px;

    }

</style>

<body>

    <p>Haha</p>

    <div class="wrap">

        <p>Hehe</p>

    </div>

</body>

如下代碼相鄰Box水平方向margin重疊。

<!doctype HTML>

<html>

<head>

<style type="text/css">

 

    #green {

        margin:10px 10px 10px 10px

    }

    #blue {

        margin:10px 10px 10px 10px

    }

    #red {

        margin:10px 10px 10px 10px

    }

    body {

        writing-mode:tb-rl;

    }

</style>

</head>

<body>

<div id="green" style="background:lightgreen;height:100px;width:100px;"></div>

<div id="blue" style="background:lightblue;height:100px;width:100px;"></div>

<div id="red" style="background:pink;height:100px;width:100px;"></div>

</body>

</html>

咱們能夠給div加個display:inline-block,觸每一個div容器生成一個BFC。那麼三個DIV便不屬於同一個BFC(這裏是body根元素造成的BFC),就不會發生margin重疊了。

清除內部浮動

<style>

    .par {

        border: 5px solid #fcc;

        width: 300px;

    }

 

    .child {

        border: 5px solid #f66;

        width:100px;

        height: 100px;

        float: left;

    }

</style>

<body>

    <div class="par">

        <div class="child"></div>

        <div class="child"></div>

    </div>

</body>

根據BFC佈局規則第六條:計算BFC的高度時,浮動元素也參與計算。

如下代碼爲達到清除內部浮動,咱們能夠觸發par生成BFC,那麼par在計算高度時,par內部的浮動元素child也會參與計算。

.par {
    overflow: hidden;
}

以前看聖盃佈局,雙飛翼佈局一直不理解原理。。。今天看了下面的有一些懂了。還有浮動元素如何撐開父元素的高度。其實用的就是BFC原理!

自適應兩欄佈局

<style>

    body {

        width: 300px;

        position: relative;

    }

    .aside {

        width: 100px;

        height: 150px;

        float: left;

        background: #f66;

    }

    .main {

        height: 200px;

        background: #fcc;

    }

</style>

<body>

    <div class="aside"></div>

    <div class="main"></div>

</body>

根據BFC佈局規則第3條:

每一個元素的margin box的左邊, 與包含塊border box的左邊相接觸(對於從左往右的格式化,不然相反)。即便存在浮動也是如此。所以,雖然存在浮動的元素aslide,但main的左邊依然會與包含塊的左邊相接觸。

根據BFC佈局規則第四條:

BFC的區域不會與float box重疊。所以咱們能夠經過經過觸發main生成BFC, 來實現main的左邊不與包含塊的左邊相接觸,與aslide右邊接觸。來實現自適應兩欄佈局。

.main {
    overflow: hidden;
}

當觸發main生成BFC後,這個新的BFC不會與浮動的aside重疊。所以會根據包含塊的寬度,和aside的寬度,自動調整寬窄。

自適應三欄佈局

如下佈局左右兩欄寬度固定,中間欄能夠根據瀏覽器寬度自適應。

<!DOCTYPE html>

<html> 

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <!--The viewport meta tag is used to improve the presentation and behavior of the samples

    on iOS devices-->

  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>

  <title></title>

  <style>

    html, body { height: 100%; width: 100%; margin: 0; padding: 0; }

    .left{

      background:pink;

      float: left;

      width:180px;

    }

    .center{

      background:lightyellow;

      overflow:hidden;

    }

    .right{

      background: lightblue;

      width:180px;

      float:right;

    }

  </style>

</head>

<body class="claro">

  <div class="container">

    <div class="left">

      <pre>

  .left{

    background:pink;

    float: left;

    width:180px;

  }

      </pre>

    </div>

    <div class="right">

       <pre>

  .right{

    background:lightblue;

    width:180px;

    float:right;

  }

      </pre>

    </div>

    <div class="center">

    <pre>

  .center{

    background:lightyellow;

    overflow:hidden;

    height:116px;

  }

      </pre>

    </div>

  </div>

</html>

當BFC外部存在浮動時,它不該該影響BFC內部Box的佈局,BFC會經過變窄,而不與浮動有重疊。  -----  多欄佈局

當BFC內部有浮動時,爲了避免影響外部元素的佈局,BFC計算高度時會包括浮動的高度。 -----   浮動元素撐開父元素的高度

 

原文:http://www.javashuo.com/article/p-blcxqflw-w.html

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息