CSS Reference ยท Lists, Tables and Generated Content
The counter-reset property
Reference for the CSS counter-reset property: create or reset named counters, set start values, understand counter scope and build nested numbering.
CSS counters are named numbers the browser maintains while it renders a page. The counter-reset property creates a counter on an element (or resets an existing one) and gives it a starting value. You then raise it with counter-increment and print it with counter() inside content.
CSS
body { counter-reset: chapter; } h2 { counter-increment: chapter; } h2::before { content: "Chapter " counter(chapter) ". "; }
Try it
Syntax
CSS
counter-reset: none | [ <counter-name> <integer>? ]+; counter-reset: chapter; /* chapter = 0 */ counter-reset: section 10 item 1; counter-reset: reversed(steps); /* counts down */
| Initial value | none |
| Applies to | All elements |
| Inherited | No |
| Animatable | No |
| Computed value | none, or a list of name and integer pairs |
Values
| Value | Description |
|---|---|
none |
No counters are created or reset. |
<counter-name> |
A name you choose (an identifier; not none, inherit, initial or unset). |
<integer> |
The value the counter gets. Defaults to 0 if omitted. May be negative. |
reversed(<name>) |
Creates a counter that counts down, starting from the number of items; mainly used with lists. Newer feature, so check a compatibility table. |
Several counters can be listed, separated by spaces.
Usage notes
- Scope. A counter created on an element is available to that element, its descendants, and its following siblings with their descendants. That lets a heading increment a counter that was reset on a parent.
- Nested counters. If a descendant resets a counter with the same name, a new instance is created inside it. Use
counters(name, ".")to join all instances into text such as1.2.3. - Same element. Reset is processed before increment, so an element can reset a counter and increment it in the same rule (the nested example in
counter-incrementrelies on this). - Reset at the right level. Reset the counter on the container that should restart numbering (a section, an article), not on the element that counts.
- Hidden elements. Elements with
display: noneneither reset nor increment counters;visibility: hiddenones do. - Lists have a built-in counter called
list-item, which you can also reset, for example to start numbering elsewhere. - Accessibility. Generated numbers may not be read out reliably; for real numbered lists use
<ol>.
Browser support
Supported in all browsers. reversed() is more recent; check a compatibility table.