counter() CSS Function
The counter() function inserts the current value of a CSS counter as text, usually in the content property, such as counter(section).
Description
The counter() CSS function is used to retrieve the current value of a CSS counter-reset or counter-increment for a specified counter and display it in generated content, typically within the content property. It allows you to insert numeric values dynamically into your web page without altering the HTML structure, making it particularly useful for automatically numbered lists, headings, or sections.
Counters in CSS are essentially named variables that track numeric values. You can initialize a counter using counter-reset, increment it with counter-increment, and then display it using counter(). By default, counter() displays the value as a decimal number. An optional second argument chooses a different counter style, such as upper-roman, lower-alpha, a style defined with @counter-style, or an inline style made with symbols(), for example counter(section, upper-roman). To show nested counters such as "1.2.3", use counters() instead.
A simple example of counter() usage is numbering paragraphs within a div:
div {
counter-reset: section;
}
div p::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
In this example:
- The counter named
sectionis reset at the start of eachdiv. - Each
pinside thedivincrements the counter. - The
counter()function then displays the current counter value before the paragraph text.
This approach allows for automatic numbering without hardcoding numbers in the HTML, keeping content flexible and easier to maintain. You can also use counter() multiple times in the content property to display counters in complex formats, such as nested lists or multi-level headings.
Interactive Demo
- Mix the dry ingredients
- Add the milk and eggs
- Bake for 30 minutes
Syntax
counter() = counter( <counter-name>, <counter-style>? )
Values
- <counter-name>The name of the counter, a case-sensitive <custom-ident> that has been created with counter-reset, counter-increment or counter-set. The name list-item refers to the built-in counter used by ordered lists. If no counter with that name exists, one is created with the value 0.
- <counter-style>Optional. The style used to display the value: a list-style-type keyword such as decimal (the default), decimal-leading-zero, lower-roman, upper-roman, lower-alpha or upper-alpha, the name of an @counter-style rule, or a symbols() function. The value none displays nothing.
Example
Browser Support
The following information will show you the current browser support for the CSS counter() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





