docs.rodeo

MDN Web Docs mirror

counter()

{{CSSRef}} 

The counter() CSS function returns a string representing the current value of the named counter, if there is one.

The counter() function is generally used within pseudo-element through the {{cssxref("content")}}  property but, theoretically, it can be used wherever a <string> value is supported.

{{EmbedInteractiveExample("pages/tabbed/function-counter.html", "tabbed-shorter")}} 

Syntax

/* Basic usage */
counter(counter-name);

/* changing the counter display */
counter(counter-name, upper-roman)

Counters have no visible effect by themselves. The counter() and {{cssxref("counters", "counters()")}}  functions are what make counters useful by returning developer-defined strings (or images).

Values

The counter() function accepts up to two parameters. The first parameter is the <counter-name>. The optional second parameter is the <counter-style>.

[!NOTE] To join the counter values when nesting counters, use the {{cssxref("counters", "counters()")}}  function, which provides an additional {{cssxref("string")}}  parameter.

Formal syntax

{{CSSSyntax}} 

Examples

lower-roman compared to lower-alpha

In this example, we display a counter using lower-roman and lower-alpha list styles.

HTML

<ol>
  <li></li>
  <li></li>
  <li></li>
</ol>

CSS

ol {
  counter-reset: count;
}
li {
  counter-increment: count;
}
li::after {
  content:
    "[" counter(count, lower-roman) "] == ["
    counter(count, lower-alpha) "]";
}

Result

{{EmbedLiveSample("lower-roman compared to lower-alpha", "100%", 150)}} 

Displaying a counter using three styles

In this example, we use the counter() function three times.

HTML

<ol>
  <li></li>
  <li></li>
  <li></li>
</ol>

CSS

We include the counter() function with three different counter styles, including the default decimal value. We’ve added padding to the list to provide space for the long ::marker string.

ol {
  counter-reset: listCounter;
  padding-left: 5em;
}
li {
  counter-increment: listCounter;
}
li::marker {
  content:
    "Item #" counter(listCounter) " is: ";
}
li::after {
  content:
    "[" counter(listCounter, decimal-leading-zero) "] == ["
    counter(listCounter, upper-roman) "]";
}

Result

{{EmbedLiveSample("Displaying a counter using three styles", "100%", 150)}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN