flex and margin, complement each other

flex page layout to facilitate the construction of a great extent, which follows several layout, in order not to add additional nested tags, separate flex layout would also need to deal with a number of other settings, such as with the use of the dummy element , the next time will be described, with the use of flex here with margin, and used their own personal opinion is the most convenient method.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>flex margin</title>
    <style>
        .wrapper {
            display: flex;
            margin: 50px 0;
        }
        .wrapper > div {
            border: 1px solid purple;
            background-color: pink;
            width: 200px;
        }
        .wrapper-1 .item-3 {
            margin-left: auto;
        }
        /* .wrapper-2 .item-2 {
            margin-left: auto;
        } */
        /* 下面三行代码与上面注释掉的三行代码是同样效果的 */
        .wrapper-2 .item-1 {
            margin-right: auto;
        }

        .wrapper-3 > div {
            margin: auto;/*效果同 justify-content:space-around*/
        }
        .wrapper-4 > .item-3 {
            margin: auto;
        }
        .wrapper-5 > .item-2 {
            margin-left: auto;
        }
        .wrapper-5 > .item-3 {
            margin-right: auto;
        }
    </style>
</head>
<body>
    <div class="wrapper wrapper-1">
        <div class="item-1">1</div>
        <div class="item-2">2</div>
        <div class="item-3">3</div>
    </div>
    <div class="wrapper wrapper-2">
        <div class="item-1">1</div>
        <div class="item-2">2</div>
        <div class="item-3">3</div>
    </div>
    <div class="wrapper wrapper-3">
        <div class="item-1">1</div>
        <div class="item-2">2</div>
        <div class="item-3">3</div>
        <div class="item-4">4</div>
        <div class="item-5">5</div>
    </div>
    <div class="wrapper wrapper-4">
        <div class="item-1">1</div>
        <div class="item-2">2</div>
        <div class="item-3">3</div>
        <div class="item-4">4</div>
        <div class="item-5">5</div>
    </div>
    <div class="wrapper wrapper-5">
        <div class="item-1">1</div>
        <div class="item-2">2</div>
        <div class="item-3">3</div>
        <div class="item-4">4</div>
        <div class="item-5">5</div>
    </div>
</body>
</html>

Reference Links: You may not know the 49 CSS knowledge

Guess you like

Origin www.cnblogs.com/chaoyueqi/p/11364268.html
Recommended