用flex布局实现九宫格

效果如下:

 

代码如下:

<!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>Document</title>

    <style>

        .container{

            width: 350px;

            /* flex布局 */

            display: flex;

            /* 水平对齐方式 */

            justify-content: space-evenly;

            /* 实现换行 */

            flex-wrap: wrap;

            border:2px solid black;

            padding-bottom: 10px;

        }

       .item{

            width: 100px;

            height: 100px;

            background-color: #62e03b;

            margin-top: 10px;

        }

    </style>

</head>

<body>

    <div class="container">

        <div class="item">1</div>

        <div class="item">2</div>

        <div class="item">3</div>

        <div class="item">4</div>

        <div class="item">5</div>

        <div class="item">6</div>

        <div class="item">7</div>

        <div class="item">8</div>

        <div class="item">9</div>

    </div>

</body>

</html>

   扩展:若想让每个格子的内容呈现以下效果,则在每个格子里实现flex布局

.item{
            width: 100px;
            height: 100px;
            background-color: #62e03b;
            margin-top: 10px;
           /* 以下为新加的 */
            display: flex;
            justify-content: center;
            align-items: center;
        }

猜你喜欢

转载自blog.csdn.net/weixin_53141315/article/details/132499136