On this page10 sections
  1. Learning Objectives
  2. 1. The Three Steps
  3. 2. Numbered Steps With Big Badges
  4. 3. Nested Counters
  5. 4. Nested Lists With counters()
  6. 5. Options
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 025: Counters

Category5: Pseudo-classes & Components
Lecture025 of 057
Estimated video length8 min
TargetCSS3 · any modern browser
LevelBeginner

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() inside content
  • Build nested numbering such as 1.1, 1.2 with counters()

1. The Three Steps

  1. counter-reset: name; creates (or resets) the counter at zero.
  2. counter-increment: name; adds one each time an element appears.
  3. content: counter(name); shows the value, inside ::before or ::after.

Example

html
<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

html
<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

html
<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

html
<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

css
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

  1. Which property creates a counter?
  2. Which property adds one to it?
  3. How do you display a counter called fig?
  4. Number h2 headings "Chapter 1", "Chapter 2"…
  5. What's the difference between counter() and counters()?

Practice Answers

  1. counter-reset
  2. counter-increment
  3. content: counter(fig); inside ::before or ::after.
  4. body { counter-reset: ch; } h2::before { counter-increment: ch; content: "Chapter " counter(ch) " "; }
  5. 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.