詳解flex佈局

文章目錄

一、flex佈局是什麼?

flex:彈性佈局
任何一個容器都可以指定爲 flex 佈局

.box{
  display: flex;
}

行內元素也可以使用flex 佈局

.box{
  display: inline-flex;
}

對於webkit 內核的瀏覽器,必須加上-webkit 前綴

.box{
  display: -webkit-flex; /* Safari */
  display: flex;
}

注意: 設置爲flex 佈局之後,子元素的float 、clear、 vertical-align(垂直文本對齊)都將失效

二、基本概念

flex 容器

在這裏插入圖片描述

  • 容器存在兩根軸,水平的主軸(main axis)和垂直的交叉軸(cross axis)
  • 主軸的開始位置(與邊框的交叉點)叫做main start,結束位置叫做main end
  • 交叉軸的開始位置叫做cross start,結束位置叫做cross end
  • 項目默認沿主軸排列。單個項目佔據的主軸空間叫做main size,佔據的交叉軸空間叫做cross size

三、容器的屬性

1. flex-direction屬性決定主軸的方向(即項目的排列方向)

.box {
  flex-direction: row | row-reverse | column | column-reverse;
}
  • row(默認值):主軸爲水平方向,起點在左端。
  • row-reverse:主軸爲水平方向,起點在右端。
  • column:主軸爲垂直方向,起點在上沿。
  • column-reverse:主軸爲垂直方向,起點在下沿。

下圖分別對應
在這裏插入圖片描述

2. flex-wrap屬性定義如果一條軸線排不下,如何換行。

.box{
  flex-wrap: nowrap | wrap | wrap-reverse;
}
  • nowrap(默認):不換行。
    在這裏插入圖片描述
  • wrap:換行,第一行在上方。
    在這裏插入圖片描述
  • wrap-reverse:換行,第一行在下方。
    在這裏插入圖片描述

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

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

4. justify-content屬性定義了項目在主軸上的對齊方式

.box {
  justify-content: flex-start | flex-end | center | space-between | space-around;
}
  • flex-start(默認值):左對齊
  • flex-end:右對齊
  • center: 居中
  • space-between:兩端對齊,項目之間的間隔都相等。
  • space-around:每個項目兩側的間隔相等。所以,項目之間的間隔比項目與邊框的間隔大一倍。
  • space-evenly:均勻分佈
    在這裏插入圖片描述

5. align-items屬性定義項目在交叉軸上如何對齊

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

在這裏插入圖片描述

6. align-content屬性定義了多根軸線的對齊方式。如果項目只有一根軸線,該屬性不起作用。

