css里flex+margin布局

css里flex+margin布局

在flex+margin的布局中,margin设置auto会自动将元素剩余的空间用margin填满

居中

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>居中</title>
  <style>
      * {
          margin: 0;
          padding: 0;
      }
      .container {
          height: 500px;
          display: flex;
          border: 2px solid red;
          box-sizing: border-box;
      }
      .item {
          width: 100px;
          height: 100px;
          line-height: 100px;
          text-align: center;
          background-color: #ccc;
      }
      .item {
          margin: auto;
      }
  </style>
</head>
<body>
<div class="container">
  <div class="item">1</div>
</div>
</body>
</html>

两端排列

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>两端排列</title>
  <style>
      * {
          margin: 0;
          padding: 0;
      }
      .container {
          display: flex;
          border: 2px solid red;
          box-sizing: border-box;
      }
      .item {
          width: 100px;
          height: 100px;
          line-height: 100px;
          text-align: center;
          background-color: #ccc;
      }
      .item:nth-child(3) {
          margin-left: auto;
      }
  </style>
</head>
<body>
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>
</body>
</html>

依次排列

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>依次排列</title>
  <style>
      * {
          margin: 0;
          padding: 0;
      }
      .container {
          display: flex;
          flex-wrap: wrap;
          border: 2px solid red;
          box-sizing: border-box;
      }
      .item {
          width: 100px;
          height: 100px;
          line-height: 100px;
          text-align: center;
          background-color: #ccc;
      }
      .container .item {
          --n: 10;
          /* 计算得出左右两边的间距:元素剩余的空间 / 元素个数 / 2 */
          --gap: calc((100% - 100px * var(--n)) / var(--n) / 2);
          margin: 20px var(--gap);
      }
  </style>
</head>
<body>
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
  <div class="item">7</div>
  <div class="item">8</div>
  <div class="item">9</div>
  <div class="item">10</div>
  <div class="item">11</div>
  <div class="item">12</div>
  <div class="item">13</div>
  <div class="item">14</div>
  <div class="item">15</div>
  <div class="item">16</div>
  <div class="item">17</div>
  <div class="item">18</div>
  <div class="item">19</div>
  <div class="item">20</div>
  <div class="item">21</div>
  <div class="item">22</div>
</div>
</body>
</html>
相关推荐
专吃海绵宝宝菠萝屋的派大星1 天前
使用Dify对接自己开发的mcp
java·服务器·前端
爱分享的阿Q1 天前
Rust加WebAssembly前端性能革命实践指南
前端·rust·wasm
蓝黑20201 天前
Vue的 value=“1“ 和 :value=“1“ 有什么区别
前端·javascript·vue
小李子呢02111 天前
前端八股6---v-model双向绑定
前端·javascript·算法
He少年1 天前
【基础知识、Skill、Rules和MCP案例介绍】
java·前端·python
史迪仔01121 天前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
AwesomeCPA1 天前
Miaoduo MCP 使用指南(VDI内网环境)
前端·ui·ai编程
前端大波1 天前
前端面试通关包(2026版,完整版)
前端·面试·职场和发展
qq_433502181 天前
Codex cli 飞书文档创建进阶实用命令 + Skill 创建&使用 小白完整教程
java·前端·飞书
IT_陈寒1 天前
为什么我的Vite热更新老是重新加载整个页面?
前端·人工智能·后端