TExcelTAD
Home › CSS › CSS Counters

CSS Counters

CSS counters are variables maintained by CSS that you can increment and display with generated content. They let you number headings, steps, figures or nested sections automatically — no need to type numbers into the HTML, and they update themselves when you reorder content.

The three properties

Property / function Purpose
counter-reset: name [start] Creates (or resets) a counter, default start 0
counter-increment: name [step] Adds to the counter (default +1) each time the element appears
counter(name, style) Outputs the current value, used inside content
counters(name, "sep", style) Outputs all nested levels joined by a separator, e.g. 2.1.3
counter-set: name value Sets a counter to a specific value without creating a new scope

Numbering headings

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; counter-reset: chapter; }
  h3 { counter-increment: chapter; margin: 8px 0; }
  h3::before { content: "Chapter " counter(chapter) ": "; color: #d81b60; }
</style>
</head>
<body>
  <h3>Getting started</h3>
  <h3>Selectors</h3>
  <h3>The box model</h3>
  <h3>Layout</h3>
</body>
</html>

Try moving a heading up or deleting one — the numbers fix themselves.

Nested counters

Reset a second counter inside each section to create multi-level numbering like 1.1, 1.2, 2.1.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; counter-reset: section; }
  h4 { counter-increment: section; counter-reset: sub; margin: 10px 0 4px; color: #1565c0; }
  h4::before { content: counter(section) ". "; }
  p { counter-increment: sub; margin: 2px 0 2px 16px; }
  p::before { content: counter(section) "." counter(sub) " "; font-weight: bold; color: #90a4ae; }
</style>
</head>
<body>
  <h4>Colors</h4><p>Names</p><p>HEX</p><p>RGB</p>
  <h4>Text</h4><p>Alignment</p><p>Decoration</p>
</body>
</html>

counters() for nested lists

For lists inside lists, use a single counter name and counters(). Each <ol> creates a new instance of the counter, and counters() joins all levels.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  ol { counter-reset: item; list-style: none; padding-left: 18px; }
  li { counter-increment: item; margin: 3px 0; }
  li::before { content: counters(item, ".") " "; color: #6a1b9a; font-weight: bold; }
</style>
</head>
<body>
  <ol>
    <li>Install
      <ol><li>Download</li><li>Run setup
        <ol><li>Accept license</li><li>Choose folder</li></ol>
      </li></ol>
    </li>
    <li>Configure</li>
    <li>Launch</li>
  </ol>
</body>
</html>

Styles and steps

The second argument of counter() is any list-style type: upper-roman, lower-alpha, decimal-leading-zero and more. counter-increment can also take a step, such as 10 or -1.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .steps { counter-reset: step; display: flex; gap: 10px; }
  .steps div { counter-increment: step; flex: 1; text-align: center; font-size: 14px; }
  .steps div::before {
    content: counter(step, decimal-leading-zero);
    display: grid; place-items: center; margin: 0 auto 6px;
    width: 44px; height: 44px; border-radius: 50%;
    background: #00897b; color: white; font-weight: bold;
  }
  .roman { counter-reset: r; }
  .roman span { counter-increment: r; margin-right: 12px; }
  .roman span::before { content: counter(r, upper-roman) " · "; color: #ef6c00; }
  .countdown { counter-reset: c 6; }
  .countdown b { counter-increment: c -1; margin-right: 8px; }
  .countdown b::after { content: counter(c); }
</style>
</head>
<body>
  <div class="steps"><div>Cart</div><div>Address</div><div>Payment</div><div>Done</div></div>
  <p class="roman"><span>Intro</span><span>Body</span><span>End</span></p>
  <p class="countdown">Countdown: <b></b><b></b><b></b><b></b><b></b></p>
</body>
</html>

Tip: Generated numbers aren't part of the document text and may not be copied or announced consistently. For content where numbering is meaningful (legal clauses, recipes), a real <ol> is often the more accessible choice.

Test yourself

Which property creates a counter and sets it to its starting value?

  1. counter-increment
  2. counter-reset
  3. counter-start
  4. counter()

What does counters(item, ".") output for a third-level item?

  1. Only the innermost number
  2. All levels joined with dots, e.g. 1.2.1
  3. The total count
  4. Nothing

Where is the counter value displayed?

  1. In the HTML automatically
  2. Through the content property of a pseudo-element
  3. In the title attribute
  4. In the console