flex布局,换行的元素上下设置间距

要生成的效果图如下:
在这里插入图片描述

 display:flex
 flex-direction: row;
 flex-wrap: wrap;

当我们使用弹性盒子布局后,默认元素是没有外边距的,紧挨着样式就有点丑,如果想使换行后,元素的外边距有个距离,可以用如下方法解决

解决办法
1.父元素定高的情况下,直接使用 align-content: space-between;

       ul{
   
            list-style: none;
            display: flex;
            height: 614px;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: space-between;
            align-content: space-between;
        }
        ul li{
   
            width: 234px;
            height: 300px;
            background-color: rgb(255, 2, 192);
        }

2.父元素不定高的情况下
1)设置需要更改间距的元素(li)的margin-bottom:14px,然后用父容器(ul)的margin-bottom: -14px;来抵消。

       ul{
   
            list-style: none;
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            margin-bottom: -14px;
            justify-content: space-between;
            align-content: space-between;
        }
        li{
   
            margin-bottom: 14px;
        }
   ul li{
   
            display: flex;
            width: 234px;
            height: 300px;
            background-color: rgb(255, 2, 192);
        }

2) 设置需要更改间距的元素(li)的margin-bottom:14px;然后使用结构伪类选择器设置最后的几个元素margin-bottom: 0 ;

      ul{
   
            list-style: none;
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: space-between;
            align-content: space-between;
        }
        li{
   
            margin-bottom: 14px;
        }
       li:nth-child(n+5){
   
            margin-top: 0;
        }
        ul li{
   
            display: flex;
            width: 234px;
            height: 300px;
            background-color: rgb(255, 2, 192);
        }

最近更新

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

    2023-12-13 08:58:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-13 08:58:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-13 08:58:04       82 阅读
  4. Python语言-面向对象

    2023-12-13 08:58:04       91 阅读

热门阅读

  1. Tomcat

    2023-12-13 08:58:04       50 阅读
  2. css表单

    css表单

    2023-12-13 08:58:04      58 阅读
  3. Linux下的网络服务

    2023-12-13 08:58:04       53 阅读
  4. 为 PHP 引入 Python 生态的经验分享

    2023-12-13 08:58:04       63 阅读
  5. 【MODBUS】libmodbus库写一个Modbus TCP客户端

    2023-12-13 08:58:04       60 阅读
  6. 常用的线程锁

    2023-12-13 08:58:04       65 阅读
  7. 【C#】Microsoft C# 视频学习总结

    2023-12-13 08:58:04       57 阅读