Learn CSS Counters

A CSS counter is a counter the browser runs to number elements automatically, with no JavaScript. counter-reset: name on the wrapper element (the parent of…

Say chapter numbers are typed directly into the HTML ("Chapter 1", "Chapter 2"). Then every time you add a new chapter in the middle, you have to change every number after it, one by one. A CSS counter is a counter that the browser runs for you. The browser counts the elements you point at, then shows the number in front of each one. Think of a person at the door of an event who presses a hand counter each time someone walks in. The browser is that person, the CSS counter is the hand counter, and each element is one visitor.

Three steps: create, increase, show

The HTML holds only the titles, with no numbers. The CSS inside <style> makes the numbers:

<div class="toc">
  <h2>Introduction</h2>
  <h2>Basics</h2>
  <h2>Going further</h2>
</div>

<style>
.toc {
  counter-reset: chapter;
}
.toc h2::before {
  counter-increment: chapter;
  content: "Chapter " counter(chapter) ": ";
}
</style>

The result: "Chapter 1: Introduction", "Chapter 2: Basics", "Chapter 3: Going further". If you add a new chapter in the middle, you don't have to change the numbers below it. The browser counts again every time the content of the page changes. So a chapter you add, delete or move gets the right number right away.

The counter's name must be exactly the same in counter-reset, counter-increment and counter(). If even one letter is different, it is a different counter.

Change the number style

counter() takes a number style as a second value inside the brackets, after a comma:

Without a second value you get plain numbers: 1, 2, 3.

⚠️ Are the numbers wrong?

💡 Nested numbers (1, 1.1, 1.2)? Use counters(item, "."), with an s at the end. When every <ol>, the outer one and each one inside it, has counter-reset: item, counters() joins the numbers of all the levels, with a dot between them.

TL;DR: A CSS counter is a counter the browser runs to number elements automatically, with no JavaScript. counter-reset: name on the wrapper element (the parent of the elements you number) creates the counter, counter-increment: name on each element raises it, and content: counter(name) in ::before shows it. Use the same name in all three. You can change the number style, for example counter(name, upper-roman) for Roman numerals.

⚡ Create, increase, show

Write:

.toc {
  counter-reset: chapter;
}
.toc h2::before {
  counter-increment: chapter;
  content: "Chapter " counter(chapter) ": ";
}

Meaning: A CSS counter is a counter that the browser runs to number elements. counter-reset: chapter on the wrapper (.toc, the element that holds all the <h2> elements) creates a counter called chapter that starts at 0. counter-increment: chapter adds 1 for every <h2>. counter(chapter) in content shows the number in ::before, a small box that CSS creates before the element's content. So if the first <h2> is <h2>Introduction</h2>, that title reads "Chapter 1: Introduction".

⚡ Reset on the wrapper

Guess first: if counter-reset goes on the h2 instead of its wrapper, what number does each title get?

All of them get 1. Every title starts its counter from 0 again, then raises it by one.

Meaning: Put counter-reset on the wrapper, so the counter is created once for all the titles. The counter's name must be exactly the same in counter-reset, counter-increment and counter(). If even one letter is different, it is a different counter.

⚡ Change the number style

Meaning: counter() takes a number style as a second value. counter(chapter, upper-roman) shows I, II, III, and counter(chapter, lower-alpha) shows a, b, c. Without a second value you get plain numbers: 1, 2, 3.

What you will learn