user10059339
user10059339

Reputation:

Customize nested ordered lists numbering

I have nested lists of which I can't change the structure of. The first li element must not be included in the numbering, however, its children must ALL be numbered. Here is the 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>

Upvotes: 2

Views: 1096

Answers (1)

Stickers
Stickers

Reputation: 78686

You have invalid markup, ol cannot be a direct child of another ol. You must update the HTML to fix it, your existing CSS works fine with a little edit.

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>

Upvotes: 6

Related Questions