【问题标题】:Custom ordered list with paragraphs CSS带有段落 CSS 的自定义有序列表
【发布时间】:2020-03-05 12:08:29
【问题描述】:

我正在尝试获得一个自定义的有序列表来处理段落和其他容器,但我无法让列表的行为与默认值相同。

具体来说,我希望有一个自定义列表和分段内容,它与枚举出现在同一行。另外,我正在寻找一种可以更改列表而不是段落的解决方案。该段落只是一些我不想更改的生成内容的示例。

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

【问题讨论】:

  • display:inline-blockp ?
  • 我已经尝试过了,但如果您有可行的解决方案,请提供代码片段。

标签: html css paragraph


【解决方案1】:

正如@Temani Afif 提到的,您可以将inline-block 添加到p - 像这样:

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

更新

根据 cmets(和更新的问题),如果您在同一个 &lt;li&gt; 上有多个段落,那么您可以为列表中的第一个 p 和列表中的其余 ps 添加不同的样式.大致如下:

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

见下面的演示代码..

更新了基于 cmets 的演示代码

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

【讨论】:

  • 太棒了!谢谢大家,但它的性能仍然与原始列表不同。我已将第二项更改为包含两个段落。非自定义列表获得预期的行为,但解决方案没有(段落之间没有换行符)。
  • @fcimeson 我不确定我是否理解您的评论,但 我根据我的理解更新了答案 - 您需要根据font-familyfont-size 您正在使用(以及应用于您的页面的其他样式,例如 padding 等...以上只是一个起点
  • 看起来更好!必须调整边距有点痛苦。你知道默认的有序列表设置为(对于 css)吗?
  • 哦,伙计,有一些默认值 (w3schools.com/cssref/css_default_values.asp) 但很多默认 也是从父级继承的,所以我不知道该问题的简单答案。
猜你喜欢
  • 2011-05-31
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多