css properties counter-reset

The counter-reset CSS property resets a CSS counter to a given value. This property will create a new counter or reversed counter with the given name on the specified element.

Normal counters have a default initial value of 0. Reversed counters are intended to count down, and have a default initial value set to the number of elements at the current level. The default initial values make it easy to implement the two most common numbering patterns: counting up from one to the number of elements, and counting down from the number of elements to one.

A counter's value is increased or decreased using the counter-increment CSS property, and the value of an existing counter may be set using counter-set.

In addition to author-created counters, the property can also reset the list-item counters used by ordered lists (as created using <ol> elements). These have the same behavior as author-created counters, except they are automatically incremented/decremented by one with each list element. This behavior can be overridden using counter-increment.

Syntax

/* Set "my-counter" to 0 */
counter-reset: my-counter;

/* Set "my-counter" to -3 */
counter-reset: my-counter -3;

/* Set reversed "my-counter" to "the number of peer elements" */
counter-reset: reversed(my-counter);

/* Set reversed "my-counter" to -1 */
counter-reset: reversed(my-counter) -1;

/* Set counter2 to 9 and reversed "counter1" and "counter3" to 1 and 4, respectively*/
counter-reset: reversed(counter1) 1 counter2 9 reversed(counter3) 4;

/* Cancel any reset that could have been set in less specific rules */
counter-reset: none;

/* Global values */
counter-reset: inherit;
counter-reset: initial;
counter-reset: revert;
counter-reset: revert-layer;
counter-reset: unset;

The counter-reset property is specified as either one of the following:

The "implicit" counter named list-item can be used to control the numbering for ordered lists, as created using <ol>

Values

<custom-ident>

The name of the counter to reset.

<integer>

The value to reset the counter to on each occurrence of the element. Defaults to 0 if not specified.

none

No counter reset is to be performed. This can be used to override a counter-reset defined in a less specific rule.

Formal definition

Initial valuenone
Applies toall elements
Inheritedno
Computed valuethe keyword none or a list, each item an identifier or a reversed() function paired with an integer
Animation typeby computed value type

Formal syntax

[ <counter-name> <integer>? | <reversed-counter-name> <integer>? ]+ | none

Examples

The following examples show how to reset the counters, but not how they are incremented, decremented, and displayed.

For more-complete examples see Using CSS Counters.

Resetting named counters

h1 {
  counter-reset: chapter section 1 page;
  /* Sets the chapter and page counters to 0,
     and the section counter to 1 */
}

Reversing a counter

h1 {
  counter-reset: reversed(chapter) reversed(section) 1 page;
  /* Sets the reversed flag on the chapter and section counters.
     Sets the chapter to the number of elements, the section counter to 1,
     and the page counters to 0*/
}

See also