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>
相关推荐
GISer_Jing7 分钟前
前端面试通关:Cesium+Three+React优化+TypeScript实战+ECharts性能方案
前端·react.js·面试
落霞的思绪1 小时前
CSS复习
前端·css
咖啡の猫3 小时前
Shell脚本-for循环应用案例
前端·chrome
百万蹄蹄向前冲5 小时前
Trae分析Phaser.js游戏《洋葱头捡星星》
前端·游戏开发·trae
朝阳5816 小时前
在浏览器端使用 xml2js 遇到的报错及解决方法
前端
GIS之路6 小时前
GeoTools 读取影像元数据
前端
ssshooter7 小时前
VSCode 自带的 TS 版本可能跟项目TS 版本不一样
前端·面试·typescript
Jerry7 小时前
Jetpack Compose 中的状态
前端
dae bal8 小时前
关于RSA和AES加密
前端·vue.js
柳杉8 小时前
使用three.js搭建3d隧道监测-2
前端·javascript·数据可视化