Lesson 34 of 50
cssCSS Counters – Automatic Numbering
5 mins
Course progress68%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 34 of 50
css5 mins
Keep these ideas in mind as you work through the lesson.
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.
/* 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;
}
<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;
}
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.
.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;
}
content property can’t include images.Because counters are generated visually, they are announced by screen readers as part of the element’s text, so no extra ARIA is needed.