CSS Counters: counter-reset, counter-increment and Nested Numbering
Numbering anything with CSS counters: counter-reset, counter-increment and counter-set, counter() and counters() in any style, how scopes make nested numbering like 1.2.1 work, the built-in list-item counter, and the pitfalls that number every heading 1.
kvi5hnuCode Sprout team member7 min readOn this page
Ordered lists number themselves, but headings, figures, footnotes, steps in a tutorial and table rows don't. CSS counters number any elements, and the numbers update automatically when you add, remove or reorder content.
The three properties
| Property | Effect | Default number |
|---|---|---|
counter-reset: name 0 | creates a new counter instance on this element | 0 |
counter-increment: name 1 | adds to the counter (negative values count down) | 1 |
counter-set: name 10 | sets an existing counter to a value, without creating a new instance (if none is in scope, it creates one, like counter-increment) | 0 |
Then show the value with counter(name) in content:
article { counter-reset: figure; }
figure { counter-increment: figure; }
figcaption::before { content: "Figure " counter(figure) ": "; font-weight: 700; }
Several counters can be listed in one declaration: counter-reset: chapter section 0 note 1. Note that a later counter-reset or counter-increment declaration replaces the whole list, as with any property, so combine counters in one declaration instead of writing two rules.
Order on one element
On a single element, the steps happen in a fixed order: reset, then increment, then set, and then the element and its ::before and ::after are rendered with the result. So an element that increments a counter shows the incremented value, a counter reset to 0 and incremented on the same element shows 1, and counter-set wins over an increment on the same element.
Counter styles
counter() takes a second argument, any counter style, the same names as list-style-type:
h2::before { content: counter(chapter, upper-roman) ". "; } /* I. II. III. */
.appendix::before { content: counter(app, upper-alpha); } /* A B C */
.sub::before { content: counter(item, lower-alpha) ") "; } /* a) b) c) */
Tested in Chrome: one counter shown as counter(q, upper-roman) " / " counter(q, lower-alpha) gave "I / a", then "II / b". Custom styles from @counter-style work too (article 97).
Scope: where a counter exists
A counter-reset creates an instance that's visible in the element, its descendants, and its following siblings and their descendants. This sibling rule is what makes heading numbering work, since headings are siblings, not nested:
article { counter-reset: h2; }
h2 { counter-increment: h2; counter-reset: h3; } /* each h2 starts a new h3 count */
h3 { counter-increment: h3; }
h2::before { content: counter(h2) " "; }
h3::before { content: counter(h2) "." counter(h3) " "; }
Tested in Chrome: headings A, x, y, B, z were numbered 1, 1.1, 1.2, 2, 2.1.
The "everything is 1" mistake
Resetting and incrementing the same counter on the same element restarts it every time:
h2 { counter-reset: section; counter-increment: section; } /* wrong: always 1 */
Tested in Chrome: every <h2> with this rule showed section 1. The reset belongs on the parent level: the container, or the previous heading level, as in the example above.
Nested counters: counters()
When elements nest, like lists inside lists, each nested counter-reset creates a new instance inside the outer one. counter() shows only the innermost instance; counters(name, separator) joins all of them:

ol { counter-reset: item; list-style: none; }
li { counter-increment: item; }
li::before { content: counters(item, ".") " "; }
Tested in Chrome: this gave 1, 1.1, 1.2, 1.2.1 and 2, with one rule for any depth. counters() takes a style too, counters(item, ".", lower-alpha), which applies to every level.
The built-in list-item counter
Every element with display: list-item automatically increments a counter named list-item, and <ol> and <ul> reset it. Native markers show this counter, and you can use it too:
ol li::marker { content: counter(list-item) ") "; }
You can also adjust it, which changes the native numbering:
ol.skip-two li:nth-child(3) { counter-increment: list-item 2; } /* numbering jumps */
ol.restart li.new-part { counter-set: list-item 1; } /* this item is 1 again */
Tested in Chrome: in a four-item list, counter-increment: list-item 2 on the third item numbered the list 1, 2, 4, 5. With counter-set: list-item 0 on the third item instead, it showed 0, not 1, because the set happens after the item's own increment; counter-set: list-item 1 is what restarts at 1.
HTML attributes are clearer for content-based numbering (article 94): start, reversed and value.

