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

相关推荐
刘发财1 天前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶1 天前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶1 天前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol1 天前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路1 天前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide1 天前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter1 天前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸1 天前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000001 天前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉1 天前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化