CSS counter-reset Property
The counter-reset property creates a named CSS counter and initializes it to a specified integer value.
CSS counters provide automatic numbering for headings, lists, steps, figures, and other repeated content. The counter-reset property establishes the counter, counter-increment changes its value, and the counter() or counters() function displays the result.
By default, a counter created with counter-reset starts at 0. You can specify a different starting value, create multiple counters, or use the reversed() notation to create a counter that counts backward.
counter-reset Syntax
The counter-reset property accepts one or more counter names, each optionally followed by an integer that specifies the starting value.
selector {
counter-reset: counter-name;
}
General Syntax
selector {
counter-reset: counter-name integer;
}
Initialize at Zero
.steps {
counter-reset: step;
}
Initialize at Five
.steps {
counter-reset: step 5;
}
Initialize Multiple Counters
.document {
counter-reset: section 0 example 0;
}
Create a Reversed Counter
.countdown {
counter-reset: reversed(step) 5;
}
Disable Explicit Counter Resets
.steps {
counter-reset: none;
}
CSS-Wide Keywords
.steps {
counter-reset: inherit;
}
.steps {
counter-reset: initial;
}
.steps {
counter-reset: unset;
}
.steps {
counter-reset: revert;
}
.steps {
counter-reset: revert-layer;
}
Counter names are case-sensitive identifiers. Starting values must be integers, and the default starting value is 0 when no integer is supplied.
Property Values
| Value | Description |
|---|---|
none |
Creates no counters through this declaration. This is the initial value. |
counter-name |
Creates a named counter initialized to 0. |
counter-name integer |
Creates a named counter initialized to the specified integer. |
counter-name -integer |
Creates a named counter initialized to a negative integer. |
reversed(counter-name) |
Creates a reversed counter, with its initial value determined automatically when omitted. |
reversed(counter-name) integer |
Creates a reversed counter with an explicit initial value. |
| Multiple counter definitions | Creates several counters in one declaration. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | All elements |
| Computed value | Specified counter names, reversal status, and integer values |
| Animation type | By computed value type |
| Percentages | Not applicable |
The property establishes counter instances and their initial values. It does not display numbers or automatically increase ordinary named counters.
How It Works
CSS counters use three basic operations: creating a counter, changing its value, and displaying that value.
Step 1: Create the Counter
.steps {
counter-reset: step;
}
This creates a counter named step with an initial value of 0.
Step 2: Increment the Counter
.steps .step {
counter-increment: step;
}
Each matching element increases the counter by 1.
Step 3: Display the Counter
.steps .step::before {
content: "Step " counter(step) ": ";
}
Step 4: Add the HTML
<div class="steps">
<p class="step">Create the HTML structure.</p>
<p class="step">Add the CSS styles.</p>
<p class="step">Preview the result.</p>
</div>
Step 5: View the Result
The browser generates Step 1, Step 2, and Step 3 before the corresponding paragraphs.
Important Distinction
The counter is maintained by CSS. Adding or removing matching elements automatically updates the numbering without changing the HTML text.
Default Starting Value
When a counter name is specified without an integer, the counter is initialized to 0.
Basic Declaration
.items {
counter-reset: item;
}
Equivalent Declaration
.items {
counter-reset: item 0;
}
First Displayed Value
.items {
counter-reset: item;
}
.items .item {
counter-increment: item;
}
.items .item::before {
content: counter(item) ". ";
}
The first item displays 1 because the counter increases before its value is displayed.
Display Without Incrementing
.items {
counter-reset: item;
}
.items::before {
content: "Starting value: " counter(item);
}
This displays the initial value of 0.
Practical Consideration
Starting at 0 is appropriate for most numbering sequences because the first increment produces 1.
Custom Starting Values
Specify an integer after the counter name to initialize a counter to a value other than 0.
Start at Five
.items {
counter-reset: item 5;
}
Continue Numbering at Six
.items {
counter-reset: item 5;
}
.items .item {
counter-increment: item;
}
.items .item::before {
content: counter(item) ". ";
}
The first item displays 6, followed by 7, 8, and so on.
Start Displayed Numbering at Ten
.items {
counter-reset: item 9;
}
.items .item {
counter-increment: item;
}
The first increment changes the counter from 9 to 10.
Custom Numbering for Chapters
.chapters {
counter-reset: chapter 3;
}
.chapters h2 {
counter-increment: chapter;
}
.chapters h2::before {
content: "Chapter " counter(chapter) ": ";
}
The first heading displays Chapter 4.
Important Consideration
Remember that counter-reset establishes the starting value, while counter-increment determines the value displayed on each numbered element.
Negative Starting Values
A counter can be initialized to a negative integer.
Initialize at Negative Five
.items {
counter-reset: item -5;
}
Increment Toward Zero
.items {
counter-reset: item -5;
}
.items .item {
counter-increment: item;
}
.items .item::before {
content: counter(item) ". ";
}
The displayed values begin at -4, followed by -3, -2, and so on.
Count Down from Zero
.items {
counter-reset: item 0;
}
.items .item {
counter-increment: item -1;
}
The counter decreases to -1, -2, -3, and so on.
Use a Positive Starting Value for a Countdown
.countdown {
counter-reset: count 6;
}
.countdown .item {
counter-increment: count -1;
}
Five items display 5, 4, 3, 2, and 1 when the counter is shown with counter(count).
Practical Consideration
Negative starting values and negative increments are separate features. Use them together or independently depending on the required numbering sequence.
Multiple Counters
A single counter-reset declaration can create multiple counters.
Initialize Two Counters
.document {
counter-reset: section example;
}
Both counters start at 0.
Different Starting Values
.document {
counter-reset: section 0 example 5;
}
Three Independent Counters
.document {
counter-reset: chapter 0 figure 0 table 0;
}
Increment Different Counters
.document h2 {
counter-increment: chapter;
}
.document figure {
counter-increment: figure;
}
.document table {
counter-increment: table;
}
Display Their Values
.document h2::before {
content: "Chapter " counter(chapter) ": ";
}
.document figcaption::before {
content: "Figure " counter(figure) ": ";
}
Important Consideration
Each counter maintains its own value. Creating several counters in one declaration does not make their values dependent on one another.
Reversed Counters
The reversed() notation creates a counter intended for descending numbering.
Basic Syntax
.countdown {
counter-reset: reversed(step);
}
When no starting integer is provided, the browser determines the initial value based on the number of elements that increment or decrement the reversed counter within its scope.
Explicit Starting Value
.countdown {
counter-reset: reversed(step) 5;
}
Reversed Counter Example
.countdown {
counter-reset: reversed(step) 5;
}
.countdown .item {
counter-increment: step -1;
}
.countdown .item::before {
content: counter(step) ". ";
}
With five items, this explicit starting value produces 4, 3, 2, 1, and 0.
Automatic Descending Numbering
.countdown {
counter-reset: reversed(step);
}
.countdown .item {
counter-increment: step -1;
}
.countdown .item::before {
content: counter(step) ". ";
}
With five matching items, the automatically determined starting value is 5, producing 4, 3, 2, 1, and 0 after each decrement.
Browser Compatibility
The reversed() notation is newer than basic CSS counter support. Check browser compatibility before relying on it.
Practical Alternative
For predictable descending numbering with a known starting value, a regular counter with an explicit starting value and a negative increment is often sufficient.
Using counter-increment
The counter-increment property changes the value of a counter created with counter-reset.
Basic Increment
.items {
counter-reset: item;
}
.items .item {
counter-increment: item;
}
Increase by Two
.items .item {
counter-increment: item 2;
}
Decrease by One
.items .item {
counter-increment: item -1;
}
No Numeric Change
.items .item {
counter-increment: item 0;
}
Reset and Increment on the Same Element
.item {
counter-reset: number 10;
counter-increment: number;
}
The counter is initialized to 10 and then incremented to 11 on that element.
Important Distinction
counter-reset creates a new counter instance, while counter-increment changes the value of an existing counter or implicitly creates one when necessary.
Displaying Counter Values
Creating a counter does not automatically display its value. Use the counter() or counters() function with generated content.
Basic Counter Display
.item::before {
content: counter(item);
}
Add Punctuation
.item::before {
content: counter(item) ". ";
}
Add a Text Label
.item::before {
content: "Item " counter(item) ": ";
}
Display Roman Numerals
.item::before {
content: counter(item, upper-roman) ". ";
}
Display Letters
.item::before {
content: counter(item, lower-alpha) ". ";
}
Display Nested Counter Values
.nested-item::before {
content: counters(section, ".") " ";
}
Important Distinction
The counter() function displays the innermost active value of a named counter. The counters() function combines values from multiple nested counter instances with the same name.
Counter Scope
CSS counters operate within scopes that determine where their values are available.
Counter Scope in a Container
.group {
counter-reset: item;
}
.group .item {
counter-increment: item;
}
Separate Groups
<div class="group">
<p class="item">First item</p>
<p class="item">Second item</p>
</div>
<div class="group">
<p class="item">First item</p>
<p class="item">Second item</p>
</div>
Each group creates its own counter, so numbering begins at 1 in both groups.
Nested Counter Scopes
.outline {
counter-reset: section;
}
.outline .subsection-group {
counter-reset: subsection;
}
Resetting Within a Section
.document {
counter-reset: chapter;
}
.document .chapter {
counter-increment: chapter;
counter-reset: example;
}
.document .example {
counter-increment: example;
}
Each chapter creates a new example counter, allowing example numbering to restart within that chapter.
Important Consideration
Counter scopes are not the same as CSS inheritance. Although counter-reset is not inherited, descendant elements can access counters established in an applicable ancestor scope.
Nested Counters
Nested counters support hierarchical numbering, such as 1, 1.1, 1.2, and 2.
HTML Structure
<ol class="nested-outline">
<li>Introduction
<ol>
<li>Overview</li>
<li>Requirements</li>
</ol>
</li>
<li>Examples
<ol>
<li>Basic Example</li>
<li>Advanced Example</li>
</ol>
</li>
</ol>
CSS Counter Rules
.nested-outline,
.nested-outline ol {
counter-reset: section;
list-style: none;
}
.nested-outline li {
counter-increment: section;
}
.nested-outline li::before {
content: counters(section, ".") " ";
font-weight: bold;
}
Generated Numbering
The outer list displays 1 and 2. Nested items display values such as 1.1, 1.2, 2.1, and 2.2.
How It Works
Each nested list creates a new counter instance named section. The counters() function combines the active counter values into a hierarchical label.
Practical Consideration
Use nested counters for document outlines, structured lists, and hierarchical numbering. Keep the HTML structure meaningful independently of the generated labels.
Numbering Headings
The counter-reset property can initialize counters used to number headings automatically.
Initialize the Counter
.numbered-article {
counter-reset: section;
}
Increment Each Heading
.numbered-article h2 {
counter-increment: section;
}
Display the Number
.numbered-article h2::before {
content: counter(section) ". ";
}
Complete Example
.numbered-article {
counter-reset: section;
}
.numbered-article h2 {
counter-increment: section;
}
.numbered-article h2::before {
content: "Section " counter(section) ": ";
color: #336699;
}
HTML Structure
<article class="numbered-article">
<h2>Introduction</h2>
<h2>Syntax</h2>
<h2>Examples</h2>
</article>
Practical Consideration
CSS-generated heading numbers are useful for presentation, but heading levels should still reflect the document's semantic structure.
Numbering Lists
CSS counters can create custom numbering for ordered lists and other repeated items.
Custom Ordered List
.custom-list {
counter-reset: item;
list-style: none;
}
.custom-list li {
counter-increment: item;
}
.custom-list li::before {
content: counter(item) ". ";
font-weight: bold;
}
Custom Step Labels
.step-list {
counter-reset: step;
list-style: none;
}
.step-list li {
counter-increment: step;
}
.step-list li::before {
content: "Step " counter(step) ": ";
}
Restart Numbering in Each List
.numbered-list {
counter-reset: item;
}
.numbered-list li {
counter-increment: item;
}
Built-In List Counters
HTML ordered lists already use a built-in list-item counter. Custom counters are not required for ordinary list numbering.
When to Use Native Lists
Use <ol> and <li> for meaningful ordered sequences. Custom CSS counters are most useful when special numbering or formatting is required.
counter-reset vs. counter-set
The counter-reset and counter-set properties both assign values to counters, but they differ in how they handle counter scopes.
counter-reset
.group {
counter-reset: item 0;
}
Creates a new counter instance initialized to 0.
counter-set
.item {
counter-set: item 10;
}
Sets the value of an existing counter to 10. If no applicable counter exists, a new counter is created.
counter-increment
.item {
counter-increment: item 1;
}
Adds 1 to the current counter value.
Comparison Table
| Property | Purpose | Example |
|---|---|---|
counter-reset |
Creates a new counter scope with an initial value | counter-reset: item 0; |
counter-set |
Assigns a value to an existing counter when available | counter-set: item 10; |
counter-increment |
Adds to or subtracts from a counter | counter-increment: item 1; |
Important Distinction
Use counter-reset when establishing a new numbering scope. Use counter-set when changing the current value without intentionally starting a new scope.
Practical Uses
The counter-reset property is useful for creating independent numbering sequences throughout a page.
Tutorial Steps
.tutorial-steps {
counter-reset: step;
}
.tutorial-steps .step {
counter-increment: step;
}
.tutorial-steps .step::before {
content: "Step " counter(step) ": ";
}
Numbered Examples
.examples {
counter-reset: example;
}
.examples .example {
counter-increment: example;
}
.examples .example::before {
content: "Example " counter(example) ": ";
}
Figure Numbering
.figures {
counter-reset: figure;
}
.figures figure {
counter-increment: figure;
}
.figures figcaption::before {
content: "Figure " counter(figure) ": ";
}
Chapter Numbering
.chapters {
counter-reset: chapter;
}
.chapters h2 {
counter-increment: chapter;
}
.chapters h2::before {
content: "Chapter " counter(chapter) ": ";
}
Independent Numbered Groups
.card-group {
counter-reset: card;
}
.card-group .card {
counter-increment: card;
}
.card-group .card::before {
content: counter(card, decimal-leading-zero);
}
Practical Consideration
Place counter-reset on the container that should own the numbering sequence. This makes it easier to control where numbering starts and restarts.
counter-reset Example
This example demonstrates two independent groups of numbered tutorial steps.
Each group uses counter-reset: step to start a new counter at 0. The steps then increase the counter by 1 and display their values using counter(step).
<div class="counter-reset-demo">
<div class="demo-group">
<h3>HTML Basics</h3>
<p class="demo-step">Create the document structure.</p>
<p class="demo-step">Add headings and paragraphs.</p>
<p class="demo-step">Save the HTML file.</p>
</div>
<div class="demo-group">
<h3>CSS Basics</h3>
<p class="demo-step">Create a stylesheet.</p>
<p class="demo-step">Add selectors and declarations.</p>
<p class="demo-step">Link the stylesheet to the HTML.</p>
</div>
</div>
.counter-reset-demo {
display: grid;
gap: 20px;
}
.counter-reset-demo .demo-group {
counter-reset: step;
padding: 20px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.counter-reset-demo h3 {
margin: 0 0 15px;
color: #336699;
font-size: 1.125rem;
}
.counter-reset-demo .demo-step {
counter-increment: step;
margin: 0 0 10px;
}
.counter-reset-demo .demo-step:last-child {
margin-bottom: 0;
}
.counter-reset-demo .demo-step::before {
content: counter(step) ". ";
font-weight: bold;
color: #336699;
}
HTML Basics
Create the document structure.
Add headings and paragraphs.
Save the HTML file.
CSS Basics
Create a stylesheet.
Add selectors and declarations.
Link the stylesheet to the HTML.
Both groups begin numbering at 1 because each .demo-group creates a separate counter instance.
The counter-increment: step declaration increases the active counter for each paragraph, and counter(step) displays the current value.
Additional groups can be added without affecting the numbering of existing groups.
Accessibility
CSS counters can improve visual organization, but important numbering should remain understandable to users who do not receive generated CSS content.
Use Semantic Lists
When content represents an ordered sequence, prefer HTML <ol> and <li> elements.
Preserve Heading Hierarchy
Generated heading numbers do not establish semantic heading levels. Use appropriate HTML heading elements.
Generated Content and Assistive Technology
Screen readers may announce CSS-generated counter values differently depending on the browser and assistive technology combination.
Essential Numbering
If users must reference a specific numbered step or section, consider including that number in the HTML or using native ordered-list numbering.
Avoid Duplicate Numbering
When adding custom numbers to lists, check that native list markers do not create confusing duplicate labels.
Test Without Styles
Ensure that the document remains understandable when CSS is disabled or unavailable.
Maintain Readable Contrast
Generated numbers should have sufficient contrast and remain readable when users zoom or increase text size.
Important Considerations
Keep the following points in mind when using counter-reset:
- Purpose: Creates a new named CSS counter and initializes its value.
- Initial property value:
none. - Inheritance: The property is not inherited.
- Default counter value: A newly created ordinary counter starts at 0 unless another integer is specified.
- Custom values: Positive and negative integers can be used.
- Multiple counters: Several counters can be created in one declaration.
- Counter names: Names are case-sensitive identifiers.
- Counter scopes: Each reset creates a new counter instance in the applicable scope.
- Nested counters: Nested scopes support hierarchical numbering.
- Reversed counters: The
reversed()notation creates counters intended for descending sequences. - Automatic increments: Ordinary named counters do not increase merely because they were created.
- counter-increment: Changes the current counter value by an integer amount.
- counter-set: Assigns a value to an existing counter when available.
- Displaying values: Use
counter()orcounters()with generated content. - List numbering: HTML ordered lists already provide built-in numbering.
- Automatic updates: Generated numbering adjusts when matching elements are added or removed.
- Accessibility: Essential numbering should not rely exclusively on CSS-generated content.
- Browser support: Basic counter resets are widely supported; newer reversed-counter syntax requires separate compatibility checks.
Place counter-reset on the element that should establish a numbering sequence. Combine it with counter-increment and generated content to produce automatic numbering that remains consistent as the document changes.
Browser Support
The CSS counter-reset property is widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.
Basic named counters, custom integer starting values, multiple counter definitions, and nested counter scopes have broad browser support.
The newer reversed() notation has different compatibility requirements and should be checked separately before production use.
Basic Example
.steps {
counter-reset: step;
}
.steps .step {
counter-increment: step;
}
.steps .step::before {
content: counter(step) ". ";
}
Feature Detection
@supports (counter-reset: step 0) {
.steps {
counter-reset: step 0;
}
}
Reversed Counter Feature Detection
@supports (counter-reset: reversed(step)) {
.countdown {
counter-reset: reversed(step);
}
}
For current compatibility information, visit Can I Use? .
Specifications
The counter-reset property is defined in CSS Lists and Counters Module Level 3.
CSS Lists and Counters Module Level 3
The specification defines counter creation, initial values, counter scopes, reversed counters, and the interaction between CSS counters and generated content.
