CSS Counters

Create automatic numbering for headings and lists natively.

CSS counters are variables maintained by CSS whose values can be incremented by CSS rules to track how many times they are used. Initialize with counter-reset, increment with counter-increment, and display using counter() in ::before pseudo-elements.

Key Takeaways: CSS Counters

Mastering CSS Counters helps you write clean and reliable CSS code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

CSS Counters Code Example

CSS
body {
  counter-reset: section;
}
h2::before {
  counter-increment: section;
  content: "Section " counter(section) ": ";
  color: #2563EB;
}
Output
Automatically prefixes headings with Section 1:, Section 2:
Free sign-in required · Google login available
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services