完美解决 flex 实现一行三个,显示多行,左对齐

效果图

完美解决 flex 实现一行三个,显示多行,左对齐_第1张图片

代码

<body>
    <section class="content">
        <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 class="item">元素div>
    section>
body>
<style>
    .content {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        background-color: skyblue;
    }
    .item {
        flex: 0 0 calc((100% - 10px)/3);
        height: 120px;
        background-color: pink;
        /* 间隙为5px */
        margin: 0 5px 5px 0;
    }
    .item:nth-child(3n) {
        /* 去除第3n个的margin-right */
        margin-right: 0;
    }
style>

方法二

.item {
    /* flex: 0 0 calc((100% - 10px)/3); */
    width: calc((100% - 10px) / 3);
    height: 120px;
    background-color: pink;
    /* 间隙为5px */
    margin: 0 5px 5px 0;
}

你可能感兴趣的:(css,javascript,css,前端)