如何添加A“线路断路”到flexbox,而不会引起双重差距?

发布于 2025-02-02 18:21:19 字数 1137 浏览 2 评论 0原文

我有一个使用gap进行间距的Flexbox,该flexbox需要支持“线路断裂”,这会导致后续的Flex项目跳到下一行。

我看到的所有示例建议使用具有flex-basis的新flex项目:100%(请参阅下面的示例),但是这将导致行之间的双尺寸差距,与其他不同由普通项目包装引起的行。

An demonstration of two flexbox rows, each with three items. The rows have twice the gap between them as the columns.

示例:

<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.

An demonstration of two flexbox rows, each with three items. The rows have twice the gap between them as the columns.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

做个少女永远怀春 2025-02-09 18:21:19

看起来这是当前使用GAP当前无法完成的,因此您必须使用Flex项目上的底部边距建立差距。

.flex {
  display: flex;
  flex-flow: row wrap;
  column-gap: 10px;
  margin-bottom: -10px;
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
  margin-bottom: 10px;
}

.break {
  flex-basis: 100%;
}

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.

.flex {
  display: flex;
  flex-flow: row wrap;
  column-gap: 10px;
  margin-bottom: -10px;
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
  margin-bottom: 10px;
}

.break {
  flex-basis: 100%;
}
兮子 2025-02-09 18:21:19

GAP属性是行 - GAPcolumn-gap的速记。这意味着您可以为间隙提供两个值,从而使行和列距离均衡:

gap: 5px 10px;  

.flex {
  display: flex;
  flex-flow: row wrap;
  gap: 5px 10px;
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
}

.break {
  flex-basis: 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>

The gap property is shorthand for row-gap and column-gap. This means that you can provide two values for gap, thus equalizing row and column distance:

gap: 5px 10px;  

.flex {
  display: flex;
  flex-flow: row wrap;
  gap: 5px 10px;
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
}

.break {
  flex-basis: 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>

鹿港巷口少年归 2025-02-09 18:21:19

我可以找到的最佳方法是仅在流动方向上使用GAP(在这种情况下为水平),然后使用垂直边距来控制垂直间距。

这比使用gap更笨拙,但是通过在Flex容器上添加负差(以补偿项目的边距)可以实现所需的效果而没有副作用(例如,顶部和底部不希望的间距):

.flex {
  display: flex;
  flex-flow: row wrap;
  gap: 0 10px;    /* Only use gap for horizontal spacing */
  margin: -5px 0; /* Compensate for item margin */
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
  margin: 5px 0; /* Add (0.5 * gap) vertical margin to flex items */
}

作为旁注,我很惊讶.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):

.flex {
  display: flex;
  flex-flow: row wrap;
  gap: 0 10px;    /* Only use gap for horizontal spacing */
  margin: -5px 0; /* Compensate for item margin */
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
  margin: 5px 0; /* Add (0.5 * gap) vertical margin to flex items */
}

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 adding margin-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.

可爱咩 2025-02-09 18:21:19

使用gap:10px; sets 10px gap的垂直和水平差距。您可以使用差距:5px 10px; 垂直差距的5px10px of Horizo​​ntal。

.flex {
  display: flex;
  flex-flow: row wrap;
  gap: 5px 10px;
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
}

.break {
  flex-basis: 100%;
}
<div class="flex">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <span class="break"></span>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Using gap: 10px; sets 10px of gap both vertically and horizontally. You can use gap: 5px 10px; for 5px of vertical gap and 10px of horizontal.

.flex {
  display: flex;
  flex-flow: row wrap;
  gap: 5px 10px;
}

.item {
  width: 100px;
  height: 100px;
  background-color: orange;
  border-radius: 5px;
}

.break {
  flex-basis: 100%;
}
<div class="flex">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <span class="break"></span>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文