How to create numbering using counter-increment property in CSS ?
Last Updated :
24 Apr, 2023
This article will give you an idea about how to use counter-reset and counter-increment properties in CSS. We can use counter-reset and counter-increment properties to set count to the HTML elements in the webpage.
Need for using counter-increment Property:
This property can be used to set to increment/decrement value of a counter. For organizing the data properly, it is necessary to format it to make a webpage attractive. We can set count to the HTML elements in several ways. The simplest way is that we can set numbers manually, which will be not a good practice. We can use the HTML list that can be either an ordered list or an unordered list. Although, there are a few drawbacks of using the list over the CSS counter properties, which are given below:
- We can use the CSS counter to set counts to the complex list.
- Users can add a custom design to the counts, whereas numbered list doesn't allow for customization.
- Users can set the starting point of the counter.
- The most useful feature is that we can set the incremental value using CSS counter, whereas numbered list only increases the value by 1.
There are several ways of numbering with CSS counters.
Syntax:
- Initialize span-count variable.
counter-reset: Span-count;
- Increase span-count variable.
counter-increment: Span-count;
content: counter(Span-count);
Attributes: The following properties are contained by the CSS counter:
- Counter-reset: To Initialize the counter variable. The default value of the counter variable is 0. Also, we can set its value manually. It is required to create the counter-reset variable before we use counter-increment in the webpage.
- Counter-increment: To increment counter variable. The default increment value is 1. We can also set it up manually.
- Counter (counter_variable): This function takes the counter variable as an argument. It adds the count before or after to the HTML element.
- Counters(counter_variable): It is the same as the counter() function. We can use it to implement the nested counters.
- Content: To set count to the content of the HTML element.
Example: This is a simple example that demonstrates to use of CSS counters.
HTML
<!DOCTYPE html>
<html>
<head>
<title>CSS counters</title>
<style>
/* Initializing the counter variable */
body {
counter-reset: Span-count;
}
/* Incrementing the value for counter variable */
span {
counter-increment: Span-count;
color: green;
}
/* Adding counter to the content */
span:before {
content: counter(Span-count) ". ";
}
</style>
</head>
<body>
<span>GeeksforGeeks</span> <br>
<span>GFG</span>
</body>
</html>
Output:
CSS CounterChanging starting and incremental value of CSS counter: The default starting and incremental values for the CSS counter are 0 and 1 respectively. We can change the default starting value by setting up the value of the counter-reset variable. No matter what value we set for the counter-reset variable, it will begin counting from the value which is the sum of the initial value and incremental value. For example, If we initialize the counter-reset variable with 1 and counter-increment to 2, it will start the numbering from 3.
Syntax:
counter-reset: Span-count -1;
Syntax:
To change the incremental value of the CSS counter, we need to set the value of the counter-increment properties.
counter-increment: Span-count 2;
Example: This code demonstrates to change the default value of the counter-reset and counter-increment variable.
HTML
<!DOCTYPE html>
<html>
<head>
<title>CSS counters</title>
<style>
/* Initialize the counter-reset variable with -2*/
body {
counter-reset: Span-count -2;
}
/* Changing incremental value to 2 */
span {
counter-increment: Span-count 2;
color: green;
}
span:before {
content: counter(Span-count) "- ";
}
</style>
</head>
<body>
<span>GeeksforGeeks</span><br>
<span>GFG</span>
</body>
</html>
Output:
Incrementing the counter by 2 using CSS CounterSet count format: Another interesting feature of the CSS counter is that it provides different count formats. The default format for the counters is the decimal format. We can change the default format to lower-latin, lower-greek, and lower-roman.
Syntax: We need to change only the content property as shown below, to change the format of the counts.
content: counter(Span-count, values);
Attributes values:
- decimal: Represent the numeric values (e.g., 1,2,3,..)
- lower-latin: To set alphabet as a count.
- lower-greek: To set greek symbols as a count.
- lower-roman: To set roman numbers as a count.
Example: This example demonstrates to change the format of the counts of the CSS counter.
HTML
<!DOCTYPE html>
<html>
<head>
<title>CSS counters</title>
<style>
/* Initialize the counter variable */
body {
counter-reset: Span-count;
}
/* Increment the value for counter variable */
span {
counter-increment: Span-count;
color: green;
}
/* Adding counter to the content
changing count format to lower-roman*/
span:before {
content: counter(Span-count, lower-roman) "- ";
}
</style>
</head>
<body>
<span>GeeksforGeeks</span><br>
<span>GFG</span>
</body>
</html>
Output:
Setting the counter format in a lower romanNested counters: The counter within a counter is known as a nested counter. Nested counters are used to create headings and subheadings. Using CSS nested counters makes it easy instead of complex numbered lists. While numbering nested data, we need to add counts like 1.1, 1.2, 1.1.2, etc. We can accomplish the same thing by using CSS counters. Here, we will know the use of the counters() function.
Example: This example demonstrates nested counts using CSS counter. When a new div will start, the counter-reset variable recreates itself and reinitialize with the starting value.
HTML
<!DOCTYPE html>
<html>
<head>
<title>CSS counters</title>
<style>
/* Resetting the counter variable inside div */
div {
counter-reset: counter_p;
}
/* Creating the nested list */
p:before {
counter-increment: counter_p;
content: counters(counter_p, ".") ". ";
}
p {
color: green;
}
</style>
</head>
<body>
<div>
<p>HTML List</p>
<div>
<p>Ordered List</p>
<p>Unordered List</p>
</div>
<p>Dictionary</p>
</div>
</body>
</html>
Output:
Nested Counters
Similar Reads
How to Create Section Counter using HTML and CSS ?
Section counter is like a card and is useful for webpage footer. It contains details about the company. In this article, we will introduce some predicted data about some companies. We divide this article into two sections, in the first section we will create the normal structure then we will work on
3 min read
CSS counter-increment Property
The CSS counter-increment Property is used to increment/decrement value of a counter. A CSS counter is a variable that is used to track how many times a variable is used. Syntax: counter-increment: none | identifier | initial | inherit;Default Value: none Property values:none: This is the default va
4 min read
How to create a simple counter Using ReactJS?
React counter app is a program that allows users to interact with a numerical counter value. It demonstrates basic state management and user interaction within a user interface.Prerequisites:NPM & Node.js React JSReact useState hookApproach:Building a simple counter application is a great way to
2 min read
How to Create a Simple Counter App using Next.js ?
This article explains the process of creating a simple counter application using Next.js. We will see how to build a functional counter with increment and decreÂment buttons. In a typical scenario, imagine the need to create a user-friendly counteÂr application. The application would require buttons
3 min read
How to create timeline using CSS?
We can easily create a timeline using some basic HTML and CSS. HTML Code is used to create a basic structure of the timeline and CSS code is used to set the style. HTML Code: In this section, we will create a structure of our timeline. Our structure will include four events. Steps: Create a div elem
4 min read
How to Create Notification Buttons using CSS?
Notification buttons are frequently used in web applications to inform users of new messages, updates, or alerts. These buttons typically feature a badge or icon that displays the number of notifications, enhancing user awareness and engagement. Here we will create notification buttons using CSS. Ap
2 min read
How to Create Style Labels using HTML and CSS ?
Creating style labels using HTML and CSS involves designing form labels that enhance user interaction. By applying CSS properties like color, font, and spacing, you can improve the appearance of labels associated with input fields, making forms more visually appealing and user-friendly.Creating Styl
1 min read
How to create a Number Input using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops In this article, we will be creating a number input area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ h
1 min read
How to create Text Buttons using CSS ?
In this article, we are going to see how to create text buttons using CSS. The text button is a button that appears to be text but acts as a button if we press it. Text buttons are different from anchor tags even if they might look similar. To create text buttons first, we create simple buttons in H
2 min read
How to Reverse Custom Counters using CSS ?
Counters in CSS are basically variables which can be used for numbering and values of CSS counters may be incremented by CSS rules. Follow the article, you will get to know 2 methods to reverse custom counter using CSS. We will be going to use the following two approaches to reverse counters: In app
3 min read