Ordered lists number themselves, but what about numbering headings, figures or steps in a design? CSS has its own counters for this. They count elements as the page is drawn and print the numbers with ::before. Together with the ::marker pseudo-element and the many built-in list styles, including Bangla numerals, counters give you full control over numbering without typing a single number by hand.
In this lesson you will learn
counter-reset,counter-incrementandcounter()- Numbering chapters and sections automatically
- Nested numbers like 1.2.1 with
counters() - Styling bullets and numbers with
::marker list-style-typeoptions, includingbengali
You should know lists and pseudo-elements.
Counting Headings
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS counters</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; counter-reset: chapter; }
h3 { margin: 10px 0 4px; counter-increment: chapter; counter-reset: section; }
h3::before { content: "Chapter " counter(chapter) ": "; color: #1e88e5; }
h4 { margin: 4px 0 4px 16px; font-weight: normal; counter-increment: section; }
h4::before { content: counter(chapter) "." counter(section) " "; color: #888; }
/* Nested numbering for nested lists with counters() */
ol.legal { counter-reset: item; list-style: none; padding-left: 0; margin: 8px 0; }
ol.legal ol { counter-reset: item; list-style: none; padding-left: 20px; }
ol.legal li { counter-increment: item; }
ol.legal li::before { content: counters(item, ".") ". "; font-weight: bold; }
</style>
</head>
<body>
<h3>HTML</h3>
<h4>Elements</h4>
<h4>Attributes</h4>
<h3>CSS</h3>
<h4>Selectors</h4>
<ol class="legal">
<li>Rules
<ol><li>Members must be polite</li><li>No spam
<ol><li>No advertising</li></ol></li></ol>
</li>
<li>Fees</li>
</ol>
</body>
</html>
Result
- A counter needs three steps.
counter-reset: chaptercreates it (starting at 0).counter-increment: chapteradds 1 each time an element matches.counter(chapter)prints its value insidecontent. - Each
h3adds 1 tochapterand resetssection, so section numbers start again in every chapter: 1.1, 1.2, then 2.1. content: counter(chapter) "." counter(section) " "joins counter values and text.- For nested lists,
counters(item, ".")(note the s) prints the values of every level joined with a dot, giving numbers like 1.2.1. - Because the numbers come from CSS, adding or removing a heading renumbers everything automatically.
Styling List Markers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Styling list markers</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px 16px; }
ul, ol { margin: 0; padding-left: 22px; }
.ticks { list-style: "✔ "; }
.ticks li::marker { color: #43a047; }
.bangla { list-style-type: bengali; }
.roman { list-style-type: upper-roman; }
.big li::marker { color: #e53935; font-size: 1.3em; font-weight: bold; }
/* A custom step list built with a counter and ::before */
.steps { list-style: none; padding: 0; counter-reset: step; grid-column: span 2; display: flex; gap: 10px; }
.steps li { counter-increment: step; display: flex; align-items: center; gap: 6px; }
.steps li::before {
content: counter(step);
width: 26px; height: 26px; border-radius: 50%;
background: #5e35b1; color: white; display: grid; place-items: center;
}
</style>
</head>
<body>
<ul class="ticks"><li>Free lessons</li><li>Real examples</li></ul>
<ol class="bangla"><li>Dhaka</li><li>Chattogram</li><li>Sylhet</li></ol>
<ol class="roman"><li>Introduction</li><li>Methods</li></ol>
<ol class="big"><li>Read</li><li>Practise</li></ol>
<ol class="steps"><li>Sign up</li><li>Choose a course</li><li>Start learning</li></ol>
</body>
</html>
Result
list-style: "✔ "uses any text as the bullet.::markerselects the bullet or number of a list item. You can change its colour, size and font, as the red numbers show.list-style-type: bengalinumbers items with Bangla digits (১, ২, ৩). Other values includeupper-roman,lower-alphaandarabic-indic.- The step list hides the normal numbers with
list-style: noneand draws its own round badges with a counter and::before, which can be styled far more than a marker.
| Need | Use |
|---|---|
| Change bullet colour or size | li::marker { color: …; } |
| Different numbering style | list-style-type: upper-roman, bengali, … |
| Custom badge with background and shape | list-style: none + counter + ::before |
| Numbered headings or figures | counter-reset + counter-increment |
Common Mistakes
- Forgetting
counter-increment, so every number shows 0. - Resetting the counter on the item itself instead of its parent, so every item shows 1.
- Using
counter()when you need nested numbers. Usecounters()with a separator. - Trying to set
backgroundorpaddingon::marker. It only accepts a few properties, such as colour and font; use::beforefor full styling.
Practice Exercises
Exercise 1. Number every figure on a page as “Figure 1:”, “Figure 2:” before its caption.
Show answer
body { counter-reset: fig; }
figure { counter-increment: fig; }
figcaption::before { content: "Figure " counter(fig) ": "; font-weight: bold; }
Exercise 2. Make an ordered list count down from 5 to 1 using HTML only.
Show answer
<ol reversed>
<li>Fifth place</li> <li>Fourth</li> <li>Third</li> <li>Second</li> <li>Winner</li>
</ol>
The reversed attribute counts down; start="10" would choose a different first number.
Frequently Asked Questions
Do screen readers read numbers made with counters?
Most modern screen readers do read generated ::before content, but support is not perfect. If a number is important to the meaning, such as a step number in instructions, a real ordered list is the safest choice.
Can a counter go up by more than 1?
Yes. counter-increment: page 2 adds 2 each time, and counter-reset: page 10 sets the starting value to 10, so the first increased value is 11.
Summary
- Counters are created with
counter-reset, increased withcounter-incrementand shown withcounter(). counters()prints nested numbers like 1.2.1.::markerstyles list bullets and numbers;list-style-typechooses the numbering system.- For fully custom badges, hide the marker and use a counter with
::before.
Next: HTML data-* Attributes.
