Skip to content
Related Articles
Open in App
Not now

Related Articles

CSS | Counters

Improve Article
Save Article
Like Article
  • Last Updated : 06 Jul, 2022
Improve Article
Save Article
Like Article

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: 

html




<!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>Example of automatic numbering with CSS counters</h3>
        <h4>GeekforGeeks</h4>
        <h4>Computer Science portal</h4>
        <h4>Geeks</h4>
    </body>
</html>                                


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: 

html




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


Output:

 nested counter property

Supported Browsers:

  • Google Chrome 1.0 and above
  • Edge version 12 and above
  • Firefox version 1.5 and above
  • Internet Explorer 8.0 and above
  • Opera 10.0 and above
  • Safari 3.0 and above

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!