flex布局使用講解

一、起步

1、flex介紹

使用flex彈性布局的元素,稱為flex 容器(flex-container)。它的所有子元素自動成為容器成員,稱為 flex 項目(flex-item)。

即需要分為兩個部分,一為flex-container,另一部分為flex-item,flex-item包含于flex-container中,如下面代碼中,類名為“container”的div塊為flex-container,div中所包含的類名為“item1”、“item2”、“item3”的div塊均為flex-item。

注意:設為 flex布局以后,子元素的float、clear和vertical-align屬性將失效。

<div class="container">    
    <div class="item item1">item1</div>    
    <div class="item item2">item2</div>    
    <div class="item item3">item3</div>
</div>
.container  {
    width: 500px;
    height: 400px;
    background-color: orange;
    margin: 0 auto;
}

.item {
    width: 100px;
    height: 100px;
    color: #fff;
    line-height: 100px;
    text-align: center;
}

.item1 {
  background-color: #f00;
}

.item2 {
  background-color: #0f0;
}

.item3 { 
  background-color: #00f;
}

二、flex主軸概念

(1)彈性布局開啟后flex-container元素默認存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。
(2)主軸的開始位置(與邊框的交叉點)叫做main start,結束位置叫做main end
(3)交叉軸的開始位置叫做cross start,結束位置叫做cross end
(4)flex-item元素默認沿主軸排列。單個項目占據的主軸空間叫做main size,占據的交叉軸空間叫做cross size

二、開啟flex布局

對flex-container以及其中的flex-item開啟flex布局,需在flex-container做如下設置:

.container{
      width: 500px;
      height: 400px;
      background-color: orange;
      margin: 0 auto;
      display:flex || inline-flex; 
      // 開啟flex布局,當display為flex,此容器為塊級元素,當為inline-flex時為行內元素
      //Webkit 內核的瀏覽器,必須加上-webkit前綴,即-webkit-flex。
}
//其他item屬性暫時不變
<div class="container">    
    <div class="item item1">item1</div>    
    <div class="item  item2">item2</div>    
    <div class="item  item3">item3</div>
</div>
<span>我是行內元素</span>

分布如下圖所示:

三、flex-container屬性

flex-container可設置如下6種屬性:
(1)flex-direction:控制主軸放方向,默認屬性為row
(2)justify-content:決定了flex-item與main axis上的對齊方式
(3)align-items:決定了flex-item與cross axis上的對齊方式
(4)flex-wrap:決定了flex container是單行和多行
(5)flex-flow為flex-direction和flex-wrap的縮寫屬性
(6)align-content決定了多行flex-item與cross axis上的對齊方式,用法與justify-content相似

1、flex-direction有如下4個選項:

(1)row:flex-direction控制主軸方向,默認屬性為row。
(2)row-revers:翻轉主軸方向。
(3)column:主軸方向改為豎直方向。
(4)column-reverse:翻轉此時的主軸方向。

.container {
    flex-direction:row || row-reverse|| column || column-reverse || column-reverse;
    // flex-direction:控制主軸方向,默認屬性為row
    // row-revers:翻轉主軸方向
    // column:主軸方向改為豎直方向
    // column-reverse:翻轉此時的主軸方向
}

示例如下:


2、justify-content:決定了flex-item與main axis上的對齊方式

(1)flex-start(默認值):與main start對齊
(2)flex-end:與main end對齊
(3)center:居中對齊
(4)space-between: flex-item之間距離相等,與main start main end兩端對齊
(5)space-around: flex-item之間距離相等,flex-item與main start、main end之間的距離等于flex-item之間的距離
(6)space-evenly: flex-item之間距離相等,flex-item與main start、main end之間的距離是flex-item之間的距離的一半

.container {
justify-content: flex-start || flex-end || center || space-between ||  space-around
// flex-start(默認值):與main start對齊
// flex-end:與main end對齊
// center:居中對齊
// space-between: flex-item之間距離相等,與main start、main end兩端對齊
// space-around:  flex-item之間距離相等,flex-item與main start、main end之間的距離等于flex-item之間的距離
// space-evenly:  flex-item之間距離相等,flex-item與main start、main end之間的距離是flex-item之間的距離的一半
}

示例如下:

3、align-items:決定了flex-item與cross axis上的對齊方式

(1)normal:與stretch效果一樣
(2)stretch:當flex-item在cross axi放下的size為auto時,會自動拉伸填充至flex container
(3)flex-start(默認值):items在cross start對齊
(4)flex-end:在cross end對齊
(5)center:居住對齊
(6)baseline:基準線對齊

.container {
   align-items: normal || stretch || flex-start || flex-end || center || baseline
   // normal: 與stretch效果一樣
   // stretch:當flex-item在cross axi放下的size為auto時,會自動拉伸填充至flex  container
   // flex-start(默認值):items在cross start對齊
   // flex-end:在cross end對齊
   // center:居住對齊
   //  baseline:基準線對齊
}

示例如下:

4、flex-wrap:決定了flex container是單行和多行

(1)nowrap(默認值):單行
(2)wrap:多行,當第一行最后剩的距離占不下時,會按cross方向等分的flex container部分對齊分配第二行
(3)wrap-reverse:多行(對比wrap,cross start與cross end相反)

