彈性佈局

彈性佈局

1、Flex佈局是什麼?

Flex是Flexible Box的縮寫,意爲」彈性佈局」,用來爲盒狀模型提供最大的靈活性。
任何一個容器均可以指定爲Flex佈局。css

2、基本概念

採用Flex佈局的元素,稱爲Flex容器(flex container),簡稱」容器」。它的全部子元素自動成爲容器成員,稱爲Flex項目(flex item),簡稱」項目」。html

代碼以下:

3、容器的屬性

如下6個屬性設置在容器上。
  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

<!DOCTYPE html>佈局

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>彈性佈局</title>
    <style>
     body{
         margin: 0;
         padding: 0;
     }   
     #main
{
    width:50vw;
    height:50vh;
    border:1px solid black;
    display:flex;
    margin: 0 auto;
   
}

     #main div
        {
    flex:1;
    
    }

    
    </style>
</head>
<body>
    <div>
        <div id="main">
            <div style="background:#ff0">1</div>
            <div style="background:#f0f">2</div>
            <div style="background:#f00">3</div>
            <div style="background:#0ff">4</div>
            <div style="background:#0f0">5</div>
        </div>
    </div>    
</body>
</html>

效果圖:

圖片描述

提示:在css樣式裏面加上flex-direction:column;就會變成另一種佈局效果。以下圖:

圖片描述

相關文章
相關標籤/搜索