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. All the descriptors are listed at the end of this topic.

CSS @counter-styles - Symbols Counter Style

This program demonstrates the usage of the @counter-styles property using symbols. 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>

CSS @counter-styles - Alphabetic Counter Styles

Following example demonstrates usage of the @counter-styles property using alphabets.

  • 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>

Descriptors

The table given below lists all the descriptors related to @counter-styles

Descriptor Description
system Specifies the method for turning the integer value of a counter into a string form.
additive-symbols Used to define counter symbols when the @counter-style system descriptor value is configured as additive.
negative Allows the author to add symbols at the start or end of the counter representation if the value is negative.
prefix Defines a symbol to be added at the beginning of the marker representation.
suffix Defines a symbol to be added after the marker representation.
range 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.
speak-as This descriptor explains how to interpret the counter style for speech synthesizers like screen readers.
symbols Defines the symbols used for marker representations.
The descriptors symbols and speak-as are not supported by most of the browsers.
Advertisements