.box {
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
  • flex-start:與交叉軸的起點對齊。
  • flex-end:與交叉軸的終點對齊。
  • center:與交叉軸的中點對齊。
  • space-between:與交叉軸兩端對齊,軸線之間的間隔平均分佈。
  • space-around:每根軸線兩側的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
  • stretch(默認值):軸線佔滿整個交叉軸。
    在這裏插入圖片描述

四、項目的屬性

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

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

2. flex-grow屬性定義項目的放大比例,默認爲0,即如果存在剩餘空間,也不放大

在這裏插入圖片描述
如果所有項目的flex-grow屬性都爲1,則它們將等分剩餘空間(如果有的話)。如果一個項目的flex-grow屬性爲2,其他項目都爲1,則前者佔據的剩餘空間將比其他項多一倍。

3. flex-shrink屬性定義了項目的縮小比例,默認爲1,即如果空間不足,該項目將縮小。

在這裏插入圖片描述
如果所有項目的flex-shrink屬性都爲1,當空間不足時,都將等比例縮小。如果一個項目的flex-shrink屬性爲0,其他項目都爲1,則空間不足時,前者不縮小。
負值對該屬性無效。

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

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

它可以設爲跟width或height屬性一樣的值(比如350px),則項目將佔據固定空間

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

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

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

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

在這裏插入圖片描述

本文來自阮一峯:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

四、flex實例運用

1. 單個元素水平居中

<style> *{padding: 0;margin: 0;} .box{ border: 1px solid black; display: flex; justify-content: center; } .box1{ width: 100px; height: 100px; background-color: darkcyan; } </style>
</head>
<body>
    <div class="box">
        <div class="box1">1</div>
    </div>
</body>

在這裏插入圖片描述

1. 多個元素水平居中

<style> *{padding: 0;margin: 0;} .box{ border: 1px solid black; display: flex; justify-content: center; } .box1{ width: 100px; height: 100px; background-color: darkcyan; border: 1px solid yellow; } </style>
</head>
<body>
    <div class="box">
        <div class="box1">1</div>
        <div class="box1">2</div>
        <div class="box1">3</div>
    </div>

在這裏插入圖片描述

3. 多個元素水平分佈

<style> *{padding: 0;margin: 0;} .box{ border: 1px solid black; display: flex; justify-content: space-between; } .box1{ width: 100px; height: 100px; background-color: darkcyan; border: 1px solid yellow; } </style>
</head>
<body>
    <div class="box">
        <div class="box1">1</div>
        <div class="box1">2</div>
        <div class="box1">3</div>
    </div>
  1. justify-content: center;
    在這裏插入圖片描述

  2. justify-content: flex-start;在這裏插入圖片描述

  3. justify-content: flex-end;
    在這裏插入圖片描述

  4. justify-content: space-between;
    在這裏插入圖片描述

  5. justify-content: space-around;
    兩個相鄰間隔是自己的兩倍
    在這裏插入圖片描述

  6. justify-content: space-evenly;
    均勻分佈
    在這裏插入圖片描述

4. 移動主軸的情況下水平居中

爲父級元素設置高度

  1. align-items: center
<style> *{padding: 0;margin: 0;} .box{ border: 1px solid black; display: flex; justify-content: space-evenly; align-items: center; height: 500px; } .box1{ width: 100px; height: 100px; background-color: darkcyan; border: 1px solid yellow; } </style>
</head>
<body>
    <div class="box">
        <div class="box1">1</div>
        <div class="box1">2</div>
        <div class="box1">3</div>
    </div>

在這裏插入圖片描述

  1. align-items: flex-end;
    在這裏插入圖片描述
  2. align-items: flex-start;在這裏插入圖片描述

5. 垂直居中

<style> *{padding: 0;margin: 0;} .box{ border: 1px solid black; display: flex; align-items: center; height: 500px; flex-direction: column; } .box1{ width: 100px; height: 100px; background-color: darkcyan; border: 1px solid yellow; } </style>
</head>
<body>
    <div class="box">
        <div class="box1">1</div>
        <div class="box1">2</div>
        <div class="box1">3</div>
    </div>
</body>

在這裏插入圖片描述

1. 改變主軸方向
  1. align-items: flex-end;
    在這裏插入圖片描述
  2. align-items: flex-start;
    在這裏插入圖片描述
2. 設置剩餘空間分配
  1. justify-content: center;
.box{
        border: 1px solid black;
        display: flex;
        align-items: center;
        height: 500px;
        flex-direction: column;
        justify-content: center;
    }

在這裏插入圖片描述
2. justify-content:space-around;
在這裏插入圖片描述
3. justify-content:space-between;
在這裏插入圖片描述
4. justify-content:space-evenly;
在這裏插入圖片描述
5. justify-content:flex-start;
在這裏插入圖片描述
6. justify-content:flex-end;
在這裏插入圖片描述

使用flex設置多個元素垂直居中的時候爲

  1. 設置父元素爲:display:flex
  2. 設置主軸的方向:flex-direction: column;
  3. 設置主軸位置:align-items: center;(左中右)
  4. 如果想要調整剩餘空間則使用:justify-content: center;

使用flex 設置多個元素水平居中

  1. 設置設置主軸的方向:flex-direction: row或者不設置flex-direction(默認爲row)
  2. 設置剩餘空間分佈:justify-content: center;
  3. 如果需要設置主軸位置可以設置:align-items: center/flex-end/flex-start(上中下)