
How to Automatically Number Headings with CSS
To automatically number headings with CSS, we can use three CSS properties that are working as counters: counter-reset
, counter-increment
, and counter
:
Copied to clipboard! Playground
body {
counter-reset: heading;
}
h1:before {
content: counter(heading) ") ";
counter-increment: heading;
}
This code will first create a new counter called heading
, with a default value of 0. To create counters in CSS, we can use counter-reset
. Then to use it for headings, we can use the ::before
pseudo-element and reference the counter
Β we have created, using the counter function. Make sure you also call counter-increment
, this will increment the counter by one for every element. This will generate the following:
Copied to clipboard! Playground
<h1>Table of Contents</h1>
<h1>The Tale of CSS</h1>
<h1>Acknowledgments</h1>
<!-- Will be rendered as: -->
1) Table of Contents
2) The Tale of CSS
3) Acknowledgments


Resources:
π More Webtips
Master the Art of Frontend
Access exclusive interactive lessons
Unlimited access to hundreds of tutorials
Remove ads to learn without distractions
Courses

CSS - The Complete Guide (including Flexbox, Grid and Sass)
Including Flexbox, Grid, and Sass
Whether you're learning CSS for the first time or brushing up on your CSS skills and diving even deeper, this course is for you. Every web developer has to know CSS.

The HTML & CSS Bootcamp
From Zero to Expert!
This course covers flexbox, CSS grid, animations, responsive design, and much more! You will find tons of exercises & projects inside this course.

The Creative HTML5 & CSS3 Course
Build Awesome Websites
Learn HTML5 and CSS3 by creating three amazing, well-designed and animated websites from scratch.