CSS - @counter-styles



The @counter-styles CSS at-rule allows authors to create custom counter styles that aren't included in the standard styles. A @counter-styles rule outlines how to change a counter value into a string representation.

Syntax

@counter-style = @counter-style <counter-style-name> { <declaration-list> }  

Every @counter-style has a name and a set of descriptors associated with it.

Descriptors

Descriptors Meaning
system Specifies the method for turning the integer value of a counter into a string form.
negative Allows the author to add symbols at the start or end of the counter representation if the value is negative.
prefix It defines a symbol to be added at the beginning of the marker representation.
suffix It defines a symbol to be added after the marker representation.
range It specifies the range of values for which the counter style is valid.
pad This is used when you want marker representations to have a minimum length.
fallback Specifies a fallback system to use if the defined system can't create the representation of a counter value or if the value is beyond the specified range.
additive-symbols It allows you to define symbols when the @counter-style system descriptor is set to additive.
symbol Determines the symbols that are used for marker representations.
speak-as Specifies how screen readers and speech synthesizers should vocalize the counter style.
The descriptors Symbol and Speak-as are not supported or partially supported by most of the browsers.

Example

  • The following example demonstrates the usage of the @counter-styles property.

  • In this code, we define a custom counter style named cyclic-counter using the @counter-style rule.

  • We specify the cyclic system, which cycles through the symbols Ⓐ, Ⓑ, Ⓒ, and Ⓓ.

  • We also set a space as the suffix for the counter.

<html>
<head>
<style>
   /* Define the counter style */
   @counter-style cyclic-counter {
      system: cyclic;
      symbols: Ⓐ Ⓑ Ⓒ Ⓓ ;
      suffix: " ";
   }
   /* Apply the counter style to the elements */
   .democounter {
      counter-reset: my-counter;
      
   }
   .democounter-item::before {
      counter-increment: my-counter;
      content: counter(my-counter, cyclic-counter);
      color: red;
   }
</style>
</head>
<body>
   <div class="democounter">
   <div class="democounter-item">Item 1</div>
   <div class="democounter-item">Item 2</div>
   <div class="democounter-item">Item 3</div>
   <div class="democounter-item">Item 4</div>
   <div class="democounter-item">Item 5</div>
   <div class="democounter-item">Item 6</div>
   <div class="democounter-item">Item 7</div>
   <div class="democounter-item">Item 8</div>
   <div class="democounter-item">Item 9</div>
   <div class="democounter-item">Item 10</div>
   </div>
</body>
</html>

Example

  • This program demonstrates the usage of the @counter-styles property.

  • The @counter-style rule is used to define a new counter style named alphabetic.

  • The system property is set to alphabetic to specify that the counter should use alphabetic characters.

  • The symbols property is used to define the list of alphabetic characters from 'a' to 'c'.

  • The ul element is styled with the list-style property set to alphabetic, which applies the alphabetic counter style to the list.

<html>
<head>
<style>
   @counter-style alphabetic {
   system: alphabetic;
   symbols: 'a' 'b' 'c';
   }
   ul {
   list-style: alphabetic;
   }
</style>
</head>
<body>
  <ul>
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
   <li>Item 4</li>
   <li>Item 5</li>
   <li>Item 6</li>
   <li>Item 7</li>
   <li>Item 8</li>
   <li>Item 9</li>
  </ul>
</body>
</html>
Advertisements