×

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much they are applied.

These properties in CSS provide simple incrementing i.e., CSS based and representation of any number i.e., for various generated content.

Note: Before going to start using a counter, it should be made with the counter-reset.

Counter Properties

Some of the critical properties of the counter are listed and specified below:

counter-reset: This property is applied to reset or create a counter.

counter-increment: This attribute is applied to the counter values to increment them.

content: It is applied to add generated content.

counters() or counter() function: This property is applied to insert any counter value for an element.

Counter Example:

In the following example, we are incrementing every next element’s counter value and also make the counter to a page.

Look at the following example:

<!DOCTYPE html>
<html>
<head>
<style>
body
{
 counter-reset: section;
}
h2::before
{
 counter-increment: section;
 content: "Section" counter(section)": ";
}
</style>
</head>
<body>
<h1> CSS Counters Examples are as follows: </h1>
<h2> CSS Tutorial </h2>
<h2> HTML Tutorial </h2>
<h2> C Tutorial </h2>
<h2> Java Tutorial </h2>
<p><strong>Note:
</strong> If a !DOCTYPE is described then only IE8 supports these properties.</p>
</body>
</html>

Explanation of above example- we can see that the counter is made for a page inside body selector. After that, it increments the value of the counter for every <h2> element and inserts “Section <value of the counter>:” for the starting of every <h2> element.

Output:

Nesting Counters

We can also make counters inside the counter, and known as counter nesting.

Look at the following example to understand the concept of nesting counter:

<!DOCTYPE html>
<html>
<head>
<style>
body
{
 counter-reset: section;
}
h2
{
 counter-reset: subsection;
}
h2::before
{
 counter-increment: section;
 content: "Section" counter(section)": ";
}
h3::before
{
 counter-increment: subsection;
 content: counter(section) "." counter(subsection) " ";
}
</style>
</head>
<body>
<h2> Web technology tutorials: </h2>
<h3> CSS Tutorial </h3>
<h3> HTML Tutorial </h3>
<h3> jQuery Tutorial </h3>
<h3> Bootstrap Tutorial </h3>
<h2> Java tutorials: </h2>
<h3> Core Java Tutorial </h3>
<h3> Servlet Tutorial </h3>
<h3> JSP Tutorial </h3>
<h3> Spring Tutorial </h3>
<h3> Hibernate Tutorial </h3>
<h2> Database tutorials: </h2>
<h3> SQL Tutorial </h3>
<h3> MySQL Tutorial </h3>
<h3> PL/SQL Tutorial </h3>
<h3> Oracle Tutorial </h3>
<p><strong>Note:
</strong> If a !DOCTYPE is described then only IE8 supports these properties.</p>
</body>
</html>

Explanation of above example- we can see that the counter is made for a section and other nesting counter called subsection is made inside section.

Output:

Distinct phases of a Nesting Counters

We can make an outlined list with the use of the nesting counters. The nesting counter provides us to add the string between distinct phases of the nested counters.

Look at the following example:

<!DOCTYPE html>
<html>
<head>
<style>
ol
{
    counter-reset: section;
    list-style-type: none;
}
li::before
{
    counter-increment: section;
    content: counters(section,".") " ";
}
</style>
</head>
<body>
<h2>Different Phases of the Nesting Counters</h2>
<ol>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item</li>
</ol>
</li>
<li>item</li>
</ol>
</li>
<li>item</li>
<li>item</li>
</ol>
<p><b>Note:</b> If a !DOCTYPE is described then only IE8 supports these properties.</p>
</body>
</html>

 Output:


Related Topics

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

How to Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

How to get rid/remove of bullet pioints in CSS

We include a list of items on the web pages that were sometimes numbered and sometimes bulleted in various styles. In HTML, there are two kinds of lists: ordered and unordered...

3 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.