counter-reset
Definition
The CSS counter-reset property is used to reset the value of a counter to a specified value. It is used in conjunction with the counter-increment and counter() properties to display a running list of items with custom symbols or styles.
Examples
In this example, a counter named section
is reset for the entire body
element using counter-reset: section;
. The h1
element's :before
pseudo-element increments the section
counter using counter-increment: section;
, and then the content
property displays the counter value using content: "Section " counter(section) ": ";
:
body {
counter-reset: section;
}
h1:before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
In this example, an ordered list ol
has a counter named li
reset using counter-reset: li;
. Each list item li
increments the li
counter using counter-increment: li;
, and the :before
pseudo-element of each list item displays the counter value using content: counter(li) ".";
:
ol {
counter-reset: li;
}
li {
counter-increment: li;
}
li:before {
content: counter(li) ".";
}
In this example, a counter named custom-symbol
is reset for the entire body
element using counter-reset: custom-symbol;
. The h2
element's :before
pseudo-element increments the custom-symbol
counter using counter-increment: custom-symbol;
, and then the content
property displays the custom symbol using content: "➤";
:
body {
counter-reset: custom-symbol;
}
h2:before {
counter-increment: custom-symbol;
content: "➤";
}
Values
Value | Description |
---|---|
[counter-name] | The name of the counter that will be reset. |
[integer] | The value to which the counter will be reset. The default is 0. |
Best Practices
- Use the counter-reset property in conjunction with counter-increment to create a running list of items with custom symbols or styles.
- Use the counter() function to display the value of a counter in the content property.
- Use unique names for each counter to avoid conflicts.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |