Full answer
A CSS counter is a number the browser keeps while it reads the page. You decide when it starts, when it goes up, and where it is shown. This is useful for numbered headings, figure captions or steps, because the numbers stay correct when you add or move an item.
Three pieces are involved.
counter-reset: namecreates or resets a counter on an element. The value starts at 0.counter-increment: nameadds 1 (or the amount you give) each time the selector matches.counter(name)shows the current value inside acontentproperty, usually on::before.
For nested numbering like 1, 1.1, 1.2, use counters(name, "."). Each nested element that resets the counter creates a new level, and counters() joins all levels with the separator.
A built-in alternative is ol with li, which numbers itself. Counters are for cases where you cannot use ol, or where numbers should appear on headings, table rows or steps built from div.
The headings read "1. Install the tools" and "2. Write the page". The step counter restarts after each h2, so the paragraphs read Step 1, Step 2, then Step 1 again. The list prints 1, 1.1, 1.2 and 2.
Generated numbers are not part of the page text. Screen readers may or may not read them, so for important numbering, such as steps, also consider a real ol.
<style>
body { counter-reset: section; }
h2 { counter-reset: step; }
h2::before { counter-increment: section; content: counter(section) ". "; }
.step::before { counter-increment: step; content: "Step " counter(step) ": "; font-weight: bold; }
ul.tree, ul.tree ul { counter-reset: item; list-style: none; }
ul.tree li::before { counter-increment: item; content: counters(item, ".") " "; }
</style>
<h2 id="h1">Install the tools</h2>
<p class="step">Download the editor.</p>
<p class="step">Open a new file.</p>
<h2 id="h2">Write the page</h2>
<p class="step">Type the HTML.</p>
<ul class="tree">
<li>Fruit
<ul><li>Mango</li><li>Banana</li></ul>
</li>
<li>Vegetables</li>
</ul>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- counter-reset creates the counter, counter-increment raises it.
- counter(name) shows the value in content, usually on ::before.
- counters(name, separator) joins nested levels, such as 1.2.
- Resetting on a parent restarts numbering for each section.
- Generated numbers are not real text, so use ol when meaning matters.
Common mistakes
- Forgetting counter-reset, so numbering continues from another part of the page.
- Using counter-increment on the same element that shows it before the reset, so the first number starts at the wrong value.
- Relying on counter numbers for meaning that screen readers must hear. Generated content support in assistive technology varies.
Follow-ups
- How is counters() different from counter()?
- When is a plain ol a better choice than a CSS counter?
Tips
- Describe the three steps (reset, increment, display) before showing any code.
- Use counters() with a separator for outlines such as 1.2.3.