counter-reset CSS property

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

The counter-reset CSS property creates named CSS counters and initializes them to a specific value. It supports creating counters that count up from one to the number of elements, as well as those that count down from the number of elements to one.

Try it

counter-reset: none;
counter-reset: chapter-count 0;
counter-reset: chapter-count;
counter-reset: chapter-count 5;
counter-reset: chapter-count -5;
<section class="default-example" id="default-example">
  <div class="transition-all" id="chapters">
    <h1>Alice's Adventures in Wonderland</h1>
    <h2>Down the Rabbit-Hole</h2>
    <h2 id="example-element">The Pool of Tears</h2>
    <h2>A Caucus-Race and a Long Tale</h2>
    <h2>The Rabbit Sends in a Little Bill</h2>
  </div>
</section>
#default-example {
  text-align: left;
  counter-reset: chapter-count;
}

#example-element {
  background-color: lightblue;
  color: black;
}

h2 {
  counter-increment: chapter-count;
  font-size: 1em;
}

h2::before {
  content: "Chapter " counters(chapter-count, ".") ": ";
}

Syntax

css
/* Create a counter with initial default value 0 */
counter-reset: my-counter;

/* Create a counter and initialize as "-3" */
counter-reset: my-counter -3;

/* Create a reversed counter with initial default value */
counter-reset: reversed(my-counter);

/* Create a reversed counter and initialize as "-1" */
counter-reset: reversed(my-counter) -1;

/* Create reversed and regular counters at the same time */
counter-reset: reversed(pages) 10 items 1 reversed(sections) 4;

/* Remove all counter-reset declarations 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;

Values

The counter-reset property accepts a list of one or more space-separated counter names or the keyword none. For counter names, regular counters use the format <counter-name>, and reversed counters use reversed(<counter-name>), where <counter-name> is a <custom-ident> or list-item for the built-in <ol> counter. Optionally, each counter name can be followed by an <integer> to set its initial value.

<custom-ident>

Specifies the counter name to create and initialize using the <custom-ident> format. The reversed() functional notation can be used to mark the counter reversed.

<integer>

The initial value to set on the newly created counter. Defaults to 0 if not specified.

none

Specifies that no counter initialization should occur. This value is useful for overriding counter-reset values in less specific rules.

Description

The counter-reset property can create both regular and, in browsers that support it, reversed counters. You can create multiple regular and reversed counters, each separated by a space. Counters can be a standalone name or a space-separated name-value pair.

Warning: There is a difference between counter-reset and counter-set properties. After creating a counter using counter-reset, you can adjust its value by using the counter-set property. This is counterintuitive because, despite its name, the counter-reset property is used for creating and initializing counters, while the counter-set property is used for resetting the value of an existing counter.

Setting counter-increment: none on a selector with greater specificity overrides the creation of the named counter set on selectors with lower specificity.

Default initial values

The default initial values of both regular and reversed counters enable implementing 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, respectively. By including a counter value for a named counter, your counter can count up or down, starting at an integer value.

Regular counters default to 0 if no reset value is provided. By default, regular counters increment by one, which can be adjusted with the counter-increment property.

css
h1 {
  /* Create the counters "chapter" and "page" and set to initial default value.
     Create the counter "section" and set to "4". */
  counter-reset: chapter section 4 page;
}

Reversed counters

When creating reversed counters without a value, the counter will start with the value equal to the number of elements in the set, counting down so the last element in the set is 1. By default, reverse counters decrement by one; this can also be changed with the counter-increment property.

css
h1 {
  /* Create reversed counters "chapter" and "section".
      Set "chapter" as the number of elements and "section" as "10".
      Create the counter "pages" with the initial default value. */
  counter-reset: reversed(chapter) reversed(section) 10 pages;
}

Built-in list-item counter

Ordered lists (<ol>) come with built-in list-item counters that control their numbering. These counters automatically increase or decrease by one with each list item. The counter-reset property can be used to reset the list-item counters. Like with other counters, you can override the default increment value for list-item counters by using the counter-increment property.

Formal definition

