简体   繁体   English

带有段落 CSS 的自定义排序列表

[英]Custom ordered list with paragraphs CSS

I'm trying to get a customized ordered list to work with paragraphs and other containers but I can't get the lists to behave the same as the default.我正在尝试使用自定义的有序列表来处理段落和其他容器,但我无法使列表的行为与默认值相同。

Specifically, I would like to have a customized list and paragraphed content that appears on the same line as the enumeration.具体来说,我希望有一个自定义列表和段落内容与枚举显示在同一行。 Also, I'm looking for a solution that changes the list and not the paragraph.另外,我正在寻找一种更改列表而不是段落的解决方案。 The paragraph is just an example of some generated content that I do not wish to alter.该段落只是我不想更改的一些生成内容的示例。

 .custom { list-style-type: none; counter-reset: elementcounter; } .custom li:before { content: counter(elementcounter) ". "; counter-increment: elementcounter; } .solution { list-style-type: none; counter-reset: elementcounter; } .solution li>p { display: inline-block; } .solution li:before { content: counter(elementcounter) ". "; counter-increment: elementcounter; }
 <ol> <li><p>Places nicely on the same line.</p></li> <li><p>Places nicely on the same line.</p> <p>Places nicely on the same line.</p></li> </ol> <!-- Original problem <ol class="custom"> <li><p>Places poorly on the second line.</p></li> <li><p>Places poorly on the second line.</p> <p>Places nicely on the second line.</p></li> </ol> --> <ol class="solution"> <li><p>Places poorly on the second line.</p></li> <li><p>Places poorly on the second line.</p> <p>Places poorly on the same line.</p></li> </ol>


As @Temani Afif mentioned, you could add inline-block to the p - like this:正如@Temani Afif提到的,您可以将inline-block添加到p - 像这样:

.custom li > p {
  display: inline-block;
  margin: 2px; /* you can also adjust your margins/padding if you wish */
}

UPDATE更新

Based on the comments (and updated question) if you have multiple paragraphs on the same <li> then you can add different styles for the first p in the list and the rest of the p s on the list.根据该意见(和更新的问题),如果你有相同的多个段落<li>那么你可以第一个添加不同风格p在列表中的其余p单子上。 Something along the lines of:类似的东西:

.custom li > p {
  margin: 2px;
}

.custom li > p + p {
  display: block; 
  margin: 0 1.1em;
  list-style-type: none;
}

.custom li > p:first-of-type {
  display: inline-block;
}

See demo code below..请参阅下面的演示代码..

Updated demo code based on comments根据评论更新演示代码

 .custom { list-style-type: none; counter-reset: elementcounter; } .custom li:before { content: counter(elementcounter) ". "; counter-increment: elementcounter; } .custom li > p { margin: 2px; } .custom li > p + p { display: block; margin: 0 1.1em; list-style-type: none; } .custom li > p:first-of-type { display: inline-block; }
 <ol> <li> <p>Places nicely on the same line.</p> </li> <li> <p>Places nicely on the same line.</p> </li> </ol> <ol class="custom"> <li><p>Places poorly on the second line.</p></li> <li><p>Places poorly on the second line.</p> <p>Places poorly on the same line.</p></li> <li><p>Places poorly on the second line.</p></li> <li><p>Places poorly on the second line.</p> <p>Places poorly on the same line.</p></li> </ol>

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

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