🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to CSS Notes
Topic #103

CSS Counters


CSS Counters

With CSS counters, you can create dynamic numbering of elements (like headings, sections, or list items) without using JavaScript.

CSS counters are "variables" maintained by CSS, and their values can be incremented (or decremented) by CSS rules.

Pizza

Hamburger

Hotdogs


CSS Automatic Numbering With Counters

CSS counters are like "variables". The variable values can be incremented (or decremented) by CSS rules.

To work with CSS counters we will use the following properties:

  • counter-reset - Creates or resets a counter
  • counter-increment - Increments or decrements a counter
  • content - Inserts generated content
  • counter() - Adds the value of a counter to an element

To use a CSS counter, it must first be created with the counter-reset property.


CSS Increase and Decrease Counter

The following example creates a counter for the page (in the body selector), then it increments the counter value by 1 for each

element:

Example

body {
  counter-reset: section;
}

h2::before {
  counter-increment: section;
  content: "Section " counter(section) ": ";
}

Decrementing a Counter

The counter-increment property has a second parameter. The default value is 1. To decrease the counter value, you can set it to -1.

Example

body {
  counter-reset: section;
}

h2::before {
  counter-increment: section
  -1;
  content: "Section " counter(section) ": ";
}

Incrementing by Custom Values

You can increment the counter by any value. Here we increment by 2:

Example

body {
  counter-reset: section;
}

h2::before {
  counter-increment: section
  2;
  content: "Section " counter(section) ": ";
}

Want to go beyond the notes?

Join CodingNow 2.0's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

CSS Counters – FAQs

Quick answers about learning CSS Counters in CSS.

This free note from CodingNow 2.0 explains CSS Counters in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on CodingNow 2.0, including CSS Counters, is 100% free with no signup required.
With focused practice, most students grasp CSS Counters in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now