Back to CSS Notes
Topic #104

Nested Counters

Nested counters number sections hierarchically like 1.1, 1.2 — perfect for documents. They manage numbering that would be tedious by hand.


CSS Using Two Counters

You can create multiple counters for different levels of numbering. The following example creates one counter for the page (named "section") and one counter for each

element (named "subsection"):

Example

body {
  counter-reset: section;
}

h1 {

counter-reset: subsection;
}

h1::before {
  counter-increment:
section;
  content: "Section " counter(section) ". ";

}

h2::before {

counter-increment: subsection;
  content:
counter(section) "." counter(subsection) " ";
}

The CSS counters() Function

The counters() function returns the current values of the named and nested counters, as a string.

Here we use the counters() function to insert a string between different levels of nested counters:

Example

ol {
  counter-reset: section;

list-style-type: none;
}

li::before {
  counter-increment: section;

content: counters(section,".") " ";
}

CSS Counter Properties

Property Description
content Used with the ::before and ::after pseudo-elements, to insert generated content
counter-increment Increments one or more counter values
counter-reset Creates or resets one or more counters
counter() Returns the current value of the named counter
counters() Returns the current values of the named and nested counters

Want to go beyond the notes?

Join Coding Now Tech Institute's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Nested Counters – FAQs

Quick answers about learning Nested Counters in CSS.

This free note from Coding Now Tech Institute explains Nested Counters in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on Coding Now Tech Institute, including Nested Counters, is 100% free with no signup required.
With focused practice, most students grasp Nested Counters in 1–3 days from these notes; pairing it with Coding Now Tech Institute'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 Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now