简体   繁体   English

2列列表项之间的尴尬间距

[英]Awkward spacing between 2 column list items

I have a list of ingredients in a two-column layout, using flex-box. 我使用flex-box在两列布局中列出了成分列表。 When an ingredient is long enough to cause the text to break to a second line, it causes awkward vertical spacing between the list items in the adjacent column. 当一种成分足够长而导致文本中断到第二行时,它将导致相邻列中的列表项之间的垂直间距笨拙。 Before using flex-box, I tried using making the list items inline-blocks and floated them. 在使用flex-box之前,我尝试使用内联块使列表项浮动。

Here's what it looks like on my website: 这是我的网站上的样子: 在此处输入图片说明

Here's what my code looks like: 这是我的代码:

 .field-items { display: flex; flex-wrap: wrap; align-items: flex-start; } .field-item { width: 50%; float: left; } 
 <div class="field-items"> <div class="field-item even"> 8 ounces sugar snap peas, stringed </div> <div class="field-item odd"> 12 grape tomatoes, halved </div> <div class="field-item even">2 tablespoons sliced almonds</div> <div class="field-item odd">2 tablespoons extra-virgin olive, walnut, or almond oil</div> <div class="field-item even"> 2 tablespoons fruity vinegar, such as raspberry or pomegranate </div> <div class="field-item odd"> ¼ teaspoon salt </div> <div class="field-item even"> 1/8 teaspoon freshly ground pepper </div> <div class="field-item odd"> 4 cups packed mixed baby lettuce </div> <div class="field-item even"> ¼ cup snipped fresh chives (½-inch pieces) </div> <div class="field-item odd"> ¼ cup chopped fresh tarragon </div> </div> 

See it in action: https://jsfiddle.net/4t4pd3tt/2/ 实际观看: https : //jsfiddle.net/4t4pd3tt/2/

My goal is to have even spacing between all of the list elements. 我的目标是在所有列表元素之间保持均匀的间距。 I'm sure this question has already been answered before, but I wasn't able to find what I was looking for in my initial search. 我确定这个问题之前已经得到解答,但是在最初的搜索中找不到我想要的东西。

I've been in a similar position where I tried to do this with flex . 我曾在类似的位置尝试使用flex进行此操作。 I spent a long, long time on it and eventually found out that there is no nice way of doing it. 我花了很长时间,最终发现没有很好的方法。 My suggestion would be to go back to the trusty (read: awkward and annoying) float . 我的建议是回到值得信赖的(笨拙又烦人的) float

You just need to clean up your CSS a little and you can float the odd elements to the left and the even elements to the right (or the other way, if you so wish, but that would be kinda stupid, so I wouldn't advise doing that. 你只需要清理你的CSS一点,你可以浮动odd元素在左边, even元素向右(或其他方式,如果你愿意的话,但是这将是愚蠢的有点,所以我不会建议这样做。

.field-item {
  width: 50%;
}

.field-item.odd {
  float: left;
}

.field-item.even {
  float: right;
}

For this, you can remove all the rules from .field-items in your CSS. 为此,您可以从CSS中的.field-items中删除所有规则。

An obvious issue here is that you need to continue to add odd and even classes to the div tags, which could easily cause a bug if you do it wrong. 一个明显的问题是,您需要继续向div标签添加oddeven类,如果操作不正确,很容易导致错误。

I hope this helps! 我希望这有帮助!

EDIT 编辑

As Jonathan Nicol pointed out, you can also use nth-child(odd|even) to achieve the same thing, without the need for specific classes (rendering my note above redundant. 正如乔纳森·尼科尔(Jonathan Nicol)所指出的那样,您也可以使用nth-child(odd|even)来实现相同的功能,而无需特定的类(使我的笔记变得多余)。

.field-item {
  width: 50%;
}

.field-item:nth-child(odd) {
  float: left;
}

.field-item:nth-child(even) {
  float: right;
}

To make that work using Flexbox, you need amongst other, to use flex-direction: column and give the flex container a fixed height. 要使用Flexbox使其工作,除其他外,您还需要使用flex-direction: column并为flex容器设置固定的高度。

Instead, for column layouts, CSS Columns is the proper way. 相反,对于列布局,CSS Columns是正确的方法。

Stack snippet 堆栈片段

 .field-items { -moz-column-count: 2; -webkit-column-count: 2; column-count: 2; } .field-item { } 
 <div class="field-items"> <div class="field-item"> 8 ounces sugar snap peas, stringed </div> <div class="field-item"> 12 grape tomatoes, halved </div> <div class="field-item">2 tablespoons sliced almonds</div> <div class="field-item">2 tablespoons extra-virgin olive, walnut, or almond oil</div> <div class="field-item"> 2 tablespoons fruity vinegar, such as raspberry or pomegranate </div> <div class="field-item"> ¼ teaspoon salt </div> <div class="field-item"> 1/8 teaspoon freshly ground pepper </div> <div class="field-item"> 4 cups packed mixed baby lettuce </div> <div class="field-item"> ¼ cup snipped fresh chives (½-inch pieces) </div> <div class="field-item"> ¼ cup chopped fresh tarragon </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM