CSS counter-increment Property
The counter-increment property increases or decreases the value of a named CSS counter as elements are processed in document order.
CSS counters are useful for automatically numbering headings, steps, examples, figures, and other repeated content. The counter-increment property updates a counter, while counter-reset initializes it and the counter() or counters() function displays its value.
By default, a counter increases by 1 each time a matching element is encountered. You can specify a different integer, use a negative value to count backward, or update multiple counters in one declaration.
counter-increment Syntax
The counter-increment property accepts one or more counter names, each optionally followed by an integer indicating how much the counter should change.
selector {
counter-increment: counter-name;
}
General Syntax
selector {
counter-increment: counter-name integer;
}
Increase by One
.step {
counter-increment: step;
}
Increase by Two
.step {
counter-increment: step 2;
}
Decrease by One
.step {
counter-increment: step -1;
}
Update Multiple Counters
.item {
counter-increment: section 1 example 2;
}
Disable Explicit Increments
.step {
counter-increment: none;
}
CSS-Wide Keywords
.step {
counter-increment: inherit;
}
.step {
counter-increment: initial;
}
.step {
counter-increment: unset;
}
.step {
counter-increment: revert;
}
.step {
counter-increment: revert-layer;
}
Counter names are case-sensitive identifiers. The integer must be a whole number, and the default increment is 1 when no integer is supplied.
Property Values
| Value | Description |
|---|---|
none |
Specifies no explicit counter increments. This is the initial value. |
counter-name |
Increases the named counter by 1. |
counter-name integer |
Changes the named counter by the specified integer. |
counter-name -integer |
Decreases the named counter by the specified amount. |
counter-name 0 |
Leaves the counter's numeric value unchanged. |
| Multiple counter names | Updates several named counters in one declaration. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | All elements |
| Computed value | Specified counter names and integer values |
| Animation type | By computed value type |
| Percentages | Not applicable |
The counter-increment property changes counter values but does not display them. Use the content property with counter() or counters() to show the result.
How It Works
CSS counters operate through a sequence of initialization, incrementing, and displaying values.
Step 1: Initialize a Counter
.steps {
counter-reset: step;
}
The counter starts at 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 HTML Elements
<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 Numbering
The generated labels appear as Step 1, Step 2, and Step 3.
Important Distinction
The counter value is maintained by CSS. Adding or removing matching elements automatically updates the displayed numbering.
Default Increment
When a counter name is specified without an integer, the counter increases by 1.
Basic Declaration
.item {
counter-increment: item;
}
Equivalent Declaration
.item {
counter-increment: item 1;
}
Complete Example
.numbered-items {
counter-reset: item;
}
.numbered-items .item {
counter-increment: item;
}
.numbered-items .item::before {
content: counter(item) ". ";
}
Result
Each matching item receives the next counter value, beginning with 1.
Practical Consideration
The default increment is suitable for most sequential numbering, including steps, examples, and section labels.
Custom Increment Values
Specify an integer after the counter name to control how much the counter changes.
Increase by Two
.item {
counter-increment: item 2;
}
Starting from 0, the displayed values become 2, 4, 6, and so on.
Increase by Five
.item {
counter-increment: item 5;
}
Starting from 0, the displayed values become 5, 10, 15, and so on.
No Numeric Change
.item {
counter-increment: item 0;
}
The counter value remains unchanged.
Different Increments for Different Elements
.item {
counter-increment: item 1;
}
.item.double {
counter-increment: item 2;
}
Important Consideration
Only integer values are accepted. Decimal values such as 1.5 are invalid for counter-increment.
Negative Increments
A negative integer decreases the value of a CSS counter.
Decrease by One
.item {
counter-increment: item -1;
}
Countdown Example
.countdown {
counter-reset: count 6;
}
.countdown .item {
counter-increment: count -1;
}
.countdown .item::before {
content: counter(count) ". ";
}
HTML Structure
<div class="countdown">
<p class="item">First item</p>
<p class="item">Second item</p>
<p class="item">Third item</p>
<p class="item">Fourth item</p>
<p class="item">Fifth item</p>
</div>
Result
The items display values of 5, 4, 3, 2, and 1.
Decrease by Two
.item {
counter-increment: count -2;
}
Practical Consideration
Negative increments are useful for countdowns, reverse numbering, and custom sequences. For ordinary reversed ordered lists, consider the HTML reversed attribute instead.
Multiple Counters
A single counter-increment declaration can update multiple counters.
Basic Syntax
.item {
counter-increment: section 1 example 1;
}
Different Increment Amounts
.item {
counter-increment: section 1 example 2;
}
Positive and Negative Increments
.item {
counter-increment: completed 1 remaining -1;
}
Initialize Multiple Counters
.document {
counter-reset: section 0 example 0;
}
Display Multiple Counters
.item::before {
content: "Section " counter(section) ", Example " counter(example) ": ";
}
Important Consideration
Each counter is tracked independently. Changing one counter does not automatically change another unless both are included in the applicable declarations.
Using counter-reset
The counter-reset property establishes a counter and sets its starting value.
Initialize at Zero
.items {
counter-reset: item;
}
Initialize at Ten
.items {
counter-reset: item 10;
}
Increment the Counter
.items .item {
counter-increment: item;
}
Display the Value
.items .item::before {
content: counter(item) ". ";
}
Starting Value Behavior
If the counter starts at 10 and increments by 1 before being displayed, the first generated value is 11.
Reset Numbering in Separate Groups
.item-group {
counter-reset: item;
}
.item-group .item {
counter-increment: item;
}
Each group starts its own numbering sequence.
Important Consideration
A counter can also be created implicitly when an increment refers to a counter that does not yet exist in the relevant scope. Explicitly using counter-reset makes the intended numbering structure clearer.
Displaying Counter Values
The counter-increment property updates a counter but does not display it automatically.
Use the counter() function with the content property to insert the current value into generated content.
Basic Counter Display
.item::before {
content: counter(item);
}
Add Punctuation
.item::before {
content: counter(item) ". ";
}
Add a Label
.item::before {
content: "Example " counter(item) ": ";
}
Use Roman Numerals
.item::before {
content: counter(item, upper-roman) ". ";
}
Use Letters
.item::before {
content: counter(item, lower-alpha) ". ";
}
Display Nested Values
.nested-item::before {
content: counters(section, ".") " ";
}
Important Distinction
The counter() function displays a single counter value. The counters() function combines values from nested counters with the same name.
Nested Counters
Nested counters allow 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 uses values 1 and 2, while nested items display hierarchical values such as 1.1, 1.2, 2.1, and 2.2.
How Nested Scopes Work
Each nested list creates a new counter scope with the same name. The counters() function combines the active counter values into a single hierarchical label.
Practical Consideration
Nested CSS counters are useful for outlines, documentation, and structured examples. Preserve semantic HTML nesting when the hierarchy has meaning.
Numbering Headings
CSS counters can automatically number headings without manually entering numbers in the HTML.
Initialize a Section 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
Generated heading numbers are useful for visual presentation, but the underlying heading structure should remain meaningful without CSS.
Numbering Lists
CSS counters can create custom numbering for list items.
Custom List Numbering
.custom-list {
counter-reset: item;
list-style: none;
}
.custom-list li {
counter-increment: item;
}
.custom-list li::before {
content: counter(item) ". ";
font-weight: bold;
color: #336699;
}
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) ": ";
font-weight: bold;
}
Counter-Based List Markers
.numbered-list li::marker {
content: counter(list-item) ". ";
}
Ordered lists have a built-in list-item counter that can be used for custom marker formatting.
When to Use Native Numbering
For ordinary numbered lists, native <ol> and <li> elements are usually simpler and provide appropriate list semantics.
Practical Consideration
Use custom counters when you need special numbering styles, prefixes, or hierarchical formats that are not easily achieved with standard list markers.
Counter Styles
The counter-increment property changes the numeric value of a counter. The counter() function determines how that value is displayed.
Decimal Numbering
.item::before {
content: counter(item, decimal) ". ";
}
Uppercase Roman Numerals
.item::before {
content: counter(item, upper-roman) ". ";
}
Lowercase Roman Numerals
.item::before {
content: counter(item, lower-roman) ". ";
}
Uppercase Letters
.item::before {
content: counter(item, upper-alpha) ". ";
}
Lowercase Letters
.item::before {
content: counter(item, lower-alpha) ". ";
}
Leading Zeros
.item::before {
content: counter(item, decimal-leading-zero) ". ";
}
Custom Counter Styles
@counter-style circled-numbers {
system: fixed;
symbols: "①" "②" "③" "④" "⑤";
suffix: " ";
}
.item::before {
content: counter(item, circled-numbers);
}
Important Distinction
Changing the display style does not change the underlying counter value. The same counter can be displayed using different numbering formats.
Counter Scope
CSS counters have scopes that determine where their values are available and how nested counters interact.
Counter in a Container
.group {
counter-reset: item;
}
.group .item {
counter-increment: item;
}
Separate Counter Groups
<div class="group">
<p class="item">First group item</p>
<p class="item">Second group item</p>
</div>
<div class="group">
<p class="item">Another first item</p>
<p class="item">Another second item</p>
</div>
Each group establishes its own counter, so both groups begin numbering at 1.
Nested Counter Scope
.outline {
counter-reset: section;
}
.outline .subsection-group {
counter-reset: subsection;
}
.outline .section {
counter-increment: section;
}
.outline .subsection {
counter-increment: subsection;
}
Implicit Counter Creation
If counter-increment references a counter that does not exist in the applicable scope, the browser creates one with an initial value of 0 before applying the increment.
Practical Consideration
Define counter scopes deliberately to avoid unexpected numbering when components are repeated or nested.
counter-increment vs. counter-set
The counter-increment and counter-set properties both affect CSS counters, but they perform different operations.
counter-increment
.item {
counter-increment: item 2;
}
Adds 2 to the counter's current value.
counter-set
.item {
counter-set: item 10;
}
Sets the counter to 10 rather than adding to its previous value.
counter-reset
.group {
counter-reset: item 0;
}
Creates a new counter scope initialized to the specified value.
Comparison Table
| Property | Purpose | Example |
|---|---|---|
counter-reset |
Creates and initializes a counter | counter-reset: item 0; |
counter-increment |
Adds to or subtracts from a counter | counter-increment: item 1; |
counter-set |
Sets a counter to a specified value | counter-set: item 10; |
Practical Consideration
Use counter-reset to establish numbering, counter-increment to advance it, and counter-set when an existing counter must be assigned a particular value.
Practical Uses
The counter-increment property is useful wherever repeated content needs automatically maintained numbering.
Numbered 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) ": ";
}
Section Numbering
.reference {
counter-reset: section;
}
.reference h2 {
counter-increment: section;
}
.reference h2::before {
content: counter(section) ". ";
}
Custom Numbered Cards
.cards {
counter-reset: card;
}
.cards .card {
counter-increment: card;
}
.cards .card::before {
content: counter(card, decimal-leading-zero);
font-weight: bold;
color: #336699;
}
When Not to Use
Avoid using CSS counters as the only source of essential numbering when the numbers must be available in copied text, exported content, or environments without CSS.
counter-increment Example
This example demonstrates automatic numbering for tutorial steps using counter-reset, counter-increment, and counter().
Each step increases the counter by 1, and the generated number appears inside a circular label.
<div class="counter-demo">
<div class="demo-step">
<h3>Create the HTML</h3>
<p>Add the elements and structure for your page.</p>
</div>
<div class="demo-step">
<h3>Write the CSS</h3>
<p>Define the styles and counter rules.</p>
</div>
<div class="demo-step">
<h3>Preview the Result</h3>
<p>Check the layout and generated numbering.</p>
</div>
<div class="demo-step">
<h3>Refine the Design</h3>
<p>Adjust spacing, colors, and typography.</p>
</div>
</div>
.counter-demo {
counter-reset: step;
display: grid;
gap: 15px;
}
.counter-demo .demo-step {
counter-increment: step;
position: relative;
min-height: 75px;
padding: 15px 15px 15px 65px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.counter-demo .demo-step::before {
content: counter(step);
position: absolute;
top: 15px;
left: 15px;
display: flex;
align-items: center;
justify-content: center;
width: 35px;
height: 35px;
border-radius: 50%;
color: #ffffff;
background-color: #336699;
font-weight: bold;
}
.counter-demo h3 {
margin: 0 0 5px;
font-size: 1.125rem;
}
.counter-demo p {
margin: 0;
}
Create the HTML
Add the elements and structure for your page.
Write the CSS
Define the styles and counter rules.
Preview the Result
Check the layout and generated numbering.
Refine the Design
Adjust spacing, colors, and typography.
The counter-reset: step declaration initializes the counter to 0 on the container.
Each .demo-step element increases the counter by 1 using counter-increment: step.
The ::before pseudo-element displays the current value with counter(step) and styles it as a numbered circle.
If another step is added, the browser automatically continues the numbering sequence without requiring manual changes to the HTML.
Accessibility
CSS counters can improve visual organization, but generated numbering should not replace meaningful document structure.
Use Semantic Lists
When items represent an ordered sequence, use HTML <ol> and <li> elements whenever possible.
Preserve Heading Structure
Use appropriate heading levels for numbered sections. CSS-generated numbers do not establish heading hierarchy.
Generated Content and Screen Readers
Assistive technologies may announce generated counter values differently depending on the browser and screen reader combination.
Essential Numbering
If a number is required to understand instructions or identify a specific item, consider including it in the HTML rather than relying exclusively on CSS.
Avoid Duplicate Announcements
When customizing ordered list markers, test that users do not encounter confusing or duplicated numbering.
Visual Contrast
Ensure that generated numbers have sufficient contrast against their backgrounds and remain readable at increased zoom levels.
Test Without CSS
Check that the underlying document remains understandable when styles are unavailable.
Important Considerations
Keep the following points in mind when using counter-increment:
- Purpose: Increases or decreases a named CSS counter.
- Initial value:
none. - Inheritance: The property is not inherited.
- Default increment: A named counter increases by 1 when no integer is supplied.
- Custom increments: Positive integers increase the counter by the specified amount.
- Negative increments: Negative integers decrease the counter.
- Zero: An increment of 0 leaves the numeric value unchanged.
- Multiple counters: Several counters can be updated in one declaration.
- Counter names: Names are case-sensitive identifiers.
- Counter initialization:
counter-resetcreates and initializes counter scopes. - Counter assignment:
counter-setassigns a value rather than incrementing it. - Displaying values: Use
counter()orcounters()with generated content. - Nested counters: Nested scopes support hierarchical numbering.
- Counter styles: Numbering can be displayed as decimals, letters, Roman numerals, or custom symbols.
- Automatic updates: Numbering changes when matching elements are added or removed.
- DOM: CSS counters do not insert ordinary HTML text nodes.
- Accessibility: Important numbering should remain understandable without CSS.
- Browser support: Basic CSS counters are widely supported in modern browsers.
Use counter-increment for automatically maintained numbering in headings, examples, figures, and custom lists. Combine it with clearly defined counter scopes and semantic HTML for predictable results.
Browser Support
The CSS counter-increment property is widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.
Basic counter increments, negative values, multiple named counters, and generated numbering have broad support. More advanced counter styles and specialized counter behavior may have separate compatibility considerations.
Basic Example
.steps {
counter-reset: step;
}
.steps .step {
counter-increment: step;
}
.steps .step::before {
content: counter(step) ". ";
}
Feature Detection
@supports (counter-increment: step 1) {
.step {
counter-increment: step 1;
}
}
Feature queries can check whether a declaration is recognized, although basic counter-increment support is sufficiently established that feature detection is rarely necessary.
For current compatibility information, visit Can I Use? .
Specifications
The counter-increment property is defined in CSS Lists and Counters Module Level 3.
CSS Lists and Counters Module Level 3
The specification defines counter increments, counter scopes, counter initialization, and the interaction between CSS counters and generated content.
