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

效果图

代码

html 复制代码
<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>

方法二

css 复制代码
.item {
    /* flex: 0 0 calc((100% - 10px)/3); */
    width: calc((100% - 10px) / 3);
    height: 120px;
    background-color: pink;
    /* 间隙为5px */
    margin: 0 5px 5px 0;
}
相关推荐
yuanyxh2 小时前
Mac 软件推荐
前端·javascript·程序员
万少2 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
某人辛木3 小时前
Web自动化测试
前端·python·pycharm·pytest
Kagol3 小时前
Superpowers GSD gstack AgentSkills深度测评
前端·人工智能
excel4 小时前
JavaScript 字符串与模板字面量:从表象到本质理解
前端
京东云开发者4 小时前
当AI成为导演-如何用AI创作动漫短剧
前端
李白的天不白5 小时前
使用 SmartAdmin 进行前后端开发
java·前端
乘风gg5 小时前
🤡PUA AI Coding 工具 的 10 条终极语录
前端·ai编程·claude
学Linux的语莫5 小时前
Vue 3 入门教程
前端·javascript·vue.js
怕浪猫6 小时前
第一章、Chrome DevTools Protocol (CDP) 详解
前端·javascript·chrome