counter()
Quick Summary for <counter>
The counter() CSS function returns a string representing the current value of the named counter, if there is one. It is generally used in the content property of pseudo-elements, but can theoretically be used anywhere a <string> value is supported.
Code Usage for <counter>
/* Simple usage */ counter(countername);  /* changing the counter display */ counter(countername, upper-roman) 
More Details for <counter>

counter()

The counter() CSS function returns a string representing the current value of the named counter, if there is one. It is generally used in the content property of pseudo-elements, but can theoretically be used anywhere a <string> value is supported.

/* Simple usage */ counter(countername);  /* changing the counter display */ counter(countername, upper-roman) 

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

Syntax

Values

<custom-ident>

A name identifying the counter, 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 <list-style-type> name, <@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.

Formal syntax

counter( <counter-name>, <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></li>   <li></li>   <li></li> </ol> 
CSS
ol {   counter-reset: listCounter; } li {   counter-increment: listCounter; } li::after {   content: "[" counter(listCounter) "] == ["                counter(listCounter, upper-roman) "]"; } 
Result

decimal-leading-zero compared to lower-alpha

HTML
<ol>   <li></li>   <li></li>   <li></li> </ol> 
CSS
ol {   counter-reset: count; } li {   counter-increment: count; } li::after {   content: "[" counter(count, decimal-leading-zero) "] == ["                counter(count, lower-alpha) "]"; } 
Result

Specifications

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

See also

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

Last modified: Feb 3, 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
1. Show your work to the world instead of keeping in your head💆. 2. Do the work consistently👌 3. Respect your work🥰 4. Don't postpone your work 5. Make mistakes 🔥🔥🔥
Unknown
Random CSS Property

scrollbar-gutter

The scrollbar-gutter CSS property allows authors to reserve space for the scrollbar, preventing unwanted layout changes as the content grows while also avoiding unnecessary visuals when scrolling isn't needed.
scrollbar-gutter css reference