CSS | Counters

Counters in CSS are basically variables which can be used for numbering and values of CSS counters may be incremented by CSS rules. For example, CSS counters can be used to increment the numbering of the headings automatically. In HTML, <ol> tag is used to give the ordered numbers to list items but CSS contains counter to give order elements in some other fashion.

CSS counters properties: CSS counters contains the following properties:

  • counter-reset: It is used to reset a counter.
  • counter-increment: It basically increments a counter value.
  • content: It is used to generate content.
  • counter() or counters() function: The value of a counter can be displayed using either the counter() or counters() function in a content property. These two functions basically used to add the value of a counter to the element.

Initialization the CSS Counter: To use CSS counter property firstly it must be created with the counter-reset property and the first step is resetting the counter. The myCounter by default initialized to a value 0(zero) with the counter-reset property.
Syntax:

counter-reset: myCounter;

Incrementation and Use of CSS Counter: To incrementing the counter use CSS counter-increment property.
Syntax:

counter-increment: myCounter;

The counter() or counters() function in a content is used to display the content in a particular order.
Syntax:



content: counter(myCounter);

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>CSS counter property</title>
        <style>
            body {
                counter-reset: myCounter;
            }
              
            h4::before {
                counter-increment: myCounter;
                content: "Heading " counter(myCounter) ": ";
            }
            .geeks {
                color:#090;
                font-size:40px;
                font-weight:bold;
                text-align:center;
            }
            .gfg {
                text-align:center;
                font-size:18px;
            }
        </style>
    </head>
    <body>
        <div class = "geeks">GeeksforGeeks</div>
        <div class = "gfg">CSS counter property</div>
        <h3>Exmple of automatic numbering with CSS counters</h3>
        <h4>GeekforGeeks</h4>
        <h4>Computer Science portal</h4>
        <h4>Geeks</h4>
    </body>
</html>                                 

chevron_right


Output:
css counter property

Nested CSS counters: The counter within a counter is known as nested counter. Nested counter are used to create heading and subheadings. This example shows the use of CSS counters with nested tags. Different counter are used for different type of tags.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Nested css counter</title>
        <style>
            body {
                counter-reset: counter1;
            }
            h3 {
                counter-reset: counter2;
            }
            h3::before {
                counter-increment: counter1;
                content: counter(counter1) ". ";
            }
            h4::before {
                margin-left:40px;
                counter-increment: counter2;
                content: counter(counter1) "." counter(counter2) " ";
            }
            .geeks {
                color:#090;
                font-size:40px;
                font-weight:bold;
                text-align:center;
            }
            .gfg {
                text-align:center;
                font-size:18px;
            }
        </style>
    </head>
    <body>
        <div class = "geeks">GeeksforGeeks</div>
        <div class = "gfg">Nested counter property</div>
        <h3>CSS counter example</h3>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
          
        <h3>CSS counter example</h3>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
          
        <h3>CSS counter example</h3>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
        <h4>GeeksforGeeks</h4>
    </body>
</html>                    

chevron_right


Output:
nested counter property



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.