Flex佈局

本文最先發布在:Rootrl's bloghtml

簡介

Flex是Flexible Box的縮寫,意爲彈性佈局。是W3C在2009年提出的一個新的佈局方案。能夠方便的實現各類頁面佈局。目前瀏覽器兼容以下:
Flex supportgit

Flex在移動端開發上已經是主流,好比H5頁面,微信小程序等等。github

Why Flex

傳統的佈局方案主要基於CSS盒子模型,依賴Display、Position、Float等屬性。可是它對於一些特殊佈局很是不方便,好比水平垂直居中。傳統方式實現起來很是繁雜,各類黑科技,好比如下是一種水平垂直居中的實現方式:web

基礎的DOM以下,一個父元素是寬高爲500px的紅色容器,包裹着寬高爲100px的黃色子容器:小程序

<style>
    * {
        margin: 0;
        padding: 0;
    }

    .dad {
        background-color: red;
        width: 500px;
        height: 500px;
    }

    .son {
        background-color: yellow;
        width: 100px;
        height: 100px;
    }
</style>

<div class="dad">
    <div class="son">
        
    </div>
</div>

現要實現子元素在父元素中水平垂直居中,傳統的方式以下,基於定位。微信小程序

.dad {
    position: relative;
}

.son {
    position: absolute;
    margin: auto;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

而Flex只需幾行代碼搞定,很是方便:瀏覽器

.dad {
    display: flex;
    justify-content: center;
    align-items: center
}

使用

任何一個容器均可以指定爲Flex佈局。微信

.box {
    display: flex;
}

行內元素也可使用Flex佈局:佈局

.box {
    display: inline-flex
}

Webkit內核的瀏覽器需加上-webkit前綴。post

注意:
設爲Flex佈局後,子元素的float、clear、vertical-alian屬性都將失效。

基本概念

父容器設爲flex後,它的全部子元素自動成爲容器成員(這裏加入個人理解,若是不對請指出:這裏的子元素不論什麼元素,塊級仍是行內,並且只做用於子元素,孫元素不起做用,需繼續在子元素上設置display:flex)

Flex的基本概念就是容器和軸,容器包括外層的父容器和內層的子容器(也叫項目,flex item),軸包括主軸和交叉軸。

如圖:
Flex container

容器默認存在兩根軸,水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點)叫作main start,結束位置叫作main end;交叉軸的開始位置叫作cross start,結束位置叫作cross end。

子容器(項目)默認沿主軸排列,單個項目佔據的主軸空間叫作main size,佔據的交叉軸空間叫作cross size。

Flex佈局主要涉及12個屬性(不含display:flex),其中容器和子容器各6個,可是日常使用到的基本只有4個屬性,父容器和子容器各2個。

做用於父容器的屬性:

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

做用於子容器(項目)的屬性:

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

詳細介紹

容器屬性

flex-direction

flex-direction屬性決定主軸的方向,及子容器(項目)的排列方向。

.box {
  flex-direction: row | row-reverse | column | column-reverse;
}

可能的值:

  • row(默認值):主軸爲水平方向,起點在左端。
  • row-reverse:主軸爲水平方向,起點在右端。
  • column:主軸爲垂直方向,起點在上沿。
  • column-reverse:主軸爲垂直方向,起點在下沿。

如圖:
Flex direction

flex-wrap

默認狀況下,項目都排在一條線(又稱"軸線")上。flex-wrap屬性定義,若是一條軸線排不下,如何換行。

.box {
  flex-wrap: nowrap | wrap | wrap-reverse;
}

可能的值:

  • nowrap(默認):不換行
  • wrap:換行,第一行在上方
  • wrap-reverse: 換行,第一行在下方

flex-flow

flex-flow屬性是flex-direction和flex-wrap屬性的簡寫形式,默認爲:row nowrap

.box {
    flex-flow: <flex-direction> || <flex-wrap>
}

justify-content

justify-content定義了項目在主軸上的對齊方式。

.box {
    justify-content: flex-start| flext-end | center | space-between |space-around;
}

它可能取5個值,具體對齊方式與軸的方向有關。下面假設主軸爲從左到右。

  • flex-start(默認值):左對齊
  • flex-end:右對齊
  • center: 居中
  • space-between:兩端對齊,項目之間的間隔都相等。
  • space-around:每一個項目兩側的間隔相等。因此,項目之間的間隔比項目與邊框的間隔大一倍。

如圖:
Justify content

align-items

align-items屬性定義了項目在交叉軸上的對齊方式:

.box {
    align-items: flex-start| flex-end | center | baseline | strech
}

它可能取5個值。具體的對齊方式與交叉軸的方向有關,下面假設交叉軸從上到下:

  • flex-start:交叉軸的起點對齊。
  • flex-end:交叉軸的終點對齊。
  • center:交叉軸的中點對齊。
  • baseline: 項目的第一行文字的基線對齊。
  • stretch(默認值):若是項目未設置高度或設爲auto,將佔滿整個容器的高度。

如圖:
Align items

align-content

align-content屬性定義了多根軸線的對齊方式。若是項目只有一根軸線,該屬性不起做用。

可能的值:

  • flex-start:與交叉軸的起點對齊。
  • flex-end:與交叉軸的終點對齊。
  • center:與交叉軸的中點對齊。
  • space-between:與交叉軸兩端對齊,軸線之間的間隔平均分佈。
  • space-around:每根軸線兩側的間隔都相等。因此,軸線之間的間隔比軸線與邊框的間隔大一倍。
  • stretch(默認值):軸線佔滿整個交叉軸。

如圖:

Align items

項目屬性

order

order屬性定義項目的排列順序。數值越小,排列越靠前,默認爲0,

