Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Counters – Automatic Num | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 34 of 50

·css

CSS Counters – Automatic Numbering

5 mins

Course progress68%

What you'll learn

Keep these ideas in mind as you work through the lesson.

2 points
  • Counters are great for **custom list styles**, **section numbering**, or **step‑by‑step UI**.
  • Counters can be nested (`counter-reset` within a child).

Counters are a stateful feature that let you generate numbers without extra markup. Great for custom ordered lists, step‑by‑step UI, or section headings.

Declaring & Using Counters

/* Reset a counter on the parent */
section { counter-reset: step; }
/* Increment & display on each child */
section > h2::before {
  counter-increment: step;
  content: "Step " counter(step) ": ";
  font-weight:bold;
}

Example – Fancy Ordered List Without <ol>

<ul class="steps">
  <li>First thing</li>
  <li>Second thing</li>
  <li>Third thing</li>
</ul>
.steps { counter-reset: item; }
.steps li::before {
  counter-increment: item;
  content: counter(item) ". ";
  margin-right:0.5rem;
  font-weight:bold;
}

Nested Counters

ol { counter-reset: chapter; }
ol li { counter-increment: chapter; }
ol li::before { content: counter(chapter) ". "; }
ol li > ol { counter-reset: sub; }
ol li > ol li { counter-increment: sub; }
ol li > ol li::before { content: counter(chapter) "." counter(sub) " "; }

This produces a 1. 1, 1. 2, 2. 1 style like legal documents.


Real‑World Use‑Case – Timeline

.timeline { counter-reset: year; }
.timeline li {
  list-style:none;
  position:relative;
  padding-left:3rem;
}
.timeline li::before {
  counter-increment: year;
  content: counter(year);
  position:absolute; left:0; top:0;
  background:#0066cc; color:#fff; width:2rem; height:2rem;
  border-radius:50%; display:flex; align-items:center; justify-content:center;
}

Browser Support

Full support in all modern browsers, including mobile. IE 11 supports counters, but the content property can’t include images.

Accessibility Note

Because counters are generated visually, they are announced by screen readers as part of the element’s text, so no extra ARIA is needed.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Scroll‑SnapContinue to lesson 35CSS Accessibility – Focus Styles & Reduced MotionBack to CSS Basics to Advanced curriculum