On this page10 sections
Lecture 025: Counters
| Category | 5: Pseudo-classes & Components |
| Lecture | 025 of 057 |
| Estimated video length | 8 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
CSS can count. Counters number headings, sections, steps or figures automatically. Add or remove an item and every number updates, with no typing numbers by hand.
Learning Objectives
- Create and reset a counter with
counter-reset - Increase it with
counter-increment - Show it with
counter()insidecontent - Build nested numbering such as 1.1, 1.2 with
counters()
1. The Three Steps
counter-reset: name;creates (or resets) the counter at zero.counter-increment: name;adds one each time an element appears.content: counter(name);shows the value, inside::beforeor::after.
Example
<style>
body { counter-reset: section; }
h3::before { counter-increment: section; content: "Section " counter(section) ": "; color: #1971c2; }
</style>
<h3>Introduction</h3>
<h3>Installation</h3>
<h3>First steps</h3>2. Numbered Steps With Big Badges
Example
<style>
.steps { counter-reset: step; list-style: none; padding: 0; }
.steps li { counter-increment: step; display: flex; align-items: center; gap: 12px; margin: 10px 0; }
.steps li::before {
content: counter(step);
width: 32px; height: 32px; border-radius: 50%;
display: grid; place-items: center;
background: #2f9e44; color: white; font-weight: bold;
}
</style>
<ol class="steps">
<li>Create an account</li>
<li>Choose a course</li>
<li>Start the first lecture</li>
</ol>3. Nested Counters
Example
<style>
body { counter-reset: chapter; }
h3 { counter-increment: chapter; counter-reset: part; }
h3::before { content: counter(chapter) ". "; }
h4 { counter-increment: part; margin-left: 16px; }
h4::before { content: counter(chapter) "." counter(part) " "; }
</style>
<h3>HTML</h3>
<h4>Elements</h4>
<h4>Attributes</h4>
<h3>CSS</h3>
<h4>Selectors</h4>
<h4>Colours</h4>
<h4>Box model</h4>Each h3 resets part, so sub-numbering starts again at 1.
4. Nested Lists With counters()
counters(name, ".") joins the counter values of every nesting level.
Example
<style>
ol.outline { counter-reset: item; list-style: none; }
ol.outline li { counter-increment: item; }
ol.outline li::before { content: counters(item, ".") " "; font-weight: bold; }
</style>
<ol class="outline">
<li>Getting started
<ol class="outline"><li>Install</li><li>Configure</li></ol>
</li>
<li>Usage
<ol class="outline"><li>Basics</li><li>Advanced
<ol class="outline"><li>Plugins</li></ol></li></ol>
</li>
</ol>5. Options
counter-reset: figure 10; /* start at 10 */
counter-increment: figure 2; /* count in twos */
content: counter(figure, upper-roman); /* I, II, III… */Common Beginner Mistakes
1. No counter-reset. Without it the counter may never reset, so numbers carry on across sections.
2. Forgetting content. The counter counts, but nothing is shown.
3. Using counters for important numbers. Screen readers may not read generated content. Keep vital numbering in the HTML.
Practice Questions
- Which property creates a counter?
- Which property adds one to it?
- How do you display a counter called
fig? - Number
h2headings "Chapter 1", "Chapter 2"… - What's the difference between
counter()andcounters()?
Practice Answers
counter-resetcounter-incrementcontent: counter(fig);inside::beforeor::after.body { counter-reset: ch; } h2::before { counter-increment: ch; content: "Chapter " counter(ch) " "; }counter()shows one level;counters()joins all nested levels, such as 2.1.3.
Key Takeaways
counter-reset→counter-increment→content: counter(name).- Reset an inner counter on each outer item for 1.1, 1.2 numbering.
counters(name, ".")handles deeply nested lists.