DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
The counter-reset
property allows for automatic numbering of elements. Like an ordered list (<ol>
) element.
article {
counter-reset: section;
}
section {
counter-increment: section;
}
section h2:before {
content: counter(section) '. ';
}
The counter-reset
property is used to reset a CSS counter to a given value.
It is part of the CSS counter module which is part of the generated content, automatic numbering, and lists CSS2.1 specification, extended in Generated and Replaced Content Module CSS3 specification.
Syntax
counter-reset: [<user-ident> <integer>?]+ | none</integer></user-ident>
Where…
<user-ident></user-ident>
is the name of the counter you want to reset<integer></integer>
is the value to reset the counter tonone
disable the counter
body {
counter-reset: my-awesome-counter 0;
}
Note: the default value for the integer is 0. If no integer is set after the counter name, it will be reseted to 0. Thus, this works as expected:
body {
counter-reset: my-awesome-counter;
}
You can reset several counters at once with a space-separated list, each one with its specific value if you wish so.
body {
counter-reset: my-awesome-counter 5 my-other-counter;
}
This will reset my-awesome-counter
to 5 and my-other-counter
to the default value: 0.
You can see this list as: counter1 value1 counter2 value2 counter3 value3 ...
. If a value is omitted, it’s 0.
Demo
In the following demo, article
resets section
counter to its default value (0), which is then incremented at each section occurrence, then displayed in front of section headings.
Browser support
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
4 | 2 | 8 | 12 | 3.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
127 | 127 | 2.1 | 3.2 |
Very useful! Thanks!!!
Always learn a lot from CSS-Tricks.
With
counter-reset
and this usefulnth-child
tip, I was able to make double digit ordered lists.With something along these lines…
I get the output:
PS: Chris, you need to fix the padding on your ordered lists (gets cut off starting with double digits):
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirtheen
OK, the numbers are removed after the comment is posted. On my PS above, I meant typing ordered lists in Markdown on the comments. Cheers.
Numbering In Style
Doh!