SyntaxStudy
Sign Up
HTML Custom List Counters
HTML Intermediate 8 min read

Custom List Counters

Custom List Counters

CSS counters let you create fully custom numbering schemes without JavaScript. You define a counter with counter-reset on a parent element, increment it with counter-increment on each item, and display it with content: counter(name) in a ::before pseudo-element.

Nested Counters

For nested numbering like "1.1, 1.2, 2.1" you use multiple counters simultaneously. Each level resets its own counter when a new parent item starts. The counters() function (plural) concatenates all ancestor counter values with a separator you specify, making "1.2.3" style numbering straightforward to implement.

Example
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Custom Counters</title>
  <style>
    ol.custom {
      list-style: none;
      counter-reset: section;
      padding-left: 0;
    }
    ol.custom li {
      counter-increment: section;
      padding: 6px 0 6px 40px;
      position: relative;
    }
    ol.custom li::before {
      content: counter(section, upper-roman) ".";
      position: absolute;
      left: 0;
      font-weight: bold;
      color: #c0392b;
    }

    /* Nested counters: 1.1, 1.2 ... */
    ol.nested, ol.nested ol { list-style: none; counter-reset: item; }
    ol.nested li { counter-increment: item; }
    ol.nested li::before {
      content: counters(item, ".") " ";
      font-weight: bold;
    }
  </style>
</head>
<body>
  <ol class="custom">
    <li>Introduction</li>
    <li>Main Content</li>
    <li>Conclusion</li>
  </ol>

  <ol class="nested">
    <li>Chapter One
      <ol><li>Section A</li><li>Section B</li></ol>
    </li>
    <li>Chapter Two
      <ol><li>Section A</li></ol>
    </li>
  </ol>
</body>
</html>
Pro Tip

CSS counters can style any set of elements — not just lists. You can apply counter-reset to any container and counter-increment to any selector, making them useful for auto-numbering figures, tables, and headings throughout a long document.