πŸ”’ 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.

πŸ“¦ 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) ". ";
}
StyleExample
lower-alphaa, b, c
upper-alphaA, B, C
lower-romani, ii, iii
upper-romanI, 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

πŸ”₯ 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 πŸ“˜