.item {
    order: <integer>
}

flex-grow

flex-grow屬性定義項目放大比例,默認爲0,即若是存在剩餘空間,也不放大。

.item {
    flex-grow: <number> /* default 0 */
}

若是全部項目的flex-grow屬性都爲1,則它們將等分剩餘空間(若是有的話)。若是一個項目的flex-grow屬性爲2,其餘項目都爲1,則前者佔據的剩餘空間將比其餘項多一倍。

flex-shrink

flex-shrink屬性定義了項目的縮小比例,默認爲1,即若是空間不足,項目將縮小。

.item {
    flex-shrink: <number> /* default 1 */
}

若是全部項目的flex-shrink屬性都爲1,當空間不足時,都將等比例縮小。若是一個項目的flex-shrink屬性爲0,其餘項目都爲1,則空間不足時,前者不縮小。
負值對該屬性無效。

flex-basis

flex-basis屬性定義了在分配多餘空間以前,項目佔據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多餘空間。它的默認值爲auto,即項目的原本大小。

.item {
    flex-basis: <length> | auto; /* default auto */
}

它能夠設爲跟width或height屬性同樣的值(好比350px),則項目將佔據固定空間。

flex

flex屬性是flex-grow, flex-shrink 和 flex-basis的簡寫,默認值爲0 1 auto。後兩個屬性可選。

該屬性有兩個快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建議優先使用這個屬性,而不是單獨寫三個分離的屬性,由於瀏覽器會推算相關值。

align-self

align-self屬性容許單個項目有與其餘項目不同的對齊方式,可覆蓋align-items屬性。默認值爲auto,表示繼承父元素的align-items屬性,若是沒有父元素,則等同於stretch。

.item {
    align-self: auto | flex-start | flex-end| center | baseline | stretch
}

該屬性可能取6個值,除了auto,其餘都與align-items屬性徹底一致。

佈局實戰

基本網格佈局

最簡單的網格佈局,就是平均分佈。在容器裏面平均分配空間。

好比實現如圖佈局:
Grid佈局

Html代碼:

<div class="grid">
        <div class="grid-cell">cell1</div>
        <div class="grid-cell">cell2</div>
        <div class="grid-cell">cell3</div>
    </div>
    <div class="grid">
        <div class="grid-cell">cell1</div>
        <div class="grid-cell">cell2</div>
    </div>

Css代碼:

.grid {
        display: flex;
    }

    .grid-cell {
        flex: 1; /* 即爲flex-grow: 1; */
        border: green solid 1px; /* 可選,加邊框保證例子效果 */
    }

百分比佈局

某個網格寬度爲固定百分比佈局,其他網格平均分配剩餘空間。

如圖:
此處輸入圖片的描述

Html代碼:

<div class="grid">
        <div class="grid-cell cell-1of2">dsfdsf</div>
        <div class="grid-cell">dsfsdf</div>
        <div class="grid-cell">sdfsdf</div>
    </div>


    <div class="grid">
        <div class="grid-cell cell-1of3">dsfdsf</div>
        <div class="grid-cell">dsfsdf</div>
        <div class="grid-cell">sdfsdf</div>
    </div>

    <div class="grid">
        <div class="grid-cell cell-1of4">dsfdsf</div>
        <div class="grid-cell">dsfsdf</div>
        <div class="grid-cell">sdfsdf</div>
    </div>

Css代碼:

.grid {
        display: flex;
    }

    .grid-cell {
        flex: 1;
        border: red solid 1px;
    }

    .cell-full {
        flex: 0 0 100%;    
    }

    .cell-1of2 {
        flex: 0 0 50%;
    }

    .cell-1of3 {
        flex: 0 0 33.3333%;
    }

    .cell-1of4 {
        flex: 0 0 25%;
    }

聖盃佈局

聖盃佈局(Holy Grail Layout)指的是一種最多見的網站佈局。頁面從上到下,分紅三個部分:頭部(header),軀幹(body),尾部(footer)。其中軀幹又水平分紅三欄,從左到右爲:導航、主欄、副欄。

百分比佈局

Html代碼:

<body class="holy-grid">
    <header class="holy-grid-items">#header</header>
    <div class="holy-grid-content holy-grid-items">
        <div class="holy-grid-content-items holy-grid-content-left">
            # left
        </div>
        <div class="holy-grid-content-items holy-grid-content-center">
            # center
        </div>
        <div class="holy-grid-content-items holy-grid-content-right">
            # right
        </div>
    </div>
    <footer class="holy-grid-items">#footer</footer>
</body>

Css代碼以下:

* {
        margin: 0;
        padding: 0;
    }

    .holy-grid {
        display: flex;
        flex-direction: column;
        min-height: 100vh; /* 相對於視口的高度。視口被均分爲100單位的vh */
    }

    header, footer {
        text-align: center;
        flex: 0 0 100px;
    }

    .holy-grid-content {
        display: flex;
        flex: 1;
    }

    .holy-grid-content-items {
        flex: 1;
    }

    .holy-grid-content-left {
        flex: 0 0 150px;
    }

    .holy-grid-content-right {
        flex: 0 0 150px;
    }

好了,flex就到這裏了,本內容主要參考後面參考部分的教程,文章並非簡單搬過來的,而是理解後在寫的,例子也是本身在理解後,而後動手寫,再消化明白後才寫出來的。

今天一天都在搞這篇文章,如今Flex算是入門了。。

參考

Flex 佈局教程:語法篇 http://www.ruanyifeng.com/blo...
一勞永逸的搞定 flex 佈局 https://juejin.im/post/58e3a5...

相關文章
相關標籤/搜索