flex+margin布局方法

元素上下左右居中
方式:【具体代码1】

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100%;
            height: 100vh;
        }
        .box1{
            width: 100%;
            height: 30vh;
            background-color: blue;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .content1{
            width: 50%;
            height: 50%;
            background-color: lightgray;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1">
            <div class="content1"></div>
        </div>
    </div>
</body>
</html>

【具体代码2】:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100%;
            height: 100vh;
        }
        .box1{
            width: 100%;
            height: 30vh;
            background-color: blue;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .content1{
            width: 50%;
            height: 50%;
            background-color: lightgray;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1">
            <div class="content1"></div>
        </div>
    </div>
</body>
</html>

绿色的盒子到右边
【具体代码】

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100%;
            height: 100vh;
        }
        .box1{
            width: 100%;
            height: 30vh;
            background-color: blue;
            display: flex;
        }
        .content1{
            width: 20%;
            height: 30%;
            background-color: lightgray;
        }
        .content2{
            width: 20%;
            height: 30%;
            background-color: lightgoldenrodyellow;
        }
        .content3{
            width: 20%;
            height: 30%;
            background-color: pink;
            margin-left: auto;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1">
            <div class="content1"></div>
            <div class="content2"></div>
            <div class="content3"></div>
        </div>
    </div>
</body>
</html>

右边两个移动到右边
【代码具体实现】:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100%;
            height: 100vh;
        }
        .box1{
            width: 100%;
            height: 30vh;
            background-color: blue;
            display: flex;
        }
        .content1{
            width: 20%;
            height: 30%;
            background-color: lightgray;
        }
        .content2{
            width: 20%;
            height: 30%;
            background-color: lightgoldenrodyellow;
        }
        .content3{
            width: 20%;
            height: 30%;
            background-color: pink;
            margin-left: auto;
        }
        .content4{
            width: 20%;
            height: 30%;
            background-color: purple;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1">
            <div class="content1"></div>
            <div class="content2"></div>
            <div class="content3"></div>
            <div class="content4"></div>
        </div>
    </div>
</body>
</html>

在这里插入图片描述

【代码实现】:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 100%;
            height: 100vh;
        }
        .box1{
            width: 100%;
            height: 30vh;
            background-color: blue;
            display: flex;
        }
        .content1{
            width: 20%;
            height: 30%;
            background-color: lightgray;
        }
        .content2{
            width: 20%;
            height: 30%;
            background-color: lightgoldenrodyellow;
        }
        .content3{
            width: 20%;
            height: 30%;
            background-color: pink;
            margin-left: auto;
        }
        .content4{
            width: 20%;
            height: 30%;
            background-color: purple;
        }
        .content5{
            width: 20%;
            height: 30%;
            background-color: lightgray;
        }
        .content6{
            width: 20%;
            height: 30%;
            background-color: lightgoldenrodyellow;
        }
        .content7{
            width: 20%;
            height: 30%;
            background-color: pink;
        }
        .content8{
            width: 20%;
            height: 30%;
            background-color: purple;
        }
        .content9{
            width: 20%;
            height: 30%;
            background-color: lightgray;
        }
        .content10{
            width: 20%;
            height: 30%;
            background-color: lightgoldenrodyellow;
        }
        .content11{
            width: 20%;
            height: 30%;
            background-color: pink;
        }
        .content12{
            width: 20%;
            height: 30%;
            background-color: purple;
        }
        .content13{
            width: 20%;
            height: 30%;
            background-color: lightgray;
        }
        .content14{
            width: 20%;
            height: 30%;
            background-color: lightgoldenrodyellow;
        }
        .content15{
            width: 20%;
            height: 30%;
            background-color: pink;
        }
        .content16{
            width: 20%;
            height: 30%;
            background-color: purple;
        }
        #item{
            /* --n:7;
            --$ap: calc((100%-50px*7) / 7/2);
            margin: 18px var(--$ap); */
            margin: 10px calc(100% - 50px*7)/7/2;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1">
            <div class="content1" id="item"></div>
            <div class="content2" id="item"></div>
            <div class="content3" id="item"></div>
            <div class="content4" id="item"></div>
            <div class="content4" id="item"></div>
            <div class="content5" id="item"></div>
            <div class="content6" id="item"></div>
            <div class="content7" id="item"></div>
            <div class="content8" id="item"></div>
            <div class="content9" id="item"></div>
            <div class="content10" id="item"></div>
            <div class="content11" id="item"></div>
            <div class="content12" id="item"></div>
            <div class="content13" id="item"></div>
            <div class="content14" id="item"></div>
            <div class="content15" id="item"></div>
            <div class="content16" id="item"></div>
        </div>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clover‘s Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值