Web design and digital-business ideas grounded in real tasks.

CSS counters can number headings as the document changes

A web designer checking automatically numbered article headings on a desktop monitor.
AI-generated photographic illustration; not documentary photography.

A guide has six numbered sections. An editor inserts a new section near the beginning, and every later number needs attention. CSS counters can handle that visual sequence automatically, provided the numbering follows the document’s structure.

MDN’s guide to CSS counters explains the relationship between creating a counter, increasing it and displaying its value. For a simple article, an illustrative starting point is:

.numbered-guide {
  counter-reset: section;
}
.numbered-guide h2 {
  counter-increment: section;
}
.numbered-guide h2::before {
  content: counter(section) ". ";
}

The wrapper begins the sequence; each matching heading advances it. The displayed number comes from generated content. This example is deliberately scoped to one class so a navigation heading elsewhere on the page does not unexpectedly join the count.

Test an edit that changes the sequence

In a local test page, insert a new heading between the first and second sections. Move another section farther down. Check that the displayed sequence follows the new document order and that an unrelated sidebar heading is unaffected.

Then inspect the real markup. Nested components, excluded sections or multiple guide wrappers may need a more specific selector or counter design. A rule that works on three plain headings is not proof that it covers every page template.

Do not make the number carry essential meaning alone

Keep headings descriptive without their generated prefixes. Test how the content is announced with the assistive technologies relevant to your audience; generated content should not be assumed to behave identically everywhere.

If a sequence is genuinely a list of steps, an HTML ordered list may express that relationship more appropriately. And if a contract, reference or cross-link relies on a permanent section number, automatic visual renumbering can create its own editorial problem.

Use the counter for a clearly defined presentation task. The useful result is easier editing with predictable numbering, while the document remains understandable when its decorative numbering is unavailable.