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布局。我这个只是提供一个思路。

相关推荐
GIS之路2 分钟前
GDAL 实现矢量裁剪
前端·python·信息可视化
是一个Bug6 分钟前
后端开发者视角的前端开发面试题清单(50道)
前端
Amumu121388 分钟前
React面向组件编程
开发语言·前端·javascript
持续升级打怪中29 分钟前
Vue3 中虚拟滚动与分页加载的实现原理与实践
前端·性能优化
GIS之路33 分钟前
GDAL 实现矢量合并
前端
hxjhnct35 分钟前
React useContext的缺陷
前端·react.js·前端框架
前端 贾公子1 小时前
从入门到实践:前端 Monorepo 工程化实战(4)
前端
菩提小狗1 小时前
Sqlmap双击运行脚本,双击直接打开。
前端·笔记·安全·web安全
前端工作日常1 小时前
我学习到的AG-UI的概念
前端
韩师傅1 小时前
前端开发消亡史:AI也无法掩盖没有设计创造力的真相
前端·人工智能·后端