Open In App

How to define a number sections and sub-sections with section in CSS ?

In this article, we will learn how to define a number of sections and sub-sections with the section in CSS. The section tag defines the section of documents such as chapters, headers, footers, or any other sections. The section tag divides the content into sections and subsections.

Counters in CSS are basically variables that can be used for numbering and values of CSS counters may be incremented by CSS rules. So to solve this task, we will use the CSS counters properties which are the following.



By following steps we can define a number of sections and sub-sections with the section in CSS:

HTML Code: Below is the full implementation of the above approach.




<!DOCTYPE html>
<html>
<head>
    
    <style>
        div{
            color: green;
            font-size: 50px;
            margin: 50px 50px;
        }
        body {
                counter-reset: counter1;
            }
            h2 {
                counter-reset: counter2;
            }
            h2:before {
                counter-increment: counter1;
                content: "Section " counter(counter1) ". ";
            }
            h3:before {
                counter-increment: counter2;
                content: counter(counter1) "." counter(counter2) " ";
            }
    </style>
</head>
<body>
    <div>GeeksforGeeks</div>
    <h2>CSS</h2>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
    <h2>CSS</h2>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
    <h2>CSS</h2>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
    <h3>GeeksforGeeks</h3>
</body>
</html>

Output:

Counter output


Article Tags :