參考教程:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.htmlcss
語法:git
1、Flex 佈局是什麼?
Flex 是 Flexible Box 的縮寫,意爲"彈性佈局",用來爲盒狀模型提供最大的靈活性。github
任何一個容器均可以指定爲 Flex 佈局。web
.box{ display: flex; }
行內元素也可使用 Flex 佈局。瀏覽器
.box{ display: inline-flex; }
Webkit 內核的瀏覽器 (蘋果系統),必須加上
-webkit
前綴。xss.box{ display: -webkit-flex; /* Safari */ display: flex; }
注意,設爲 Flex 佈局之後,子元素的
float
、clear
和vertical-align
屬性將失效。佈局2、基本概念
採用 Flex 佈局的元素,稱爲 Flex 容器(flex container),簡稱"容器"。它的全部子元素自動成爲容器成員,稱爲 Flex 項目(flex item),簡稱"項目"。flex
容器默認存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點)叫作
main start
,結束位置叫作main end
;交叉軸的開始位置叫作cross start
,結束位置叫作cross end
。flexbox項目默認沿主軸排列。單個項目佔據的主軸空間叫作
main size
,佔據的交叉軸空間叫作cross size
。3、容器的屬性
如下6個屬性設置在容器上。
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
3.1 flex-direction屬性
flex-direction
屬性決定主軸的方向(即項目的排列方向)。.box { flex-direction: row | row-reverse | column | column-reverse; }
它可能有4個值。
row
(默認值):主軸爲水平方向,起點在左端。row-reverse
:主軸爲水平方向,起點在右端。column
:主軸爲垂直方向,起點在上沿。column-reverse
:主軸爲垂直方向,起點在下沿。3.2 flex-wrap屬性
默認狀況下,項目都排在一條線(又稱"軸線")上。
flex-wrap
屬性定義,若是一條軸線排不下,如何換行。
.box{ flex-wrap: nowrap | wrap | wrap-reverse; }
它可能取三個值。
(1)
nowrap
(默認):不換行(多的則自動調整)
(2)
wrap
:換行,第一行在上方。
(3)
wrap-reverse
:換行,第一行在下方。
3.3 flex-flow
flex-flow
屬性是flex-direction
屬性和flex-wrap
屬性的簡寫形式,默認值爲row nowrap
。.box { flex-flow: <flex-direction> || <flex-wrap>; }
3.4 justify-content屬性
justify-content
屬性定義了項目在主軸上的對齊方式。.box { justify-content: flex-start | flex-end | center | space-between | space-around; }
它可能取5個值,具體對齊方式與軸的方向有關。下面假設主軸爲從左到右。
flex-start
(默認值):左對齊flex-end
:右對齊center
: 居中space-between
:兩端對齊,項目之間的間隔都相等。space-around
:每一個項目兩側的間隔相等。因此,項目之間的間隔比項目與邊框的間隔大一倍。3.5 align-items屬性
align-items
屬性定義項目在交叉軸上如何對齊。.box { align-items: flex-start | flex-end | center | baseline | stretch; }
它可能取5個值。具體的對齊方式與交叉軸的方向有關,下面假設交叉軸從上到下。
flex-start
:交叉軸的起點對齊。flex-end
:交叉軸的終點對齊。center
:交叉軸的中點對齊。baseline
: 項目的第一行文字的基線對齊。stretch
(默認值):若是項目未設置高度或設爲auto,將佔滿整個容器的高度。3.6 align-content屬性
align-content
屬性定義了多根軸線的對齊方式。若是項目只有一根軸線,該屬性不起做用。.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }
該屬性可能取6個值。
flex-start
:與交叉軸的起點對齊。flex-end
:與交叉軸的終點對齊。center
:與交叉軸的中點對齊。space-between
:與交叉軸兩端對齊,軸線之間的間隔平均分佈。space-around
:每根軸線兩側的間隔都相等。因此,軸線之間的間隔比軸線與邊框的間隔大一倍。stretch
(默認值):軸線佔滿整個交叉軸。4、項目的屬性
如下6個屬性設置在項目上。
order
flex-grow
flex-shrink
flex-basis
flex
align-self
4.1 order屬性
order
屬性定義項目的排列順序。數值越小,排列越靠前,默認爲0。.item { order: <integer>; }
4.2 flex-grow屬性
flex-grow
屬性定義項目的放大比例,默認爲0
,即若是存在剩餘空間,也不放大。.item { flex-grow: <number>; /* default 0 */ }
若是全部項目的
flex-grow
屬性都爲1,則它們將等分剩餘空間(若是有的話)。若是一個項目的flex-grow
屬性爲2,其餘項目都爲1,則前者佔據的剩餘空間將比其餘項多一倍。4.3 flex-shrink屬性
flex-shrink
屬性定義了項目的縮小比例,默認爲1,即若是空間不足,該項目將縮小。.item { flex-shrink: <number>; /* default 1 */ }
若是全部項目的
flex-shrink
屬性都爲1,當空間不足時,都將等比例縮小。若是一個項目的flex-shrink
屬性爲0,其餘項目都爲1,則空間不足時,前者不縮小。負值對該屬性無效。
4.4 flex-basis屬性
flex-basis
屬性定義了在分配多餘空間以前,項目佔據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多餘空間。它的默認值爲auto
,即項目的原本大小。.item { flex-basis: <length> | auto; /* default auto */ }
它能夠設爲跟
width
或height
屬性同樣的值(好比350px),則項目將佔據固定空間。4.5 flex屬性
flex
屬性是flex-grow
,flex-shrink
和flex-basis
的簡寫,默認值爲0 1 auto
。後兩個屬性可選。.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] }
該屬性有兩個快捷值:
auto
(1 1 auto
) 和 none (0 0 auto
)。建議優先使用這個屬性,而不是單獨寫三個分離的屬性,由於瀏覽器會推算相關值。
4.6 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屬性徹底一致。
實例:
一、不用flex佈局
1 <!--pages/index/index.wxml--> 2 <view class="container"> 3 4 <view class="txt01"> 5 <text >pages</text> 6 </view> 7 8 <view class="txt02"> 9 <text >pages</text> 10 </view> 11 12 <view class="txt03"> 13 <text >pages</text> 14 </view> 15 16 <view class="txt04"> 17 <text >pages</text> 18 </view> 19 20 </view> 21 /* pages/index/index.wxss */ 22 .txt01{ 23 width: 150rpx; 24 height: 150rpx; 25 background-color: red; 26 } 27 .txt02{ 28 width: 150rpx; 29 height: 150rpx; 30 background-color: green; 31 } 32 .txt03{ 33 width: 150rpx; 34 height: 150rpx; 35 background-color: blue; 36 } 37 .txt04{ 38 width: 150rpx; 39 height: 150rpx; 40 background-color: yellow; 41 }
![]()
二、加上flex-direction
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row; // 默認row 5 } 6 。。。。。。。。
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: column; // 5 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row-reverse; 5 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: column-reverse; 5 }
![]()
三、加上flex-wrap
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-wrap: nowrap //默認,不換行 自動調整 5 } 6 。。。。。。。。。。
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: column; //縱向 5 flex-wrap: nowrap; 6 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-wrap: wrap; //換行 5 } 6 。。。。。
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: column; 5 flex-wrap: wrap; 6 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-wrap: wrap-reverse; //換行 逆 5 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: column; 5 flex-wrap: wrap-reverse; 6 }
![]()
三、加上flex-flow
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-flow: row nowrap // 至關於:flex-direction:row; flex-wrap:nowrap 5 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-flow: row wrap //至關於 flex-direction:row; flex-wrap:wrap 5 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-flow: row wrap-reverse // 至關於flex-direction:row;flex-wrap:wrap-reverse 5 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-flow: column nowrap //至關於:flex-direction:column; flex-wrap:nowrap 5 } 6 。。。。。。。 7 /* pages/index/index.wxss */ 8 .container{ 9 display: flex; 10 flex-flow: column wrap //至關於:flex-direction:column; flex-wrap:wrap 11 } 12 。。。。。。。 13 /* pages/index/index.wxss */ 14 .container{ 15 display: flex; 16 flex-flow: column wrap-rever //至關於:flex-direction:column;flex-wrap:wrap-reverse 17 }
四、加上justify-content
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row; 5 justify-content: flex-start //左對齊,無間隙 6 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row; 5 justify-content: flex-end //右對齊,無間隙 6 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row; 5 justify-content: center //居中,無間隙 6 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row; 5 justify-content: space-between //兩端對齊,項目之間的間隔都相等 6 }
![]()
1 /* pages/index/index.wxss */ 2 .container{ 3 display: flex; 4 flex-direction: row; 5 justify-content: space-around //每一個項目兩側的間隔相等。因此,項目之間的間隔比項目與邊框的間隔大一倍 6 }
![]()