自定义嵌套的有序列表编号

时间:2018-09-21 18:52:27

标签: html css css3 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>

1 个答案:

答案 0 :(得分:5)

您的标记无效,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>