在一行中实现每个盒子间隔相等

达成效果:
在这里插入图片描述

1. 使用justify-content: space-evenly;

<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>flex evenly</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .wrap {
      display: flex;
      justify-content: space-evenly;
      width: 700px;
      height: 400px;
      margin: 100px auto;
      background-color: #ccc;
    }
    
    .item {
      width: 140px;
      height: 120px;
      background-color: #368;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</body>
</html>

缺点:苹果的兼容性不好,推荐以下方法

2. space-between配合before+after实现space-evenly效果

<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>flex evenly兼容</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .wrap {
      display: flex;
      justify-content: space-between;
      width: 600px;
      height: 400px;
      margin: 100px auto;
      background-color: #ccc;
    }

    .wrap:before,
    .wrap:after {
     /* 用空内容来占位达到效果 */
      content: ''; 
    }

    .item {
      width: 140px;
      height: 120px;
      background-color: #368;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</body>
</html>

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 16:48:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 16:48:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 16:48:03       57 阅读
  4. Python语言-面向对象

    2024-07-09 16:48:03       68 阅读

热门阅读

  1. TCP协议是安全的吗?

    2024-07-09 16:48:03       50 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-07-09 16:48:03       39 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-07-09 16:48:03       48 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-07-09 16:48:03       55 阅读
  5. 金融行业:银行的三大类业务

    2024-07-09 16:48:03       44 阅读
  6. Vim和Nano简介

    2024-07-09 16:48:03       32 阅读
  7. 产品经理基础入门

    2024-07-09 16:48:03       37 阅读
  8. C# 用RFC的方式调用SAP接口

    2024-07-09 16:48:03       38 阅读