flex布局常用场景

目前常用四种场景:
1,定义好宽高的子元素,左排列
2,子元素,左排列,且均分父元素空间
3,多个子元素,分布在父元素的两边
4,多个元素居于正中间

demo如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex常用场景</title>
    <style>
        body >div{
            height: 260px;
            overflow: hidden;
            margin-bottom: 20px;
            background-color: rgba(0, 0, 0, 0.1);
        }

        .one{
            width: 600px;
            display: flex;
            flex-wrap: wrap;
        }
        .one .item{
            width: 160px;
            height: 100px;
            background-color: plum;
            margin-right: 10px;

        }

        .two{
            width: 600px;
            display: flex;
        }

        .two .item{
            flex: 1;
            margin-right: 10px;
            background-color: palevioletred;
        }

        .three{
            width: 600px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
        }

        .three .item{
            width: 45%;
            height: 50px;
        }

        .three .item:nth-child(even){
            background-color: pink;
        }

        .three .item:nth-child(odd){
            background-color: blueviolet;
        }
        

        .four{
            width: 600px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }

        .four .item{
            font-size: 15px;
            color: cornflowerblue;
        }
    </style>
</head>
<body>
    <!-- 定义好宽高的子元素,左排列 -->
    <div class="one">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
    <!-- 子元素,左排列,且均分父元素空间 -->
    <div class="two">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>

    <!-- 多个子元素,分布在父元素的两边 -->
    <div class="three">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
    <!-- 多个元素居于正中间 -->
    <div class="four">
        <div class="item">hello html</div>
        <div class="item">hello css</div>
        <div class="item">hello javaScript</div>
    </div>
</body>
</html>

版权声明:本文为qq_36238674原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。