CSS3——flex彈性佈局

容器的屬性

以下6個屬性設置在容器上,就是說給flex container添加這些屬性,來佈局flex item。

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

flex-direction屬性:

決定主軸的方向(即項目的排列方向)。

屬性值:

row(默認值):主軸爲水平方向,起點在左端。


row-reverse:主軸爲水平方向,起點在右端。


column:主軸爲垂直方向,起點在上沿。

column-reverse:主軸爲垂直方向,起點在下沿。

column:                                       column-reverse: 

flex-wrap屬性

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .flexBox{
            width: 1200px;
            height: 400px;
            margin: 20px auto;
            background-color: #ddd;
        }
        .flexBox ul{
            list-style: none;
            display: flex;
            flex-wrap: wrap;    //設置換行方式
        }
        .flexBox li{
            width: 150px;
            height: 150px;
            text-align: center;
            line-height: 150px;
            margin: 20px;
            background-color: pink;
        }
    </style>
</head>
<body>
<div class="flexBox">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
    </ul>
</div>
</body>
</html>

屬性值:

flex-wrap: nowrap;    不換行


flex-wrap: wrap;    換行,第一行在上面


flex-wrap: wrap-reverse;    換行,第一行在下面


flex-flow屬性

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

例如:

flex-flow: row-reverse wrap;

等價於:

flex-direction: row-reverse;
flex-wrap: wrap;


justify-content屬性

justify-content屬性定義了項目在主軸上的對齊方式。具體對齊方式與軸的方向有關。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .flexBox{
            width: 1200px;
            height: 400px;
            margin: 20px auto;
            background-color: #ddd;
        }
        .flexBox ul{
            list-style: none;
            display: flex;
            justify-content: center; //主軸上的對齊方式
        }
        .flexBox li{
            width: 150px;
            height: 150px;
            text-align: center;
            line-height: 150px;
            margin: 20px;  //設置了margin
            background-color: pink;
        }
    </style>
</head>
<body>
<div class="flexBox">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</div>
</body>
</html>

下面假設主軸爲從左到右。

屬性值:

flex-start(默認值):左對齊


flex-end:右對齊


center: 居中(水平居中)


space-between:兩端對齊,項目之間的間隔都相等。


space-around:每個項目兩側的間隔相等。所以,項目之間的間隔比項目與邊框的間隔大一倍。


align-items屬性

定義項目在交叉軸(cross axis)上如何對齊。具體的對齊方式與交叉的方向有關。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .flexBox{
            width: 1200px;
            height: 400px;
            margin: 20px auto;
            background-color: #ddd;
        }
        .flexBox ul{
            list-style: none;
            display: flex;
            align-items: baseline; //設置交叉軸對齊方式
        }
        .flexBox li{
            margin: 20px;
            background-color: pink;
        }
        .flexBox li:first-child{
            width: 150px;
            height: 200px;
            text-align: center;
            line-height: 100px;
        }
        .flexBox li:nth-child(2){
            width: 150px;
            height: 250px;
            text-align: center;
            line-height: 120px;
        }
        .flexBox li:nth-child(3){
            width: 150px;
            height: 150px;
            text-align: center;
            line-height: 70px;
        }
        .flexBox li:last-child{
            width: 150px;
            height: 300px;
            text-align: center;
            line-height: 150px;
        }
    </style>
</head>
<body>
<div class="flexBox">
    <ul>
        <li>1<p>1</p></li>
        <li>2<p>2</p></li>
        <li>3<p>3</p></li>
        <li>4<p>4</p></li>
    </ul>
</div>
</body>
</html>

下面假設交叉從上到下。

屬性值:

flex-start:交叉軸的起點對齊。


flex-end:交叉軸的終點對齊。


center:交叉軸的中點對齊。


baseline: 項目的第一行文字的基線對齊。


stretch(默認值):如果項目未設置高度或設爲auto,將佔滿整個容器的高度。

align-content屬性

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

屬性值:

flex-start:與交叉軸的起點對齊。

flex-end:與交叉軸的終點對齊。

center:與交叉軸的中點對齊。

space-between:與交叉軸兩端對齊,軸線之間的間隔平均分佈。

space-around:每根軸線兩側的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。

stretch(默認值):軸線佔滿整個交叉軸。

項目的屬性

以下6個屬性設置在項目上就是說給flex item添加這些屬性,來佈局flex item。

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

order屬性

order屬性定義項目的排列順序。數值越小,排列越靠前,默認爲0。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .flexBox{
            width: 1200px;
            height: 400px;
            margin: 20px auto;
            background-color: #ddd;
        }
        .flexBox ul{
            list-style: none;
            display: flex;
        }
        .flexBox li{
            margin: 20px;
            background-color: pink;
        }
        .flexBox li:first-child{
            width: 150px;
            height: 200px;
            text-align: center;
            line-height: 200px;
            order: 4;
        }
        .flexBox li:nth-child(2){
            width: 150px;
            height: 250px;
            text-align: center;
            line-height: 250px;
            order: 2;
        }
        .flexBox li:nth-child(3){
            width: 150px;
            height: 150px;
            text-align: center;
            line-height: 150px;
            order: 3;
        }
        .flexBox li:last-child{
            width: 150px;
            height: 300px;
            text-align: center;
            line-height: 300px;
            order: 1;
        }
    </style>
</head>
<body>
<div class="flexBox">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</div>
</body>
</html>


flex-grow屬性

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

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

 flex-shrink屬性

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

如果所有項目的flex-shrink屬性都爲1,當空間不足時,都將等比例縮小。如果一個項目的flex-shrink屬性爲0,其他項目都爲1,則空間不足時,前者不縮小。

負值對該屬性無效。

 flex-basis屬性

flex-basis屬性定義了在分配多餘空間之前,項目佔據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多餘空間。它的默認值爲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。

align-self: auto | flex-start | flex-end | center | baseline | stretch;

該屬性可能取6個值,除了auto,其他都與align-items屬性完全一致。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章