Just Frontend Things

Numbered headings with CSS

CSS

2021-01-26

With this incredibly easy way you can create numbered heading using just some lines of CSS.

Using the counter-reset, counter-increment and counter CSS properties you can easily add numbers before any element.

Add counter-reset to the element that should reset the counter, in our case body.

body { counter-reset: heading-counter 1; }

Now add counter(heading-counter), with a dot, to the pseudo selector :before of all h2. Also add the incrementer using counter-increment.

h2:before { content: counter(heading-counter) '. '; counter-increment: heading-counter; }

Write your <h2>.

<h2>My first heading</h2> <h2>My second heading</h2> <h2>My third heading</h2>

Which should give us a result looking like:

1. My first heading

2. My second heading

3. My third heading