AI CSS Counter Generator — Custom Numbering and Counting with Pure CSS
Need automatic numbering in your HTML without relying on JavaScript or manually typing numbers? CSS counters let you create custom ordered lists, section numbering, and multi-level counting using nothing but pure CSS. In this guide, we'll break down how counter-reset, counter-increment, and the counters() function work — and how our AI CSS Counter Generator can produce the code for you in seconds.
What Are CSS Counters and Why Do They Matter?
CSS counters are variables maintained by CSS whose values can be incremented or reset by CSS rules. They track how many times an element appears and display that count using the content property on pseudo-elements like ::before. Think of them as automatic numbering built right into the browser — no scripts required.
Why does this matter? Standard HTML ordered lists (<ol>) give you basic 1, 2, 3 numbering, but they're limited. What if you need "Section 1.1", "Step 2.3", or Roman numeral headings across your entire page? That's where a css counter generator becomes essential. No JavaScript, no manual updates — just declare the counter and let the browser do the rest.
The Core Properties: counter-reset and counter-increment
Every CSS counter relies on two fundamental properties working together:
Understanding counter-reset
The counter-reset property creates or resets a counter. You place it on a parent element to initialize the counter before its children start incrementing it.
body {
counter-reset: section;
}
This creates a counter named section and sets it to 0. You can also initialize it to a specific value: counter-reset: section 5; starts counting from 5. Multiple counters can be reset on the same element: counter-reset: section chapter;.
Understanding counter-increment
The counter-increment property increases (or decreases) the counter's value each time the targeted element appears in the document.
h2 {
counter-increment: section;
}
Every <h2> element now bumps the section counter by 1. You can change the increment amount too: counter-increment: section 2; counts by twos, and negative values count backwards.
Displaying Counters with the content Property
Counters become visible through the content property on ::before or ::after pseudo-elements. The counter() function retrieves the current value:
h2::before {
content: "Section " counter(section) ": ";
color: #6c5ce7;
font-weight: 700;
}
This renders as "Section 1: ", "Section 2: ", and so on — automatically. No hardcoded numbers anywhere in your HTML. If you reorder sections or add new ones, the css numbering updates itself. This css content counter pattern is the one you'll use most often.
For more ways to style your generated CSS, check out our AI CSS Generator for complete stylesheet creation.
Skip the manual coding — generate CSS counters instantly
Try the AI CSS Counter Generator →Nested Counters for Multi-Level Numbering
One of the most powerful features of CSS counters is nesting. The counters() function (note the plural) concatenates all counter values in the nesting hierarchy, giving you numbering like 1.1, 1.2, 2.1, and so on.
ol {
counter-reset: item;
list-style-type: none;
}
li {
counter-increment: item;
}
li::before {
content: counters(item, ".") " ";
font-weight: 700;
color: #00cec9;
}
With nested <ol> elements, this produces a custom ordered list with hierarchical numbering:
- 1 First item
- 1.1 Nested item
- 1.2 Another nested item
- 2 Second item
- 2.1 Sub-item
Each nested <ol> creates a new scope for the counter. The counters() function walks up the tree and joins all values with the separator you provide (in this case, a dot). This is perfect for building a table of contents or structuring legal documents with numbered clauses.
Custom Counter Styles with @counter-style
The @counter-style rule lets you define entirely custom css counter style formats beyond the built-in decimal, upper-roman, or lower-alpha options. You can create counters that use emoji, custom symbols, or any numbering system you need.
@counter-style thumbs {
system: cyclic;
symbols: "👍";
suffix: " ";
}
@counter-style custom-roman {
system: extends upper-roman;
prefix: "(";
suffix: ") ";
}
ol.fancy {
list-style-type: thumbs;
}
ol.legal {
list-style-type: custom-roman;
}
The system property controls how counter values are generated. Common values include cyclic (loops through symbols), numeric (positional numbering), alphabetic, and extends (builds on an existing style). Combined with prefix and suffix, you have full control over how each number renders.
Practical Use Cases for CSS Counters
CSS counters aren't just a neat trick — they solve real problems across many types of content:
Table of Contents
Auto-number your headings and generate a matching table of contents. Each <h2> and <h3> gets a counter, and the TOC links display the same numbers. Our AI Table of Contents Generator pairs perfectly with CSS counters for this workflow.
Step-by-Step Guides
Tutorials and how-to articles benefit from automatic numbering css. Add or remove steps freely — the numbers always stay correct without manual editing.
Legal Documents and Contracts
Legal text demands precise hierarchical numbering (1, 1.1, 1.1.1). Nested CSS counters handle this natively, and the numbering stays consistent even when clauses are inserted or removed.
FAQ Sections
Number your FAQ questions automatically. Combine counters with CSS animations to create interactive accordion-style FAQ sections with numbered headers.
Figure and Table Captions
Academic and technical documents often need "Figure 1", "Table 2" labels. CSS counters handle this without any JavaScript, keeping your markup clean and semantic.
Complete Example: Document with Section Numbering
Here's a full working example that combines everything — nested counters, custom styles, and the ::before pseudo-element:
/* Initialize counters */
body {
counter-reset: chapter;
}
h2 {
counter-reset: sub-section;
counter-increment: chapter;
}
h3 {
counter-increment: sub-section;
}
/* Display chapter numbers */
h2::before {
content: "Chapter " counter(chapter) ". ";
}
/* Display sub-section numbers */
h3::before {
content: counter(chapter) "." counter(sub-section) " ";
color: #00cec9;
}
/* Custom style for appendix items */
@counter-style appendix {
system: extends upper-alpha;
prefix: "Appendix ";
suffix: ": ";
}
This gives you "Chapter 1. Introduction", then sub-sections like "1.1 Overview", "1.2 Background" — all automatic numbering that stays in sync as your document evolves.
Want to add smooth transitions when sections expand? Pair this with our AI CSS Animation Generator. Need to add decorative text effects to your counter labels? Try the AI CSS Text Shadow Generator.
Tips for Working with CSS Counters
- Always place
counter-reseton the parent element, not on the element being counted. - Use CSS custom properties (variables) to store counter-related colors and fonts for easy theming.
- Test nested counters carefully — each nesting level needs its own
counter-resetscope. - The
counter()function accepts an optional style argument:counter(section, upper-roman)displays Roman numerals. - Combine multiple counters on one element:
content: counter(chapter) "." counter(section);. - Minify your final CSS with the AI HTML Minifier to keep your production code lean.
Generate CSS counters, nested numbering, and custom counter styles with AI
Open the CSS Counter Generator →Wrapping Up
CSS counters give you full control over automatic numbering without a single line of JavaScript. From simple ordered lists to complex multi-level document numbering, the combination of counter-reset, counter-increment, and the content property covers virtually every use case. The @counter-style rule takes it further with custom symbols and formatting.
Instead of writing these rules by hand, let our AI CSS Counter Generator handle the syntax. Describe what you need, and get production-ready counter CSS in seconds.