如何添加A“线路断路”到flexbox,而不会引起双重差距?
我有一个使用gap
进行间距的Flexbox,该flexbox需要支持“线路断裂”,这会导致后续的Flex项目跳到下一行。
我看到的所有示例建议使用具有flex-basis的新flex项目:100%
(请参阅下面的示例),但是这将导致行之间的双尺寸差距,与其他不同由普通项目包装引起的行。

示例:
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="break"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
.flex {
display: flex;
flex-flow: row wrap;
gap: 10px;
}
.item {
width: 100px;
height: 100px;
background-color: orange;
border-radius: 5px;
}
.break {
flex-basis: 100%;
}
我尝试在断路项目中添加负面/底部边距,但是由于某种原因没有任何效果。
有没有一种方法可以增加与其他线路相同的尺寸间隙的线路断路?
(在这种情况下,交换GAP
在单个项目上的边距不是一个选择。)
I have a flexbox that uses gap
for spacing, which needs to support “line breaks” that cause subsequent flex items to jump down to the next row.
All examples I’ve seen suggest doing this with a new flex item that has flex-basis: 100%
(see example below), however that will cause a double-sized gap between the rows, unlike other rows caused by normal item wrapping.
Example:
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="break"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
.flex {
display: flex;
flex-flow: row wrap;
gap: 10px;
}
.item {
width: 100px;
height: 100px;
background-color: orange;
border-radius: 5px;
}
.break {
flex-basis: 100%;
}
I’ve tried adding negative top/bottom margins to the line break item, however that doesn’t have any effect for some reason.
Is there a way to add a line break with the same size gap as other lines?
(Swapping gap
for margins on the individual items is not an option in this case.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来这是当前使用
GAP
当前无法完成的,因此您必须使用Flex项目上的底部边距建立差距。Appears this just can’t be done currently using
gap
, so you have to establish the gap using bottom margins on the flex items instead.GAP
属性是行 - GAP
和column-gap
的速记。这意味着您可以为间隙提供两个值,从而使行和列距离均衡:The
gap
property is shorthand forrow-gap
andcolumn-gap
. This means that you can provide two values for gap, thus equalizing row and column distance:我可以找到的最佳方法是仅在流动方向上使用
GAP
(在这种情况下为水平),然后使用垂直边距来控制垂直间距。这比使用
gap
更笨拙,但是通过在Flex容器上添加负差(以补偿项目的边距)可以实现所需的效果而没有副作用(例如,顶部和底部不希望的间距):作为旁注,我很惊讶
.break
元素不起作用:我希望您可以通过添加rubgin -top:-10px来解决此问题。
,但这似乎不起作用。编辑:对不起,我错过了最后一行,“ 在这种情况下,在这种情况下,在单个项目上交换差距是一个选择 ”但是,将负距离添加到容器以补偿项目边距的其他步骤使此方法可行。
The best approach I can find for this is to use
gap
only for gaps in the flow direction (horizontal in this case), and use vertical margin to control vertical spacing.This feels clunkier than using
gap
, but by adding negative margin on the flex container (to compensate for the margin on the items) achieves the desired effect without side effects (eg. undesired spacing at the top and bottom):As a side note, I'm surprised that negative margin on the
.break
element doesn't work: I'd expect you could solve this by addingmargin-top: -10px
to it, but that doesn't seem to work.Edit: Sorry, I missed that last line that "Swapping gap for margins on the individual items is not an option in this case" I'll leave this up just in case the additional step of adding negative margin to the container to compensate for the item margin makes this approach feasible though.
使用
gap:10px;
sets10px
gap的垂直和水平差距。您可以使用差距:5px 10px;
垂直差距的5px
和10px
of Horizontal。Using
gap: 10px;
sets10px
of gap both vertically and horizontally. You can usegap: 5px 10px;
for5px
of vertical gap and10px
of horizontal.