CSS Portal

counters() CSS Function

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The counters() CSS function is used to return a string representing the value of a series of counters that have been incremented through the document using the counter-increment property. Unlike the counter() function, which retrieves a single counter value, counters() allows you to concatenate multiple counter levels into a single string, separated by a defined delimiter.

This function is especially useful when styling hierarchical elements such as nested ol lists or headings, where you want to display numbering like "1.2.3" to indicate levels of depth. Each counter in the series is defined by its name and can be formatted with a string, such as decimal numbers, Roman numerals, or letters.

For example, consider nested li items where you want each sub-item to show a multi-level numbering:

ol {
  counter-reset: section;
}

li {
  counter-increment: section;
}

li::before {
  content: counters(section, ".") " ";
}

In this example, the counters(section, ".") function concatenates the counter values of all parent li elements in the hierarchy, separated by a period. So, a third-level item could appear as "1.2.3".

You can also combine counters() with other text in the content property to create more descriptive labels:

li::before {
  content: "Section " counters(section, ".") ": ";
}

Here, the text "Section" is prepended to the hierarchical numbering, producing output like "Section 2.1: ". This makes counters() a powerful tool for automatically numbering nested content without manually updating each element.

Syntax

counters(<counter-name>, <connector-string> [, <counter-style>])

Values

  • <counter-name>(Required): The case-sensitive name of the counter you defined using counter-reset.
  • <connector-string>(Required): A string that acts as the separator between the different levels of the counter (e.g., a dot . or a dash -).
  • <counter-style>(Optional): Defines the list style (e.g., decimal, lower-roman, upper-alpha). If omitted, it defaults to decimal.

Example

<div class="outline">
<ul>
<li>Fruit
<ul>
<li>Apples
<ul>
<li>Granny Smith</li>
<li>Honeycrisp</li>
</ul>
</li>
<li>Bananas</li>
</ul>
</li>
<li>Vegetables
<ul>
<li>Carrots</li>
<li>Leafy Greens
<ul>
<li>Spinach</li>
<li>Kale</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
/* 1. Initialize the counter at every level of the list */
.outline ul {
list-style-type: none; /* Hide default bullets */
counter-reset: section; /* Create a new instance of 'section' at this level */
}

/* 2. Increment the counter for every list item */
.outline li {
counter-increment: section;
}

/* 3. Display the nested counters using the ::before pseudo-element */
.outline li::before {
/* The counters() function takes two arguments:
- The counter name ('section')
- The separator string ('. ')
*/
content: counters(section, ".") " ";

/* Styling the numbers */
font-weight: bold;
color: #2a7ae2;
margin-right: 8px;
}

Browser Support

The following information will show you the current browser support for the CSS counters() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!