#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Counters

CSS counters are named variables that CSS itself keeps track of and increases as it walks through your document. You use them to number things automatically, such as headings, steps in a tutorial, figure captions or nested outline items, without typing the numbers into the HTML.

The three properties that make a counter work

A counter needs only three pieces:

  • counter-reset creates the counter (or sets it back to zero) on a parent element.
  • counter-increment adds one each time a matching element appears.
  • counter() or counters() prints the current value, almost always inside the content property of a ::before or ::after pseudo-element.

The counter name is your own choice, like step or chapter. It does not need to be declared anywhere else.

.recipe {
  counter-reset: step;
}

.recipe h3 {
  counter-increment: step;
}

.recipe h3::before {
  content: "Step " counter(step) ": ";
  color: #0d6efd;
  font-weight: 700;
}

With this CSS, every <h3> inside .recipe gets "Step 1:", "Step 2:" and so on. If you add a new step in the middle of the HTML, every number after it updates by itself.

Numbering in different styles and steps

The counter() function accepts a second argument for the list style, the same keywords you use with list-style-type. For example, counter(step, upper-roman) prints I, II, III, and counter(step, lower-alpha) prints a, b, c.

You can also change the increment and the starting value. counter-increment: step 2 counts by twos, and counter-reset: step 4 makes the first incremented item show 5, because the reset sets the value before the first increment is applied.

Nested counters for outlines

Legal documents and long guides often need numbers like 2.1 and 2.1.3. The counters() function, with an "s", joins every level of a same-named counter using a separator string you choose.

ol {
  counter-reset: item;
  list-style: none;
}

li {
  counter-increment: item;
}

li::before {
  content: counters(item, ".") " ";
  font-weight: 600;
}

Each nested <ol> creates a fresh instance of item, so an item inside a second-level list prints something like "1.2", and the outer numbering continues correctly afterwards.

Where counters go wrong

The most common surprise is putting counter-reset on the same element that you increment. If each h3 resets and then increments, every heading shows 1. The reset belongs on a shared parent or an earlier element.

Other things to keep in mind:

  • Elements with display: none do not increment counters, so hidden items will not leave gaps in the numbering.
  • Generated content is not reliably copied when a user selects text, and some assistive technologies may read it differently. For numbers that carry real meaning, such as clause numbers in a contract, consider writing them into the HTML.
  • If you want basic numbering for a plain list, an ordinary <ol> already does that. Counters shine when you need custom text, styling, or numbering of non-list elements.

Practical uses

Beyond headings, counters are handy for numbering figures ("Figure 3"), table rows in a report, FAQ entries, or showing how many checkboxes are ticked when combined with the :checked selector. They keep numbers in sync with the content, which saves you from manual edits every time the page changes.

In short: reset on the parent, increment on the item, and display with counter() inside ::before.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course