完美解决 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;
}
相关推荐
五点六六六5 小时前
你敢信这是非Native页面写出来的渐变效果吗🌝(底层原理解析
前端·javascript·面试
tedcloud1235 小时前
TradingAgents部署教程:打造AI量化分析工作流
服务器·前端·人工智能·系统架构·edge
吃西瓜的年年5 小时前
TypeScript
javascript·ubuntu·typescript
小村儿6 小时前
连载10-Sub-agents 深度解析:从源码理解 Claude Code 的分身术
前端·后端·ai编程
IT_陈寒7 小时前
Vite动态导入把我坑惨了,原来要这样用才对
前端·人工智能·后端
DFT计算杂谈7 小时前
KPROJ编译教程
java·前端·python·算法·conda
觅_7 小时前
前端学习后端的时候 选择一个技术
前端·学习
独泪了无痕7 小时前
CryptoJS:数据安全的JavaScript加密利器
前端·vue.js·node.js
发现一只大呆瓜7 小时前
一文搞懂 Vite 处理CommonJS包、按需编译逻辑及 Rollup 插件兼容规则
前端
Edwardwu7 小时前
写了个y-mxgraph:给 draw.io 接上了 Yjs,顺便解决了部署在 iframe 里的一堆问题
前端·typescript