简体   繁体   English

如何设置列表项之间的垂直间距?

[英]How do I set vertical space between list items?

Within a <ul> element, clearly the vertical spacing between lines can be formatted with the line-height attribute.<ul>元素中,显然行之间的垂直间距可以使用 line-height 属性进行格式化。

My question is, within a <ul> element, how do I set the vertical spacing between the list items?我的问题是,在<ul>元素中,如何设置列表项之间的垂直间距?

You can use margin.您可以使用保证金。 See the example:看例子:

http://jsfiddle.net/LthgY/ http://jsfiddle.net/LthgY/

li{
  margin: 10px 0;
}

HTML HTML

<ul>
   <li>A</li>
   <li>B</li>
   <li>C</li>
   <li>D</li>
   <li>E</li>
</ul>

CSS CSS

li:not(:last-child) {
    margin-bottom: 5px;
}

EDIT: If you don't use the special case for the last li element your list will have a small spacing afterwards which you can see here: http://jsfiddle.net/wQYw7/编辑:如果你不使用最后一个 li 元素的特殊情况,你的列表之后会有一个小的间距,你可以在这里看到: http : //jsfiddle.net/wQYw7/

Now compare that with my solution: http://jsfiddle.net/wQYw7/1/现在将其与我的解决方案进行比较: http : //jsfiddle.net/wQYw7/1/

Sure this doesn't work in older browsers but you can easily use js extensions which will enable this for older browsers.当然这在旧浏览器中不起作用,但您可以轻松使用 js 扩展,这将为旧浏览器启用此功能。

I would be inclined to this which has the virtue of IE8 support.我倾向于这个具有 IE8 支持的优点。

li{
    margin-top: 10px;
    border:1px solid grey;
}

li:first-child {
    margin-top:0;
}

JSFiddle JSFiddle

Add a margin to your li tags.为您的li标签添加一个margin That will create space between the li and you can use line-height to set the spacing to the text within the li tags.这将在li之间创建空间,您可以使用line-height设置li标签内文本的间距。

Old question but I think it lacked an answer.老问题,但我认为它缺乏答案。 I would use an adjacent siblings selector.我会使用相邻的兄弟姐妹选择器。 This way we only write "one" line of CSS and take into consideration the space at the end or beginning, which most of the answers lacks.这样我们只写了“一行”CSS,并考虑了结尾或开头的空格,这是大多数答案所缺乏的。

li + li {
  margin-top: 10px;
}

To apply to an entire list, use要应用于整个列表,请使用

ul.space_list li { margin-bottom: 1em; }

Then, in the html:然后,在 html 中:

<ul class=space_list>
<li>A</li>
<li>B</li>
</ul>

Many times when producing HTML email blasts you cannot use style sheets or style /style blocks.很多时候在生成 HTML 电子邮件时,您不能使用样式表或样式 / 样式块。 All CSS needs to be inline.所有 CSS 都需要内联。 In the case where you want to adjust the spacing between the bullets I use li style="margin-bottom:8px;"如果您想调整项目符号之间的间距,我使用 li style="margin-bottom:8px;" in each bullet item.在每个项目符号中。 Customize the pixels value to your liking.根据您的喜好自定义像素值。

you can also use the line-height property on the ul您还可以在 ul 上使用 line-height 属性

 ul { line-height: 45px; }
 <ul> <li>line one</li> <li>line two</li> <li>line three</li> </ul>

setting padding-bottom for each list using pseudo class is a viable method.使用伪类为每个列表设置 padding-bottom 是一种可行的方法。 Also line height can be used.也可以使用行高。 Remember that font properties such as font-family, Font-weight, etc. plays a role for uneven heights.请记住,字体属性(例如 font-family、Font-weight 等)对高度不均匀有影响。

Use CSS grid on the parent ul element like so:在父ul元素上使用 CSS 网格,如下所示:

ul {
  display: grid;
  gap: 10px;
}

I found that it was much easier for me to create spacing after list items by adding margin-bottom to ordered lists unordered lists as a whole instead of individual list items.我发现通过将 margin-bottom 添加到有序列表作为一个整体而不是单个列表项来创建列表项之后的间距要容易得多。

 <ol style="line-height: 1.5em; margin-bottom: 15px;"> <li>A</li> <li>B</li> <li>C</li> </ol> <ul style="line-height: 1.5em; margin-bottom: 15px;"> <li>A</li> <li>B</li> <li>C</li> </ul>

<br> between <li></li> line entries seems to work perfectly well in all web browsers that I've tried, but it fails to pass the on-line W3C CSS3 checker. <br> <li></li>行条目之间的<br>似乎在我尝试过的所有网络浏览器中都运行良好,但它未能通过在线 W3C CSS3 检查器。 It gives me precisely the line spacing I am after.它准确地为我提供了我所追求的行距。 As far as I am concerned, since it undoubtedly works, I am persisting in using it, whatever W3C says, until someone can come up with a good legal alternative.就我而言,因为它无疑有效,所以无论 W3C 怎么说,我都会坚持使用它,直到有人能提出一个好的合法替代方案。

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

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