CSS Counters
In this page:
body {
counter-reset: counter-name;
}
selector::before {
counter-increment: counter-name;
content: counter(counter-name);
}
Initializing Counters with counter-reset
To start a CSS counter, you must initialize its value using the counter-reset property on a parent element, which sets your counter variable's starting value (zero by default). counter-set works similarly to counter-reset but does not create a new counter if one does not already exist in scope -- it only sets the value of an existing counter, making it useful for resetting a counter's value partway through a list without restarting it from a parent.
- Declare your counter-reset properties on the parent container of the list you want to count.
- Accepted values:
- counter-reset — name and optional start value, e.g. section 0
- counter-set — like counter-reset but sets an existing counter's value
- none — no counter change (initial value)
- integer — may be negative
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Initializing Counters with counter-reset
<style>
ol {
counter-reset: item;
}
</style>
<ol><li>First</li></ol>
Incrementing Counters with counter-increment
The counter-increment property tells the browser to increase the value of your custom counter variable. Every time the browser encounters the targeted element, it ticks the counter up (by 1 by default).
- You can specify a custom increment value (like counter-increment: chapter 2) to count in steps.
- Accepted values:
- counter-increment — name and optional step, e.g. section 1
- counter-reset — name and optional start value, e.g. section 0
- none — no counter change (initial value)
- integer — may be negative
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Incrementing Counters with counter-increment
<style>
ol {
counter-reset: item;
}
li {
counter-increment: item;
}
</style>
<ol>
<li>Ticks up by 1 each time</li>
<li>Ticks up by 1 each time</li>
</ol>
Rendering Counters with Content
To display your counter value on your webpage, you use the counter() function inside the content property of a CSS pseudo-element (::before or ::after), allowing the browser to render the calculated numbers automatically.
Example: Rendering Counters with Content
<style>
ol {
counter-reset: item;
list-style: none;
}
li {
counter-increment: item;
}
li::before {
content: "Chapter " counter(item) ": ";
}
</style>
<ol>
<li>Introduction</li>
<li>Basics</li>
</ol>
Handling Nested Counters
You can nest counters to handle complex, multi-level outlines (like section '1.1' or '1.1.1'). By using the counters() function (with an s on the end), you can instruct the browser to render nested counters separated by dots automatically.
Example: Handling Nested Counters
<style>
ol {
counter-reset: section;
list-style: none;
}
li {
counter-increment: section;
}
li::before {
content: counters(section, ".") " ";
}
</style>
<ol>
<li>Chapter
<ol><li>Nested section</li></ol>
</li>
</ol>
Customizing Counter Styles
You can customize how your counter numbers display by passing a style parameter (like upper-roman or lower-alpha) to your counter() function, allowing you to render Roman numerals or alphabetical sequences easily.
- Use lowercase letters for sub-steps to make your lists easy to read and follow.
- Accepted values:
- decimal — 1, 2, 3 (initial value)
- lower-alpha / upper-alpha — a, b, c or A, B, C
- lower-roman / upper-roman — i, ii, iii or I, II, III
- disc / circle / square — bullets
- none — no marker
Example: Customizing Counter Styles
<style>
ol {
counter-reset: item;
list-style: none;
}
li {
counter-increment: item;
}
li::before {
content: counter(item, upper-roman) ". ";
}
</style>
<ol>
<li>First</li>
<li>Second</li>
</ol>
- Forgetting to initialize your counter values on a parent element using counter-reset.
- Attempting to display counter values inside elements directly without using the required ::before or ::after pseudo-elements.
- Using the simple counter() function on nested lists when counters() would be required to display sub-level numbering.
- CSS counters allow stylesheets to count page elements automatically and display the sequence numbers cleanly.
- Initialize your counter on a parent element using counter-reset, and tick the value up using counter-increment on the child elements.
- Display your counter values using counter() or counters() functions inside the content property of ::before or ::after pseudo-elements.
Standard CSS counters and nested list count functions are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline