How to Reverse Custom Counters using CSS ?
Last Updated :
25 Aug, 2021
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 approach 1 we will use how to reverse simple counter using reverse property in HTML.
- In approach 2 we will finally solve our problem statement on how to reverse Custom Counters using CSS.
Approach 1: Reverse counter using reversed attribute in HTML.
Firstly, we will assume that we know the total no.of items in the list because we will instruct the counter to start numbering from total no.of items and keeps on decreasing by one till 0(zero) and through this we can achieve the numbering in reverse order.
Note: The HTML <ol> reversed Attribute is a Boolean Attribute and used to ordered the list in Descending Order(9, 8, 7, 6 …..) instead of ascending order(1, 2, 3 ….).
Syntax:
<ol reversed>
<li> Content... </li>
<li> Content... </li>
...
<li> Content... </li>
</ol>
Below is the implementation of the above approach.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<p>
Below list is numbered in Reverse
order By using reverse property
</p>
<ol reversed>
<li>Interview Preparation</li>
<li>Content Writing</li>
<li>SDE at GeeksforGeeks</li>
</ol>
</body>
</html>
Output:
Above example is not a custom counter. It is a built-in method to reverse the counter. Follow the below approach to customize the order of the counter.
Approach 2: Using custom counter to reverse a counter. Below is the step-by-step implementation on how to use custom counter.
Step 1: At first need to disable the default counters so that no marker will be shown and this can be done by setting the CSS property list-style-type to none.
Step 2: We need to create a counter in CSS to track the number of items in the list using counter-reset which creates or resets a counter and set the counter-reset = ( total number of element in the list + 1). Adding one more element is due to counter() rule.
counter-reset: myCounter (total number of element in the list + 1);
Step 3: Use the counter() function in a content which is used to display the content in a particular order.
content: counter(myCounter);
Step 4: At last, to increment the CSS counter. You need to add counter-increment Property which is used to increment/decrement value of a counter and set the counter-increment = -1 , which use to decrease the counter by 1.Actually counter-increment property increases the integer by 1, but here we will alter this property to decrease the integer by 1.
counter-increment: myCounter -1;
Example 2:
HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
}
/* STEP-1 and STEP-2 */
ol {
/* Disable the default counters */
list-style-type: none;
/* Creates a counter */
counter-reset: myCounter 5;
}
/* STEP-3 and STEP-4 */
ol li::before {
/* To display the content in
a particular order. */
content: counter(myCounter) ".";
/* Decrease the counter by 1.*/
counter-increment: myCounter -1;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<p>
Below list is numbered in Reverse
order By Using Custom Counter.
</p>
<ol class="counter">
<li>Data Structure</li>
<li>Web Development</li>
<li>Competitive Programming</li>
<li>DBMS</li>
</ol>
</body>
</html>
Output:
Similar Reads
How to use @counter-style rule to customize list item using CSS ?
In this article, we will see how the @counter-style rule can be used to customize each list item style in CSS. Most of the cases while developing web applications and web projects, we often make use of lists, i.e., an ordered list or unordered list, or a description list. Although, there exist some
3 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 Notes using CSS?
In web design, a "note" is a styled container that stands out from the surrounding text and is frequently used to highlight particular information. We can make visually appealing notes that improve user experience with CSS.ApproachThe HTML structure includes 'div' elements to create notes. The CSS d
3 min read
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
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 customize the numbers of an ordered list using CSS ?
In this article, we will learn how to customize (decorate) the numbers in an ordered list. We use counter-increment property to decorate the numbers. Approach: It is difficult to add CSS styles to <ol> elements directly to customize the numbers. Instead, we access the range of the ordered list
2 min read
How to format a number as currency using CSS ?
Given a number and the task is to format the number of an HTML element as currency using CSS and a little bit of JavaScript. It is not possible to complete the task with pure CSS. A bit of JavaScript is needed for the parsing of the number to add the commas. Approach: In the code, the CSS class curr
1 min read
Create a Reverse Scrolling Effect using HTML and CSS
Creating an attractive UI with a reverse scroll effect can be an engaging design. The Reverse Scrolling effect facilitates a unique and visually interactive experience for users, which breaks the conventional top-down scrolling system to which the user is accustomed. In this article, we'll create a
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 Perspective Text using HTML & CSS ?
In this article, we are going to create an illusion of images below the main image. This is a simple article, we can achieve our target only by using HTML & CSS. Approach: Create a main div in which we have added a heading tag and then use the selectors in CSS to create the effects. HTML Code: F
2 min read