html - 自定义嵌套有序列表编号

标签 html css html-lists

我有嵌套列表,我无法更改其结构。第一个 li 元素不能包含在编号中,但是,它的子元素必须全部编号。这是 JsFiddle .

ol li ol {
  counter-reset: section;
  list-style-type: none;
}

ol li ol li::before {
  counter-increment: section;
  content: counters(section, ".") " ";
}
<ol id="root">
  <li>List item two with subitems (this should not be numbered):
    <ol id="toc0">
      <li>Subitem 1 (This should be numbered 1)</li>
      <li>Subitem 2 (This should be 2)</li>
      <ol>
        <li> subitem (this should be 2.1)</li>
        <li> subitem (this should be 2.2)</li>
        <ol>
          <li> subitem (This should be 2.2.1)</li>
          <li> subitem (This should be 2.2.2)</li>
        </ol>
      </ol>
      <li>Subitem 3 (This should be 3)</li>
      <li>Subitem 4 (This should be 4)</li>
    </ol>
  </li>
</ol>

最佳答案

您的标记无效,ol 不能是另一个 ol 的直接子代。您必须更新 HTML 才能修复它,您现有的 CSS 只需稍作修改即可正常工作。

ol {
  list-style: none;
}

ol li ol {
  counter-reset: section;
}

ol li ol li::before {
  counter-increment: section;
  content: counters(section, ".") " ";
}
<ol id="root">
  <li>List item two with subitems (this should not be numbered):
    <ol id="toc0">
      <li>Subitem 1 (This should be numbered 1)</li>
      <li>
        Subitem 2 (This should be 2)
        <ol>
          <li>subitem (this should be 2.1)</li>
          <li>
            subitem (this should be 2.2)
            <ol>
              <li> subitem (This should be 2.2.1)</li>
              <li> subitem (This should be 2.2.2)</li>
            </ol>
          </li>
        </ol>
      </li>
      <li>Subitem 3 (This should be 3)</li>
      <li>Subitem 4 (This should be 4)</li>
    </ol>
  </li>
</ol>

关于html - 自定义嵌套有序列表编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52449583/

相关文章:

javascript - 透明色 HTML5 Canvas

html - CSS 如何在图片下方添加一个按钮

javascript - 在 html 文本框中显示下拉按钮的值

html - 在 block 内联 div 中居中图像

javascript - 如何在点击时更改 li 标签

html - 列表项的零缩进

javascript - 访问样式背景颜色

html - CSS水平下拉

javascript - 如何设置div滚动条默认在底部

html - 从无序的 HTML 列表中删除缩进