π’ CSS counter-reset β Complete Tutorial
π What Is counter-reset?
The counter-reset property initializes or resets aCSS counter β a feature that lets you automatically number elements using pure CSS (no JavaScript or extra HTML needed).
>>βCSS counters let you generate numbering, labels, and ordering directly from CSS.β
Note
β Works with counter-increment
β Used with content: counter(name)
β Great for auto-numbering sections, lists, steps, cards, etc.
β Used with content: counter(name)
β Great for auto-numbering sections, lists, steps, cards, etc.
π¦ Basic Syntax
Syntax
counter-reset: counterName optionalStartValue;β’ counterName β the name of the counter
β’ optionalStartValue β default is 0
π 1. Resetting a Counter
Reset counter
body {
counter-reset: section;
}This creates a counter named section starting at 0.
π 2. Using Counters to Auto-Number Elements
Auto-numbering
body {
counter-reset: item;
}
h2::before {
counter-increment: item;
content: counter(item) ". ";
}Every <h2> gets automatic numbering:1. Title, 2. Title, 3. Title ...
π 3. Starting Counter at a Custom Value
Custom start
body {
counter-reset: chapter 10;
}First increment β 11
π 4. Multiple Counters
Multiple counters
body {
counter-reset: section 0 sub 0;
}You can define several counters at once.
π 5. Nested Counters (Chapters β Subsections)
Nested counters
body {
counter-reset: section;
}
h1 {
counter-reset: subsection;
counter-increment: section;
}
h1::before {
content: counter(section) ". ";
}
h2 {
counter-increment: subsection;
}
h2::before {
content: counter(section) "." counter(subsection) " ";
}Produces:1, 1.1, 1.2, 2, 2.1 ...
π 6. Using Counters in Lists (Custom List Styling)
Custom ordered list
ol {
list-style: none;
counter-reset: li;
}
li::before {
counter-increment: li;
content: counter(li) ") ";
}Creates a clean, custom-numbered list.
π§© 7. Using counters with content() & strings
Styled counter
.step::before {
counter-increment: step;
content: "Step " counter(step) ": ";
font-weight: bold;
}π 8. Roman Numerals, Letters & Other Styles
Counter style
h2::before {
counter-increment: sec;
content: counter(sec, upper-roman) ". ";
}| Style | Example |
|---|---|
| lower-alpha | a, b, c |
| upper-alpha | A, B, C |
| lower-roman | i, ii, iii |
| upper-roman | I, II, III |
π§ͺ Real-World Examples
1οΈβ£ Table of Contents Numbering
TOC numbering
.toc {
counter-reset: toc;
}
.toc-item::before {
counter-increment: toc;
content: counter(toc) ". ";
}2οΈβ£ Numbered Cards
Cards
.cards {
counter-reset: card;
}
.card::before {
counter-increment: card;
content: "Card #" counter(card);
}3οΈβ£ Numbered Steps / Workflow
Steps
.steps {
counter-reset: step;
}
.step::before {
counter-increment: step;
content: "Step " counter(step);
font-size: 1.2rem;
font-weight: bold;
}β οΈ Common Mistakes
- β Forgetting counter-reset β numbering doesnβt start
- β Using counter-reset inside the wrong element
- β Expecting counters to appear without content
- β Using increment without reset β counters continue forever
Note
β Place counter-reset on the container
β Use counter-increment on the repeated elements
β Use counter-increment on the repeated elements
π₯ Summary
counter-reset initializes CSS counters that allow auto-numbering of elements in pure CSS. It's perfect for headings, steps, lists, chapters, and structured documents.
>>βCSS counters replace JS for many numbering tasks β clean, automatic, and powerful.β
Learn more βMDN Docs π