CSS counter-set Property
The counter-set property assigns a specific integer value to an existing CSS counter without creating a new counter scope.
CSS counters provide automatic numbering for headings, lists, steps, figures, and other repeated content. The counter-reset property creates a counter, counter-increment changes its value, and counter-set assigns a particular value when needed.
Unlike counter-increment, which adds to or subtracts from the current value, counter-set replaces that value. If the named counter does not exist in an applicable scope, the property creates a new counter instance.
counter-set Syntax
The counter-set property accepts one or more counter names, each optionally followed by an integer specifying the value to assign.
selector {
counter-set: counter-name integer;
}
Set a Counter to Five
.item {
counter-set: step 5;
}
Set a Counter to Zero
.item {
counter-set: step 0;
}
Set a Negative Value
.item {
counter-set: step -3;
}
Set Multiple Counters
.item {
counter-set: chapter 3 example 10;
}
Omit the Integer
.item {
counter-set: step;
}
When the integer is omitted, the counter is assigned a value of 0.
No Counter Assignment
.item {
counter-set: none;
}
CSS-Wide Keywords
.item {
counter-set: inherit;
}
.item {
counter-set: initial;
}
.item {
counter-set: unset;
}
.item {
counter-set: revert;
}
.item {
counter-set: revert-layer;
}
Counter names are case-sensitive identifiers. The assigned values must be integers, and multiple counters are separated by spaces rather than commas.
Property Values
| Value | Description |
|---|---|
none |
Does not assign any counter values. This is the initial value. |
counter-name |
Sets the named counter to 0. |
counter-name integer |
Sets the named counter to the specified integer. |
counter-name -integer |
Sets the named counter to a negative integer. |
| Multiple counter assignments | Sets several named counters to their specified values. |
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 property assigns counter values but does not display them. Use counter() or counters() with generated content to show the current value.
How It Works
The counter-set property changes the current value of a named counter to a specific integer.
Step 1: Create a Counter
.steps {
counter-reset: step;
}
The counter starts at 0.
Step 2: Increment Each Item
.steps .step {
counter-increment: step;
}
The counter increases by 1 for each matching item.
Step 3: Assign a Specific Value
.steps .step.special {
counter-set: step 10;
}
The special item sets the counter to 10. The next ordinary item continues at 11.
Step 4: Display the Counter
.steps .step::before {
content: counter(step) ". ";
}
Step 5: Add the HTML
<div class="steps">
<p class="step">First item</p>
<p class="step">Second item</p>
<p class="step special">Special item</p>
<p class="step">Next item</p>
</div>
Generated Values
The items display 1, 2, 10, and 11.
Important Distinction
When counter-increment and counter-set apply to the same element, the increment occurs first and the assignment occurs afterward. The assigned value is the one displayed on that element.
Default Value
The initial value of counter-set is none.
.item {
counter-set: none;
}
This declaration does not assign a value to any counter.
Counter Name Without an Integer
.item {
counter-set: item;
}
This sets the named counter to 0.
Equivalent Declaration
.item {
counter-set: item 0;
}
Important Distinction
none means no counter assignment occurs. A named counter without an integer is different because it explicitly assigns the value 0.
Setting Counter Values
Use an integer after the counter name to assign a specific value.
Set the Value to Five
.item {
counter-set: item 5;
}
Set the Value to Twenty
.item {
counter-set: item 20;
}
Continue from the Assigned Value
.items {
counter-reset: item;
}
.items .item {
counter-increment: item;
}
.items .item.special {
counter-set: item 20;
}
.items .item::before {
content: counter(item) ". ";
}
When the special item sets the counter to 20, the next ordinary item displays 21.
Set a Value Without Incrementing
.item {
counter-set: item 10;
}
The counter is assigned 10 directly, without adding to its previous value.
Practical Consideration
Use counter-set when numbering must jump to a particular value or resume from a known point within an existing counter sequence.
Negative Counter Values
The counter-set property accepts negative integers.
Assign a Negative Value
.item {
counter-set: item -5;
}
Continue Increasing from a Negative Value
.items {
counter-reset: item;
}
.items .item {
counter-increment: item;
}
.items .item.special {
counter-set: item -3;
}
After the special item assigns -3, the next ordinary item increases the counter to -2.
Continue Decreasing from a Negative Value
.items .item {
counter-increment: item -1;
}
.items .item.special {
counter-set: item -5;
}
The next ordinary item decreases the counter to -6.
Practical Consideration
Negative values can be useful for custom numeric sequences, countdowns, and specialized numbering formats.
Setting Multiple Counters
A single counter-set declaration can assign values to multiple named counters.
Assign Two Values
.item {
counter-set: chapter 3 example 10;
}
Assign Three Values
.item {
counter-set: chapter 2 figure 5 table 8;
}
Mix Positive and Negative Values
.item {
counter-set: completed 10 remaining -2;
}
Create the Initial Counters
.document {
counter-reset: chapter figure table;
}
Assign Values Later
.document .special {
counter-set: chapter 4 figure 12 table 3;
}
Important Consideration
Each counter is assigned independently. Changing the value of one counter does not automatically change the values of other counters.
Counter Operations
Three CSS properties control the creation and modification of named counters.
counter-reset
.group {
counter-reset: item 0;
}
Creates a new counter instance initialized to 0.
counter-increment
.item {
counter-increment: item 1;
}
Adds 1 to the current counter value.
counter-set
.item {
counter-set: item 10;
}
Assigns the value 10 to the current counter.
Comparison Table
| Property | Operation | Effect |
|---|---|---|
counter-reset |
Initialize | Creates a new counter instance with a starting value. |
counter-increment |
Increment or decrement | Changes the current counter value by an integer amount. |
counter-set |
Assign | Replaces the current counter value with a specified integer. |
Order of Operations
When all three properties apply to the same element, the browser processes counter resets first, increments second, and assignments last.
.item {
counter-reset: number 5;
counter-increment: number 2;
counter-set: number 10;
}
The final counter value on this element is 10.
Practical Consideration
Use counter-reset for a new numbering scope, counter-increment for sequential changes, and counter-set for direct value assignments.
Changing Numbering Mid-Sequence
One of the main uses of counter-set is changing a counter's value partway through a numbering sequence.
Basic Example
.steps {
counter-reset: step;
}
.steps .step {
counter-increment: step;
}
.steps .step.jump {
counter-set: step 10;
}
.steps .step::before {
content: counter(step) ". ";
}
HTML Structure
<div class="steps">
<p class="step">First item</p>
<p class="step">Second item</p>
<p class="step jump">Jump to ten</p>
<p class="step">Continue numbering</p>
</div>
Generated Values
The displayed values are 1, 2, 10, and 11.
Why Not Use counter-reset?
Applying counter-reset to the special item would create a new counter instance. Using counter-set changes the existing counter's value, allowing subsequent elements to continue the same sequence.
Practical Consideration
This approach is useful when continuing numbering across sections or assigning special numbers to selected items.
Counter Scope
CSS counters have scopes that determine which counter instance is available to an element.
Create a Counter Scope
.group {
counter-reset: item;
}
Assign a Value Within the Scope
.group .special {
counter-set: item 10;
}
Continue Numbering
.group .item {
counter-increment: item;
}
Independent Groups
.group {
counter-reset: item;
}
.group .item {
counter-increment: item;
}
.group .special {
counter-set: item 10;
}
Each group has its own counter instance. Assigning a value in one group does not change the counter in another independent group.
When No Counter Exists
If counter-set references a counter that does not exist in an applicable scope, the browser creates a new counter instance with the specified value.
Important Distinction
Although counter-set is not inherited, descendant elements can access counter values established within an applicable ancestor scope.
Nested Counters
The counter-set property can modify counters used in hierarchical numbering systems.
Initialize Section and Subsection Counters
.document {
counter-reset: section;
}
.document .section {
counter-increment: section;
counter-reset: subsection;
}
.document .subsection {
counter-increment: subsection;
}
Assign a Section Number
.document .section.special {
counter-set: section 5;
}
Assign a Subsection Number
.document .subsection.special {
counter-set: subsection 10;
}
Display the Hierarchical Values
.document .section::before {
content: counter(section) ". ";
}
.document .subsection::before {
content: counter(section) "." counter(subsection) " ";
}
Practical Consideration
When changing a nested counter, ensure that the correct counter instance is active. A value assigned to a subsection counter should not unintentionally affect the numbering of unrelated sections.
List Numbering
The counter-set property can adjust numbering in custom lists and can also work with the built-in list-item counter.
Custom List Counter
.custom-list {
counter-reset: item;
list-style: none;
}
.custom-list li {
counter-increment: item;
}
.custom-list li.special {
counter-set: item 10;
}
.custom-list li::before {
content: counter(item) ". ";
}
Built-In list-item Counter
.numbered-list li.special {
counter-set: list-item 10;
}
The list-item counter is used by native list markers. Assigning a value can change the number displayed for a selected list item.
HTML Alternative
<ol>
<li>First item</li>
<li>Second item</li>
<li value="10">Tenth item</li>
<li>Eleventh item</li>
</ol>
Practical Consideration
For ordinary ordered lists, the HTML value attribute is often preferable because it preserves the intended numbering in the document rather than depending on CSS.
Displaying Counter Values
The counter-set property assigns a value but does not display it. Use the counter() or counters() function with generated content.
Display a Counter
.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 Values
.nested-item::before {
content: counters(section, ".") " ";
}
Important Distinction
The counter() function displays one counter value, while counters() combines values from nested counter instances with the same name.
Practical Uses
The counter-set property is useful when automatic numbering needs a specific adjustment.
Resume Numbering at a Known Value
.continued-section {
counter-set: section 10;
}
Jump to a Specific Step
.step.special {
counter-set: step 20;
}
Assign a Figure Number
figure.special {
counter-set: figure 15;
}
Assign a Chapter Number
.chapter.special {
counter-set: chapter 5;
}
Adjust Multiple Counters
.document .special {
counter-set: chapter 3 example 12;
}
Modify Native List Numbering
ol li.special {
counter-set: list-item 10;
}
Practical Consideration
Use counter-set for exceptions within an otherwise automatic numbering sequence. For ordinary sequential numbering, counter-reset and counter-increment are usually sufficient.
counter-set Example
This example demonstrates how counter-set changes numbering partway through a sequence without creating a new counter scope.
The first two items are numbered normally. The third item assigns the value 10, and the remaining items continue from that value.
<div class="counter-set-demo">
<div class="demo-item">Create the HTML document.</div>
<div class="demo-item">Add the page content.</div>
<div class="demo-item special">Continue at item ten.</div>
<div class="demo-item">Add the CSS styles.</div>
<div class="demo-item">Preview the finished page.</div>
</div>
.counter-set-demo {
counter-reset: item;
display: grid;
gap: 10px;
}
.counter-set-demo .demo-item {
counter-increment: item;
padding: 12px 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.counter-set-demo .demo-item::before {
content: counter(item) ". ";
font-weight: bold;
color: #336699;
}
.counter-set-demo .demo-item.special {
counter-set: item 10;
border-color: #336699;
background-color: #eaf2f8;
}
The container creates a counter named item with an initial value of 0.
Each .demo-item increases the counter by 1. The special item then uses counter-set: item 10 to assign the value 10.
The resulting numbering is 1, 2, 10, 11, and 12.
The highlighted item demonstrates where the numbering changes. Subsequent items continue automatically from the assigned value.
Accessibility
CSS counters can improve visual organization, but generated numbering should not be the only source of essential information.
Use Semantic HTML
For ordered sequences, use <ol> and <li> elements whenever possible.
Preserve Meaningful Numbering
If a particular number is important for understanding instructions, consider including it in the HTML.
Screen Reader Support
Assistive technologies may announce CSS-generated counter values differently depending on the browser and screen reader combination.
Avoid Confusing Numbering Changes
When numbering jumps to another value, ensure the change is understandable and does not make the sequence difficult to follow.
Use Native List Features When Appropriate
The HTML start, reversed, and value attributes provide meaningful numbering controls for ordered lists.
Test Without CSS
Check that the document remains understandable when generated numbering is unavailable.
Maintain Visual Contrast
Ensure that generated numbers remain readable against their backgrounds and at increased zoom levels.
Important Considerations
Keep the following points in mind when using counter-set:
- Purpose: Assigns a specific integer value to a named CSS counter.
- Initial value:
none. - Inheritance: The property is not inherited.
- Default assigned value: A named counter without an integer is set to 0.
- Positive values: Positive integers can be assigned directly.
- Negative values: Negative integers are supported.
- Multiple counters: Several counters can be assigned values in one declaration.
- Counter names: Names are case-sensitive identifiers.
- Counter scopes: Existing counter instances are updated without intentionally creating a new scope.
- Missing counters: A new counter instance is created when no applicable counter exists.
- counter-reset: Creates a new counter instance with an initial value.
- counter-increment: Adds to or subtracts from the current counter value.
- Operation order: Counter resets occur before increments, and assignments occur afterward.
- Nested counters: Values can be assigned within hierarchical numbering systems.
- Built-in list counters: The
list-itemcounter can be modified for native list numbering. - Displaying values: Use
counter()orcounters()with generated content. - Automatic continuation: Subsequent increments continue from the assigned value.
- Accessibility: Essential numbering should not depend exclusively on generated CSS content.
Use counter-set when a numbering sequence needs to jump to or resume from a particular value. For ordinary sequential numbering, combine counter-reset and counter-increment instead.
Browser Support
The CSS counter-set property is supported in modern versions of Chrome, Edge, Firefox, and Safari.
Although CSS counters have been supported for many years, counter-set was introduced later than counter-reset and counter-increment. Older browsers may not recognize it.
Basic Example
.items {
counter-reset: item;
}
.items .item {
counter-increment: item;
}
.items .item.special {
counter-set: item 10;
}
Feature Detection
@supports (counter-set: item 10) {
.item.special {
counter-set: item 10;
}
}
The @supports rule checks whether the browser recognizes the declaration.
Fallback Considerations
When supporting older browsers, use ordinary sequential counters as a fallback or provide important numbering directly in the HTML.
For current compatibility information, visit Can I Use? .
Specifications
The counter-set property is defined in CSS Lists and Counters Module Level 3.
CSS Lists and Counters Module Level 3
The specification defines counter assignments, counter scopes, and the interaction between counter-set, counter-reset, and counter-increment.
