Javatpoint Logo
Javatpoint Logo

CSS Counters

CSS counters are similar to variables. These are maintained by CSS and those values can be incremented by CSS rules to track how many times they are used.

CSS counters facilitate simple CSS based incrementing and display of a number for generated content.

CSS Counter Properties

Following is a list of properties that are used with CSS counter:

  • counter-reset: It is used to create or reset a counter.
  • counter-increment: It is used to increment the counter value.
  • content: It is used to insert generated content.
  • counter() or counters() function: It is used to add the value of a counter to an element.

Note: Before using a CSS counter, it must be created with counter-reset.

CSS Counter Example

Let's take an example to create a counter for a page and increment the counter value for each next element.

See this example:

Test it Now

Note: In the above example you can see that a counter is created for the page in the body selector and it increments the counter value for each <h2> element and adds "Section <value of the counter>:" to the beginning of each <h2> element.


CSS Nesting Counters

You can also create counters within the counter. It is called nesting of a counter. Let's take an example to demonstrate nesting counter.

See this example:

Test it Now

Note: In the above example you can see that a counter is created for the section and another nesting counter named subsection is created within section.


Different level of nesting counters

You can create outlined lists by using nesting counters. It facilitates you to insert a string between different levels of nested counters.

See this example:

Test it Now
Next TopicCSS clearfix





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA