Flex布局中元素换行设置间距问题

要生成的效果图如下:

 解决办法

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);
        }

posted @   Tricia11  阅读(203)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示