Initial valuenone
Applies toall elements
Inheritedno
Computed valueas specified
Animation typeby computed value type

Formal syntax

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

<counter-name> =
<custom-ident>

<integer> =
<number-token>

<reversed-counter-name> =
reversed( <counter-name> )

Examples

Overriding the list-item counter

In this example, the counter-reset property is used to set a starting value for an implicit list-item counter.

HTML

html
<ol>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>Fourth</li>
  <li>Fifth</li>
</ol>

CSS

Using counter-reset, we set the implicit list-item counter to start at a value other than the default 1:

css
ol {
  counter-reset: list-item 3;
}

Result

Using counter-reset, we set the implicit list-item counter to start counting at 3 for every ol. Then, the first item would be numbered 4, second would be numbered 5, etc., similar to the effect of writing <ol start="4"> in HTML.

Using a reverse counter

In the following example, we've declared a reversed counter named 'priority'. The counter is being used to number five tasks.

html
<ul class="stack">
  <li>Task A</li>
  <li>Task B</li>
  <li>Task C</li>
  <li>Task D</li>
  <li>Task E</li>
</ul>
css
li::before {
  content: counter(priority) ". ";
  counter-increment: priority -1;
}

.stack {
  counter-reset: reversed(priority);
  list-style: none;
}

In the output, the items are numbered in reversed order from 5 to 1. Notice in the code we haven't specified the counter's initial value. The browser automatically calculates the initial value at layout-time using the counter increment value.

Specifications

Specification
CSS Lists and Counters Module Level 3
# counter-reset

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
counter-reset
Chrome – Full support
Chrome 2 (Release date: 2009-05-21)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 9.2 (Release date: 2007-04-11)
footnote Full support
Safari – Full support
Safari 3 (Release date: 2007-10-26)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 25 (Release date: 2013-10-29)
footnote Full support
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
list-item
Chrome – Partial support
Chrome 28 (Release date: 2013-07-09)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter has no effect when the default marker string for list items (::marker) is generated; see bug 338233131.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Partial support
Opera 15 (Release date: 2013-07-02)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter has no effect when the default marker string for list items (::marker) is generated; see bug 338233131.
Safari – Partial support
Safari 9 (Release date: 2015-09-30)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter results in incorrect values for the counter() function used to generate content, as it is not fully implemented; see bug 260436.
Chrome Android – Partial support
Chrome Android 28 (Release date: 2013-07-10)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter has no effect when the default marker string for list items (::marker) is generated; see bug 338233131.
Firefox for Android – Full support
Firefox for Android 25 (Release date: 2013-10-29)
footnote Full support
Opera Android – Partial support
Opera Android 15 (Release date: 2013-07-08)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter has no effect when the default marker string for list items (::marker) is generated; see bug 338233131.
Safari on iOS – Partial support
Safari on iOS 9 (Release date: 2015-09-16)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter results in incorrect values for the counter() function used to generate content, as it is not fully implemented; see bug 260436.
Samsung Internet – Partial support
Samsung Internet 1.5 (Release date: 2013-09-25)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter has no effect when the default marker string for list items (::marker) is generated; see bug 338233131.
WebView Android – Partial support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter has no effect when the default marker string for list items (::marker) is generated; see bug 338233131.
WebView on iOS – Partial support
WebView on iOS 9 (Release date: 2015-09-16)
footnote Partial support
footnote Overriding the initial value of the implicit list-item counter results in incorrect values for the counter() function used to generate content, as it is not fully implemented; see bug 260436.
none
Chrome – Full support
Chrome 2 (Release date: 2009-05-21)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 3 (Release date: 2007-10-26)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 25 (Release date: 2013-10-29)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 2 (Release date: 2008-07-11)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 2 (Release date: 2008-07-11)
footnote Full support
Resets counter on current element (not sibling elements).
Experimental
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
reversed()
Experimental
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Full support
Firefox 96 (Release date: 2022-01-11)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – Full support
Firefox for Android 96 (Release date: 2022-01-11)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Experimental. Expect behavior to change in the future.

See also