CSS Counters
Counters in CSS are basically variables that 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 a counter to give order elements in some other fashion.
CSS counters properties: CSS counters contain 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 the 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 increment 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:
<!DOCTYPE html>
<html>
<head>
<title>CSS counter property</title>
<style>
body {
counter-reset: myCounter;
}
h4::before {
counter-increment: myCounter;
content: "Heading " counter(myCounter) ": ";
}
.Beginner {
color: #090;
font-size: 40px;
font-weight: bold;
text-align: center;
}
.gfg {
text-align: center;
font-size: 18px;
}
</style>
</head>
<body>
<div class="Beginner">w3wiki</div>
<div class="gfg">CSS counter property</div>
<h3>Example of automatic numbering with CSS counters</h3>
<h4>GeekforBeginner</h4>
<h4>Computer Science portal</h4>
<h4>Beginner</h4>
</body>
</html>
Output:
Nested CSS counters: The counter within a counter is known as a nested counter. Nested counters are used to create headings and subheadings. This example shows the use of CSS counters with nested tags. Different counters are used for different types of tags.
Example:
<!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) " ";
}
.Beginner {
color: #090;
font-size: 40px;
font-weight: bold;
text-align: center;
}
.gfg {
text-align: center;
font-size: 18px;
}
</style>
</head>
<body>
<div class="Beginner">w3wiki</div>
<div class="gfg">Nested counter property</div>
<h3>CSS counter example</h3>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h3>CSS counter example</h3>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h3>CSS counter example</h3>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
<h4>w3wiki</h4>
</body>
</html>
Output:
Supported Browsers:
- Google Chrome
- Edge version
- Firefox version
- Opera
- Safari
Contact Us