CSScounter-reset Property
Example
Create a counter ("my-sec-counter") and increase it by one for each occurrence of the <h2> selector:
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
h2::before {
/* Increment "my-sec-counter" by 1 */
counter-increment: my-sec-counter;
content: "Section " counter(my-sec-counter) ". ";
}
More "Try it Yourself" examples below.
Definition and Usage
Thecounter-reset property creates or resets one or more CSS counters.
Thecounter-reset property is usually used together with thecounter-increment property and thecontent property.
| Default value: | none |
|---|---|
| Inherited: | no |
| Animatable: | no.Read aboutanimatable |
| Version: | CSS2 |
| JavaScript syntax: | object.style.counterReset="section"Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
| Property | |||||
|---|---|---|---|---|---|
| counter-reset | 4.0 | 8.0 | 2.0 | 3.1 | 9.6 |
CSS Syntax
Property Values
| Value | Description |
|---|---|
| none | Default value. No counters will be reset |
| id number | Theid defines which counter to reset. Thenumber sets the value the counter is reset to on each occurrence of the selector. The defaultnumber value is 0 |
| initial | Sets this property to its default value.Read aboutinitial |
| inherit | Inherits this property from its parent element.Read aboutinherit |
More Examples
Example
Create a counter ("my-sec-counter") and decrease it by one for each occurrence of the <h2> selector:
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
h2::before {
/* Decrement "my-sec-counter" by 1 */
counter-increment: my-sec-counter -1;
content: "Section " counter(my-sec-counter) ". ";
}
Example
Numbering sections and sub-sections with "Section 1:", "1.1", "1.2", etc.:
/* Set "section" to 0 */
counter-reset: section;
}
h1 {
/* Set "subsection" to 0 */
counter-reset: subsection;
}
h1::before {
/* Increment "section" by 1 */
counter-increment: section;
content: "Section " counter(section) ": ";
}
h2::before {
/* Increment "subsection" by 1 */
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
Example
Create a counter and increase it by one (using Roman numerals) for each occurrence of the <h2> selector:
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
h2::before {
/* Increment "my-sec-counter" by 1 */
counter-increment: my-sec-counter;
content: counter(my-sec-counter, upper-roman) ". ";
}
Related Pages
CSS reference:::before pseudo element
CSS reference:::after pseudo element
CSS reference:content property
CSS reference:counter-increment property
CSS functions:counter() function
HTML DOM reference:counterReset property