.container {
    flex-wrap: nowrap || wrap || wrap-reverse
    // nowrap(默認值):單行
    // wrap:多行,當第一行最后剩的距離占不下時,會按cross方向等分的flex container部分對齊分配第二行
    // wrap-reverse:多行(對比wrap,cross start與cross end相反)
}

5、flex-flow:為flex-direction和flex-wrap的縮寫屬性

例如:

.container {
    flex-flow:column wrap
}

6、align-content:決定了多行flex-item與cross axis上的對齊方式,用法與justify-content相似

(1)stretch(默認值):與align-items的stretch效果一樣
(2)flex-start:與cross start對齊
(3)flex-start:與cross end對齊
(4)center居住對齊
(5)space-between: flex-item之間距離相等,與cross start、cross end兩端對齊
(6)space-around: flex-item之間距離相等,flex-item與cross start、cross end之間的距離等于flex-item之間的距離
(7)space-evenly: flex-item之間距離相等,flex-item與cross start、cross end之間的距離是flex-item之間的距離的一半

.container {
    align-content:stretch || flex-start || flex-end || center || space-between || space-around || space-evenly
    // stretch(默認值):與align-items的stretch效果一樣
    //flex-start:與cross start對齊
    //flex-end:與cross end對齊
    //center居住對齊
    //space-between: flex-item之間距離相等,與cross start、cross end兩端對齊
    //space-around:  flex-item之間距離相等,flex-item與cross start、cross end之間的距離等于flex-item之間的距離
    //space-evenly:  flex-item之間距離相等,flex-item與cross start、cross end之間的距離是flex-item之間的距離的一半
}

示例如下:


四、flex-item中的屬性

flex-item共用6個設置屬性,如下所示:
(1)order:定義項目的排列順序。數值越小,排列越靠前,默認為0 。
(2)align-self:覆蓋flex:-container設置的align-items。
(3)flex-grow:決定了flex-item放大比例,默認為0,即如果存在剩余空間,也不放大。
(4)flex-shrink決定了flex-item縮小比例,默認為1,即如果空間不足,該項目將縮小。
(5)flex-basis屬性定義了在分配多余空間之前,flex-item占據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多余空間。它的默認值為auto,即flex-item的本來大小。
(6)flex是flex-grow || flex-shrink || flex-basis的簡寫,flex屬性可以指定1個,2個,3個值。

1、order:定義項目的排列順序

(1)可以設置任意整數(正整數、負整數、0),值越小越排在前面
(2)默認值是0
示例如下:

2、align-self:覆蓋flex-container設置的align-items。

align-self屬性允許單個項目有與其他項目不一樣的對齊方式,可覆蓋align-items屬性。默認值為auto,表示繼承父元素的align-items屬性,如果沒有父元素,則等同于stretch。
示例:


3、flex-grow:可以設置任意非負數字(正小數、正整數、0),默認值是0

(1)當flex-container在main axis有剩余size時才會生效
(2) 如果所有flex-item的flex-grow總和sum超過1,每個flex-item擴展的size為:flex-container的剩余size * flex-grow/sum
(3)如果所有flex-item的flex-grow總和sum不超過1,每個flex-item擴展的size為:flex-container的剩余size * flex-grow
(4)flex-item擴展后的所有size不能超過max-width

示例:

20.png

4、flex-shrink可:以設置任意非負數字(正小數、正整數、0),默認值是1

(1)當flex-item 在main axis上超過flex-container的size時才會生效
(2)如果所有flex-item的flex-shrink總和sum超過1,每個flex-item收縮的size為:flex-item超出flex-container的size * (收縮比例 / 所有flex-item收縮比例之和)
(3)如果所有flex-item的flex-shrink總和sum不超過1,每個flex-item收縮的size為:每個flex-item收縮的size為:flex-item超出flex-container的size * 所有flex-item收縮比例之和 * (收縮比例/所有flex-item收縮比例之和)
i. 收縮比例=flex-shrink * flex-item的base-size
ii. base-size就是flex-item放入flex-container之前的size
iii. flex items收縮后的所有size不能小于min-width

示例:

5、flex-basis:用來設置flex-item在main axis方向上的size,

(1)設置為auto為默認值,即為原來的大小、
(2)設置具體的寬度數值(100px),即可改變flex-item的size
決定flex-item最終size的因素,從優先級高到底
i.max-width/max-height/min-width/min-height
ii.flex-basis
iii.width/height
iiii.內容本身的size

6、flex:是flex-grow || flex-shrink || flex-basis的簡寫,flex屬性可以指定1個,2個,3個值

(1)單值語法:值必須為以下其中之一:
一個無單位數(<number>):它會被當做flex-grow的值
一個有效的寬度(width)值:它會被當做flex-basis的值
關鍵字none,auto活initial
(2)雙值語法:第一個值必須為一個無單位數,并且會被當做flex-grow的值
第二個值必須為以下之一:
一個無單位數:它會被當做flex-shrink的值
一個有效的寬度值:它會被當做flex-basis的值
(3)三值語法:
第一個值必須為無單位數,并且會被當做flex-grow的值
第二個值必須為無單位數,并且會被當做flex-shrink的值
第二個值必須為一個有效的寬度值數,并且會被當做flex-basis的值

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

常用的示例:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,936評論 6 535
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,744評論 3 421
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,879評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,181評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,935評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,325評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,384評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,534評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,084評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,892評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,067評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,623評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,322評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,735評論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,990評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,800評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,084評論 2 375