如何开始一个新列表,继续上一个列表的编号?

新手上路,请多包涵

在 ol 标签上的 start 属性被弃用之前,我正在尝试做一些曾经非常简单的事情。我只想在我的页面中有一对有序列表,但是从第一个列表结束的地方开始对第二个列表进行编号。就像是:

 1. do stuff
2. do stuff

Here's a paragraph

3. do stuff

我已经看到 counter-resetcounter-increment CSS 属性应该能够实现这一点,但我无法让它工作。到目前为止,这是我的代码:

 <html>
<head>
  <style type="text/css">
    ol li { counter-increment: mycounter; }
    ol.start { counter-reset: mycounter; }
    ol.continue { counter-reset: mycounter 2; }
  </style>
</head>

<body>
  <ol class="start">
    <li>You can't touch this</li>
    <li>You can't touch this</li>
  </ol>
  <p>STOP! Hammer time.</p>
  <ol class="continue">
    <li>You can't touch this</li>
  </ol>
</body>
</html>

老实说,即使这行得通,也不是理想的。我不想在我的 ol.continue 选择器中指定第一个列表达到的数字。

我究竟做错了什么?实现预期效果所需的最少 HTML/CSS 组合是什么?

原文由 Mal Ross 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 393
2 个回答

如前所述,您需要 :beforecontent ,但您还需要禁用默认列表编号。这是你的固定 CSS:

 ol.start {
    counter-reset: mycounter;
}
ol.start li, ol.continue li {
    list-style: none;
}
ol.start li:before, ol.continue li:before {
    content: counter(mycounter) ". ";
    counter-increment: mycounter;
}

您无需为 ol.continue 重置计数器,只需继续使用您的自定义计数器即可。上面的代码确保计数器仅用于 ol.startol.continue 列表。

原文由 Felix Kling 发布,翻译遵循 CC BY-SA 2.5 许可协议

OP问题的更简单解决方案如下:

 <ol start="X">

其中 X 是您要继续的列表的值,因此在他的示例中:

 <ol>
  <li>You can't touch this</li>
  <li>You can't touch this</li>
</ol>
<p>STOP! Hammer time.</p>
<ol start="3">
  <li>You can't touch this</li>
</ol>

原文由 Morkatog 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题