Home 〉 CSS Tutorials 〉 CSS Counter Tutorial: How to Use CSS Counters with Examples
CSS Counter Tutorial: How to Use CSS Counters with Examples !
Looking to create automatic numbering in your lists or headings? CSS counters allow you to implement automatic numbering without JavaScript. This tutorial explains how to initialize, increment, and display counters for various elements. Enhance your content structure with dynamic numbering. Check out out Tutorial on CSS Counter
If you've ever wanted to add automatic numbering to lists, sections, or headings using just CSS — CSS Counters are what you need. They let you control numbering without writing extra HTML or JavaScript.
Let's break it down in a beginner-friendly way with practical examples.
A CSS counter is a feature in CSS that allows you to increment numbers and display them on elements like lists, headings, or paragraphs — all using pure CSS. It's great for:
Here's the basic flow:
Syntax ✍
/* Step 1: Reset or create the counter */
body {
counter-reset: my-counter;
}
/* Step 2: Increment the counter */
h2::before {
counter-increment: my-counter;
content: "Section " counter(my-counter) ": ";
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
counter-reset: section;
}
.counters::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
</style>
</head>
<body>
<h2 class="counters">Introduction</h2>
<h2 class="counters">CSS Counter Basics</h2>
<h2 class="counters">Advanced Counter Usage</h2>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
<!DOCTYPE html>
<html>
<head>
<style>
body {
counter-reset: chapter;
}
h2 {
counter-reset: sub;
}
.counter::before {
counter-increment: chapter;
content: "Chapter " counter(chapter) ": ";
}
.sub-counter::before {
counter-increment: sub;
content: counter(chapter) "." counter(sub) " ";
}
</style>
</head>
<body>
<h2 class="counter">Getting Started</h2>
<h3 class="sub-counter">What is CSS?</h3>
<h3 class="sub-counter">Why Counters?</h3>
<h2 class="counter">Practical Usage</h2>
<h3 class="sub-counter">List Numbering</h3>
<h3 class="sub-counter">Nested Headings</h3>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
<!DOCTYPE html>
<html>
<head>
<style>
ol.custom-counter {
counter-reset: custom;
list-style: none;
padding-left: 0;
}
ol.custom-counter li {
counter-increment: custom;
margin-bottom: 10px;
}
ol.custom-counter li::before {
content: counter(custom) ". ";
font-weight: bold;
color: #007acc;
}
</style>
</head>
<body>
<ol class="custom-counter">
<li>Install CSS</li>
<li>Set up HTML</li>
<li>Apply Counters</li>
</ol>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Property | Purpose |
---|---|
'counter-reset' | Creates or resets a counter to zero |
'counter-increment' | Increments the counter when an element appears |
'counter()' | Retrieves the value of the counter |
'::before' or '::after' | Used to insert the counter content |
CSS Counters are a clean and powerful way to add automatic numbering to your elements without extra markup or scripts. They're great for improving structure, styling, and SEO in your content. By understanding 'counter-reset', 'counter-increment', and 'counter()', you can create highly customizable layouts.
Yes, CSS counters are supported by all major modern browsers.
Yes! You can create multiple counters with different names and nest them too.
Yes. You can wrap them in spans or use '::before' for styling.
It can be, but screen readers may not read CSS-generated content. Combine it with semantic HTML for best results.