HTML/CSS multilevel nested lists numbering

MrUpsidown picture MrUpsidown · Feb 20, 2014 · Viewed 14.4k times · Source

Is there a way to achieve the below numbering using straight HTML and CSS lists (<ul> or <ol>)?

1. Link 1
2. Link 2
3. Link 3
    3.1. Link 3.1
    3.2. Link 3.2
    3.3. Link 3.3
4. Link 4
    4.1. Link 4.1
        4.1.1 Link 4.1.1
        4.1.2 Link 4.1.2
5. Link 5

Thanks in advance!

Answer

Hashem Qolami picture Hashem Qolami · Feb 20, 2014

You could use CSS counters:

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

li:before {
    counter-increment: section;
    content: counters(section, ".") ". Link " counters(section, ".") " ";
}

Working Demo (also on JSBin):

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

li:before {
  counter-increment: section;
  content: counters(section, ".") ". Link " counters(section, ".") " ";
}
<ol>
  <li></li>
  <li></li>
  <li>
    <ol>
      <li></li>
      <li></li>
      <li></li>
    </ol>
  </li>
  <li>
    <ol>
      <li>    
        <ol>
        <li></li>
        <li></li>
        </ol>
      </li>
    </ol>
  </li>
  <li></li>
</ol>