flex多行多列布局小技巧

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>flex</title>
  </head>
  <style>
    .parent {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: space-between;
      align-content: space-evenly;
      /* align-items: center; */
      width: 550px;
      height: 550px;
      /* height: 500px; */
      background-color: aqua;
    }
    .children {
      flex-shrink: 0;
      flex-grow: 0;
      width: 100px;
      height: 100px;
      background-color: burlywood;
    }
    .empty {
      width: 100px;
      /* height: 0px; */
      opacity: 0;
    }
  </style>
  <body>
    <div class="parent">
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="children"></div>
      <div class="empty"></div>
      <div class="empty"></div>
      <div class="empty"></div>
      <div class="empty"></div>
      <div class="empty"></div>
    </div>
  </body>
</html>

开发中经常会遇到多行多列的情况,一般都是要求每行都是两头在最边上,中间均匀分布的情况。这种情况使用flex的一般都会用到justify-content: space-between;,但是这种不适合用在多列的情况。

解决这个问题也很简单就是补位就行了补了几个<div class="empty"></div>这样就可以拉。注意要把这个补位的样式宽度要设置一样的,并且高度设置0或者设置透明。

这种多行多列的还可以用grid布局。我这个只是提供一个思路。

相关推荐
默_笙2 分钟前
🏝 Docker 就是"房地产开发":从施工图纸到小区物业的容器化指南
前端·javascript
kyriewen5 分钟前
我手写了个极简版 React Router——才搞懂 v6 为什么砍了这么多 API
前端·javascript·程序员
a11177611 分钟前
Shirone 二次元博客主题 开源
前端·开源
IT_陈寒23 分钟前
Python的切片赋值把我坑惨了,这不是bug是特性
前端·人工智能·后端
掘金酱27 分钟前
【社区公告】致每一位掘友:关于这次调整, 想再说几句
前端·人工智能
CoderLiu28 分钟前
程序化工具调用(PTC)与动态工作流引擎:深入大模型工具调用的架构演进与实践
前端·人工智能·后端
码农胖大海39 分钟前
我的第一个产品,只有一段提示词
前端·ai编程·产品
邋遢道1 小时前
# 企业级 Agent 从 0 到 1(二):技术选型与最小骨架
前端·chrome
计算机魔术师1 小时前
我看了 OpenAI 首席科学家的最新长文,把原来的认知推翻了一遍
前端
乱码三千2 小时前
使用ComfyUI+MinMax-H3音视频模型生成视频
前端·后端·github