Home  >  Article  >  Web Front-end  >  Why are my nested ordered lists displaying incorrect numbering in HTML and CSS, and how can I fix it?

Why are my nested ordered lists displaying incorrect numbering in HTML and CSS, and how can I fix it?

Susan Sarandon
Susan SarandonOriginal
2024-11-16 12:49:03760browse

Why are my nested ordered lists displaying incorrect numbering in HTML and CSS, and how can I fix it?

Nested Ordered Lists with Incorrect Numbering

This issue arises when creating nested ordered lists using HTML and CSS. The following code is used:

ol {
    counter-reset: item;
    padding-left: 10px;
}
li {
    display: block
}
li:before {
    content: counters(item, ".") " ";
    counter-increment: item
}
<ol>
    <li>one</li>
    <li>two</li>
    <ol>
        <li>two.one</li>
        <li>two.two</li>
        <li>two.three</li>
    </ol>
    <li>three</li>
    <ol>
        <li>three.one</li>
        <li>three.two</li>
        <ol>
            <li>three.two.one</li>
            <li>three.two.two</li>
        </ol>
    </ol>
    <li>four</li>
</ol>

Incorrect Output:

The expected output should resemble:

1. one
2. two
  2.1. two.one
  2.2. two.two
  2.3. two.three
3. three
  3.1 three.one
  3.2 three.two
    3.2.1 three.two.one
    3.2.2 three.two.two
4. four

However, the actual outcome displays incorrect numbering:

1. one
2. two
  2.1. two.one
  2.2. two.two
  2.3. two.three
2.4 three
  2.1 three.one
  2.2 three.two
    2.2.1 three.two.one
    2.2.2 three.two.two
2.3 four

Solutions:

To resolve this issue, two solutions are available:

  1. Disable "Normalize CSS" in JSFiddle. This option defaults all list margins and paddings to 0.
  2. Include sub-lists within the main
  3. elements, as shown below:
ol {
  counter-reset: item
}
li {
  display: block
}
li:before {
  content: counters(item, ".") " ";
  counter-increment: item
}
<ol>
  <li>one</li>
  <li>two
    <ol>
      <li>two.one</li>
      <li>two.two</li>
      <li>two.three</li>
    </ol>
  </li>
  <li>three
    <ol>
      <li>three.one</li>
      <li>three.two
        <ol>
          <li>three.two.one</li>
          <li>three.two.two</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>four</li>
</ol>

Once implemented, the ordered lists will display the correct numbering as intended.

The above is the detailed content of Why are my nested ordered lists displaying incorrect numbering in HTML and CSS, and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn