CSS counters let you generate ordered numbering in styles, useful for docs, steps, and nested outlines.
Counter workflow
- Initialize with
counter-reset. - Increment with
counter-increment. - Display using
content: counter(name)in pseudo-elements.
Pitfall
Generated numbers are presentational. Critical sequence meaning should still exist in semantic HTML structure when needed.
Important interview questions and answers
- Q: Why use counters instead of hardcoded numbers?
A: Automatic renumbering when items are inserted/removed. - Q: Best HTML partner for counters?
A: Ordered lists or structured headings for semantic fallback. - Q: Where are counters commonly used?
A: Documentation sidebars, step-by-step guides, and custom ordered UI lists.
Pitfall: Check cascade order—author stylesheet loses to inline styles and !important surprises.