counters()
Quick Summary for counters()
The counters() CSS function enables nested counters, returning a concatenated string representing the current values of the named counters, if there are any. The counters() function has two forms: counters(name, string) or counters(name, string, style). It is generally used with pseudo-elements, but can be used, theoretically, anywhere a <string> value is supported. The generated text is the value of all counters with the given name, from outermost to innermost, separated by the specified string. The counters are rendered in the style indicated, defaulting to decimal if no style is specified.
Code Usage for counters()

<pre class="brush: css notranslate"><code><span class="token comment">/ Simple usage - style defaults to decimal /</span> <span class="token function">counters</span><span class="token punctuation">(</span>countername<span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/ changing the counter display /</span> <span class="token function">counters</span><span class="token punctuation">(</span>countername<span class="token punctuation">,</span> <span class="token string">'.'</span><span class="token punctuation">,</span> upper-roman<span class="token punctuation">)</span> </code></pre>

More Details for counters()

counters()

The counters() CSS function enables nested counters, returning a concatenated string representing the current values of the named counters, if there are any. The counters() function has two forms: counters(name, string) or counters(name, string, style). It is generally used with pseudo-elements, but can be used, theoretically, anywhere a <string> value is supported. The generated text is the value of all counters with the given name, from outermost to innermost, separated by the specified string. The counters are rendered in the style indicated, defaulting to decimal if no style is specified.

/* Simple usage  - style defaults to decimal */ counters(countername, '-');  /* changing the counter display */ counters(countername, '.', upper-roman) 

A counter has no visible effect by itself. The counters() function (and counter() function) is what makes it useful by returning developer defined content.

Note: The counters() function can be used with any CSS property, but support for properties other than content is experimental, and support for the type-or-unit parameter is sparse.

Check the Browser compatibility table carefully before using this in production.

Syntax

Values

<custom-ident>

A name identifying the counters, which is the same case-sensitive name used for the counter-reset and counter-increment. The name cannot start with two dashes and can't be none, unset, initial, or inherit.

<counter-style>

A counter style name or symbols() function, where a counter style name is a numeric, alphabetic, or symbolic simple predefined counter style, a complex longhand east Asian or Ethiopic predefined counter style, or other predefined counter style. If omitted, the counter-style defaults to decimal

<string>

Any number of text characters. Non-Latin characters must be encoded using their Unicode escape sequences: for example, \000A9 represents the copyright symbol.

Formal syntax

counters( <counter-name>, <string>, <counter-style>? )

where <counter-name> = <custom-ident><counter-style> = <counter-style-name> | symbols()

where <counter-style-name> = <custom-ident>

Examples

default value compared to upper Roman

HTML
<ol>   <li>      <ol>         <li></li>         <li></li>         <li></li>       </ol>   </li>   <li></li>   <li></li>   <li>      <ol>         <li></li>         <li>            <ol>               <li></li>               <li></li>               <li></li>            </ol>         </li>       </ol>   </li> </ol> 
CSS
ol {   counter-reset: listCounter; } li {   counter-increment: listCounter; } li::marker {    content:  counters(listCounter, '.', upper-roman) ') '; } li::before {   content:  counters(listCounter, ".") " == " counters(listCounter, ".", lower-roman) ; } 
Result

decimal-leading-zero compared to lower-alpha

HTML
<ol>   <li>      <ol>         <li></li>         <li></li>         <li></li>       </ol>   </li>   <li></li>   <li></li>   <li>      <ol>         <li></li>         <li>            <ol>               <li></li>               <li></li>               <li></li>            </ol>         </li>       </ol>   </li> </ol> 
CSS
ol {   counter-reset: count; } li {   counter-increment: count; } li::marker {    content: counters(count, '.', upper-alpha) ') '; } li::before {   content: counters(count, ".", decimal-leading-zero) " == " counters(count, ".", lower-alpha); } 
Result

Specifications

Specification
CSS Lists and Counters Module Level 3 # counter-functions

See also

Using CSS Counters counter-set counter-reset counter-increment @counter-style CSS counter() function ::marker

Last modified: Jan 7, 2022, by MDN contributors

Select your preferred language English (US)Français日本語中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote


Me
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.