flex布局常用用法

1.flex布局控制子元素的边距

sql 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Flex 三栏布局</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .flex-container {
      display: flex;
      width: 100%;
      min-height: 100vh;
      background-color: #f5f5f5;
      align-items: center;
      padding: 0; /* 确保无内边距干扰 */
    }

    .item {
      width: 80px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: bold;
      color: white;
    }

    .item1 {
      background-color: #4CAF50;
      margin-left: 10px;   /* 距离容器左边 10px */
    }

    .item2 {
      background-color: #2196F3;
      margin-left: 20px;   /* 与 item1 间隔 20px */
    }

    .item3 {
      background-color: #FF9800;
      margin-right: 10px;  /* 距离容器右边 10px */
      margin-left: auto;  /* 关键自适应边距*/
    }
  </style>
</head>
<body>
  <div class="flex-container">
    <div class="item item1">1</div>
    <div class="item item2">2</div>
    <div class="item item3">3</div>
  </div>
</body>
</html>

2.使用gap控制子元素中间边距

sql 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>3列等宽 Flex 布局</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .container {
      display: flex;
      flex-wrap: wrap;
      gap: 10px; /* 水平和垂直间距均为 10px */
      padding: 20px;
      max-width: 1200px;
      margin: 0 auto;
    }

    .item {
      /* 关键:3列布局,2个间隙 × 10px = 20px */
      flex: 1 1 calc((100% - 20px) / 3);
      min-width: 0; /* 防止长内容撑破布局 */

      background-color: #42b883;
      color: white;
      padding: 20px;
      text-align: center;
      border-radius: 8px;
      font-family: Arial, sans-serif;
    }
  </style>
</head>
<body>
  <div class="container" id="container">
    <!-- 动态生成 8 个 item -->
  </div>

  <script>
    // 使用原生 JS 动态创建 8 个 item(模拟 Vue 的 v-for)
    const container = document.getElementById('container');
    for (let i = 1; i <= 8; i++) {
      const item = document.createElement('div');
      item.className = 'item';
      item.textContent = `Item ${i}`;
      container.appendChild(item);
    }
  </script>
</body>
</html>
相关推荐
陈随易12 分钟前
一个网吧网管,做了个网页,卖了 5000 万
前端·后端·程序员
jktantan9 小时前
Vue keep-alive 切标签后滚动位置回顶?因为 onDeactivated 里读到的 scrollTop 是 0
前端·javascript·vue.js
2601_9628857210 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
excel10 小时前
encodeURI 和 encodeURIComponent 有什么区别?
前端
前端精髓10 小时前
调用openrouter的免费API模型
java·前端·javascript
赛博守夜人10 小时前
AI安全攻防与治理之十四:AI 安全测试不能脱离传统安全,Web、API、移动端与模型攻击面的组合拳
前端·人工智能·安全
开开心心_Every10 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
networknomad11 小时前
OpenReplay:自己搭的会话回放工具,给前端排错用的
前端·其他
缓慢更新12 小时前
AI 辅助生成单测:从接口定义到用例的流水线
java·服务器·前端·网络·数据库·人工智能
陈随易12 小时前
AI 运维服务器,安全又可靠的方法
前端·后端·程序员