Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Counter CSS: Counter - Counter in CSS

Understand Counter CSS: Counter - Counter in CSS with clear explanations, practical examples, and useful tips. This updated guide covers the essential...

Table of Contents

Counter CSS: Counter - Counter in CSS is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Counter - The CSS counter helps number objects to denote the information hierarchy on the website, helping to display information in a clear and understandable way for readers. This is a pretty useful feature, but not yet known by CSS.

Counter is like variable in programming. The value of the variable increases according to the rules of CSS. The counter allows you to adjust the content interface based on its location in the document.

Automatically number with the counter

CSS counter is like "variable". Variable values can be increased by CSS rules (which will track the number of times they are used).

To work with CSS counters, we will use the following properties:

  • counter-reset : Create Counter or Reset counter.
  • counter-increment: Increase counter value.
  • content - Insert content into the counter.
  • Counter () or counters () function - Add the counter value to the component.

To use the Counter, first create a counter-reset.

Example: Create a website with a counter, increase the value of the counter for the card

 

and insert the "Section:" content

 body { 
  counter-reset: section; 
 } 
 
 h2::before { 
  counter-increment: section; 
  content: "Section " counter(section) ": "; 
 } 

Counter CSS: Counter - Counter in CSS example image 1

Full code:

Use example Counter - CSS counter:

Website TipsMake.com

Technology Village

Technology

Science

Nested counters

the example below creates a counter for the page (main - section) and a counter for each element

 

(subsection - subsection). Section counter will be counted for each element

 

with the " Section " content, and the subsection counter will be counted for each element

 

with "." :

 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) " "; 
 } 

Counter CSS: Counter - Counter in CSS example image 2

Full code:

Website TipsMake.com

Category Technology Village

Technology story

Attack the network

Technology category

Program

System

Science Category

Mystery - Strange story

Explore nature

In addition, you can use the counter in CSS to create lists, which are also useful and appropriate, which can automatically number the list as you wish.

As an example,, you create a list with a counter as follows, use the extra function counters () to decentralize nested counters:

 ol { 
  counter-reset: section; 
  list-style-type: none; 
 } 
 
 li::before { 
  counter-increment: section; 
  content: counters(section,".") " "; 
 } 

Counter CSS: Counter - Counter in CSS example image 3

Full code:

  • Technology Village
  • Technology
  • System
  • Application
  • Program
  • CSS and CSS3
  • Python
  • SQL Server
  • Game - Games
  • Science
  • Life
  • About TipsMake.com
  • Contact TipsMake.com

Next lesson: Design Layout - Website layout in CSS

FAQ

What should you know about automatically number with the counter?

CSS counter is like "variable". Variable values can be increased by CSS rules (which will track the number of times they are used).

What should you know about use example Counter - CSS counter: Website TipsMake.com Technology Village Technology Science Nested counters?

The example below creates a counter for the page (main - section) and a counter for each element.

What should you know about website TipsMake.com Category Technology Village Technology story Attack the network Technology category Program System Science Category Mystery - Strange story Explore nature?

In addition, you can use the counter in CSS to create lists, which are also useful and appropriate, which can automatically number the list as you wish.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.