Compare the first two panels: the only difference is where counter-reset lives. On the parent level it numbers the headings; on the heading itself it restarts every time.
Counting down
For a custom counter, count down with a negative increment from a starting value:
.countdown { counter-reset: n 4; }
.countdown li { counter-increment: n -1; } /* 3, 2, 1 */
CSS also defines counter-reset: reversed(n), which starts from the number of items automatically, but only Firefox supports it. Tested in Chrome: reversed(r) was treated like a normal reset, giving −1, −2, −3. For lists, <ol reversed> works everywhere, but use the native marker: tested in Chrome, counter(list-item) in a ::before on a reversed list showed 0, −1, −2, while the native markers showed 3, 2, 1.
What counts and what doesn't
- Elements with
display: nonedon't increment counters, butvisibility: hiddenelements do. Tested in Chrome: with the second of four itemsdisplay: noneand the thirdvisibility: hidden, the last item was numbered 3. - Using
counter-incrementwithout anycounter-resetcreates the counter automatically, on that element. It usually works, but resetting on a container makes the scope explicit. contain: style, whichcontainer-typealso applies (article 78), keeps counter changes inside an element from affecting the counter outside it.- Counters are generated content, so screen readers read them where the browser exposes
::beforetext; numbering that carries meaning, like legal clause numbers that people cite, is safer in the HTML.
body { font: 0.85rem/1.4 system-ui, sans-serif; margin: 1rem; }
article { counter-reset: h2 figure; }
h2 { counter-increment: h2; counter-reset: h3; font-size: 1.05rem; margin: 0.8rem 0 0.2rem; color: #8a3b12; }
h3 { counter-increment: h3; font-size: 0.9rem; margin: 0.2rem 0; }
h2::before { content: counter(h2, upper-roman) ". "; }
h3::before { content: counter(h2, upper-roman) "." counter(h3) " "; color: #8a3b12; }
figure { counter-increment: figure; margin: 0.3rem 0; padding: 0.3rem 0.5rem; background: #f4efe6; border-radius: 4px; }
figcaption::before { content: "Figure " counter(figure) ": "; font-weight: 700; }
.outline, .outline ol { counter-reset: item; list-style: none; padding-inline-start: 1rem; margin: 0.2rem 0; }
.outline { padding: 0; margin-top: 0.6rem; }
.outline li { counter-increment: item; }
.outline li::before { content: counters(item, ".") " "; font-variant-numeric: tabular-nums; color: #8a3b12; font-weight: 600; }
Headings use Roman numerals for h2 with a nested h3 count that restarts at each h2; figures count through the whole article; and the outline uses counters() for any depth.
What's current
counter-set: all browsers since Safari 17.2.counter-reset: reversed(): Firefox only.- The
list-itemcounter can be read and adjusted in all browsers.
Common questions
Why is every heading numbered 1?
The heading resets the counter it increments. Move counter-reset to the container or the parent heading level.
How do I number nested lists like 1.2.3?
counters(name, "."), with a counter-reset on every list.
What's the difference between counter-reset and counter-set?
counter-reset creates a new counter instance, starting a new scope; counter-set changes the value of the existing one.
Do hidden elements count?
visibility: hidden elements do; display: none elements don't.
Summary
counter-resetcreates a counter,counter-incrementsteps it,counter-setchanges it;counter()displays it in any counter style.- On one element: reset, then increment, then set, then display.
- A reset is visible in descendants and following siblings, which makes heading numbering work; nested resets create nested instances, joined by
counters(). - Don't reset and increment the same counter on the same element.
list-itemis the built-in list counter;display: noneelements don't count.
Practice questions
Write CSS that numbers tables "Table 1", "Table 2" in their captions, across the whole page.
Answer
body { counter-reset: table; }
table { counter-increment: table; }
caption::before { content: "Table " counter(table) ": "; }
Why is every <h3> numbered "1"?
h3 { counter-reset: sub; counter-increment: sub; }
h3::before { content: counter(sub) ". "; }
Answer
Each <h3> creates a new counter instance at 0 and increments it to 1. Move counter-reset: sub to the <h2> or the container.
A list has five items. The third has display: none and the fourth visibility: hidden. What number does the fifth show, with li { counter-increment: n }?
Answer
- The
display: noneitem isn't counted; the hidden one is.
What does counters(item, "-", upper-alpha) show for the second item of a list nested inside the third item of the outer list?
Answer
C-B: the outer instance's value is 3, shown as C, and the inner instance's value is 2, shown as B, joined with "-".
Previous: Generated Content: the content Property, Quotes and attr()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.