CSS Counters
CSS counters are named variables that CSS itself keeps track of and increases as it walks through your document. You use them to number things automatically, such as headings, steps in a tutorial, figure captions or nested outline items, without typing the numbers into the HTML.
The three properties that make a counter work
A counter needs only three pieces:
counter-resetcreates the counter (or sets it back to zero) on a parent element.counter-incrementadds one each time a matching element appears.counter()orcounters()prints the current value, almost always inside thecontentproperty of a::beforeor::afterpseudo-element.
The counter name is your own choice, like step or chapter. It does not need to be declared anywhere else.
.recipe {
counter-reset: step;
}
.recipe h3 {
counter-increment: step;
}
.recipe h3::before {
content: "Step " counter(step) ": ";
color: #0d6efd;
font-weight: 700;
}
With this CSS, every <h3> inside .recipe gets "Step 1:", "Step 2:" and so on. If you add a new step in the middle of the HTML, every number after it updates by itself.
Numbering in different styles and steps
The counter() function accepts a second argument for the list style, the same keywords you use with list-style-type. For example, counter(step, upper-roman) prints I, II, III, and counter(step, lower-alpha) prints a, b, c.
You can also change the increment and the starting value. counter-increment: step 2 counts by twos, and counter-reset: step 4 makes the first incremented item show 5, because the reset sets the value before the first increment is applied.
Nested counters for outlines
Legal documents and long guides often need numbers like 2.1 and 2.1.3. The counters() function, with an "s", joins every level of a same-named counter using a separator string you choose.
ol {
counter-reset: item;
list-style: none;
}
li {
counter-increment: item;
}
li::before {
content: counters(item, ".") " ";
font-weight: 600;
}
Each nested <ol> creates a fresh instance of item, so an item inside a second-level list prints something like "1.2", and the outer numbering continues correctly afterwards.
Where counters go wrong
The most common surprise is putting counter-reset on the same element that you increment. If each h3 resets and then increments, every heading shows 1. The reset belongs on a shared parent or an earlier element.
Other things to keep in mind:
- Elements with
display: nonedo not increment counters, so hidden items will not leave gaps in the numbering. - Generated content is not reliably copied when a user selects text, and some assistive technologies may read it differently. For numbers that carry real meaning, such as clause numbers in a contract, consider writing them into the HTML.
- If you want basic numbering for a plain list, an ordinary
<ol>already does that. Counters shine when you need custom text, styling, or numbering of non-list elements.
Practical uses
Beyond headings, counters are handy for numbering figures ("Figure 3"), table rows in a report, FAQ entries, or showing how many checkboxes are ticked when combined with the :checked selector. They keep numbers in sync with the content, which saves you from manual edits every time the page changes.
In short: reset on the parent, increment on the item, and display with counter() inside ::before.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.