900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 【CSS布局】Flex布局中元素换行设置间距问题

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

时间:2021-01-13 03:20:53

相关推荐

【CSS布局】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);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。