简体   繁体   English

CSS有序列表-在li包含多行时设置数字样式,保持缩进

[英]CSS ordered list - style the number, when li contains multiple lines, maintaining indentation

Variants of this question have been asked and answered, but I can't find an answer to this specific question... 已经提出并回答了这个问题的各种形式,但是我找不到这个特定问题的答案...

I would like to style the number in each li item in my ol , given that each li may span multiple lines, in a way that maintains the indentation. 我想在ol中为每个li项目中的数字设置样式, 使每个li可以跨越多行,从而保持缩进。

For example - here is a simple, un-styled example: 例如-这是一个简单的无样式示例:

 <div style="width: 200px;"> <ol> <li>blah blah blah blah blah blah blah blah blah blah</li> <li>blah</li> <li>blah blah blah blah blah blah blah blah blah blah</li> <li>blah</li> </ol> </div> 

This produces the following result - note the proper indentation of multiple lines: 这将产生以下结果-注意多行的正确缩进:

在此处输入图片说明

I've tried styling the numbers using li:before : 我试过使用li:before设置数字样式:

 ol { counter-reset: li; list-style: none; } li { counter-increment: li; } li:before { margin-right: 10px; content: counter(li) '.'; width: 1.2em; display: inline-block; } 
 <div style="width: 200px;"> <ol> <li>blah blah blah blah blah blah blah blah blah blah</li> <li>blah</li> <li>blah blah blah blah blah blah blah blah blah blah</li> <li>blah</li> </ol> </div> 

Now, I can easily style the numbers, but the indentation on multiple lines is lost: 现在,我可以轻松设置数字的样式,但是多行缩进会丢失:

在此处输入图片说明

So, how can I style the numbers, and maintain proper indentation? 那么,我该如何给数字加上样式保持适当的缩进呢?

Use this css. 使用此CSS。

li {
  counter-increment: li;
  padding-left: 30px;
}
li:before {
  margin-right: 10px;
  content: counter(li) '.';
  width: 1.2em;
  display: inline-block;
  margin-left: -30px;
}

You could just make some space on the left side of the element by padding it, remove the :before element from the content flow with position: absolute , and position it in the space created by the padding. 您可以通过对元素进行填充来在其左侧留出一些空间,从内容流中使用position: absolute移除:before元素,然后将其放置在由填充创建的空间中。

ol {
  counter-reset: li;
  list-style: none;
}
li {
  counter-increment: li;
  /* Anchor all :before elements to their respective li */
  position: relative;
  padding-left: 1em;
}
li:before {
  content: counter(li) '.';
  width: 1.2em;
  display: inline-block;
  position:absolute;
  left: 0;
}

If you don't mind introducing another element, you can just wrap the text in a span. 如果您不介意引入其他元素,则可以将文本包装在一个跨度中。

 li { color: red; } li span { color: black; } 
 <div style="width: 200px;"> <ol> <li><span>blah blah blah blah blah blah blah blah blah blah</span></li> <li><span>blah</span></li> </ol> </div> 

You can also update the display: to use the table values. 您还可以更新display:以使用table值。

The table-row value makes the element act like a <tr> and the table-cell makes the element act like a <td> ensuring that the different parts of the list stay aligned. table-row值使元素的行为像<tr> ,而table-cell使元素的行为像<td>确保列表的不同部分保持对齐。

 ol { counter-reset: li; list-style: none; } li { counter-increment: li; display:table-row; } li:before { margin-right: 10px; content: counter(li) '.'; width: 1.2em; display: table-cell; } 
 <div style="width: 200px;"> <ol> <li>blah blah blah blah blah blah blah blah blah blah</li> <li>blah</li> <li>blah blah blah blah blah blah blah blah blah blah</li> <li>blah</li> </ol> </div> 

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

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