CSS COUNTERS

rainbow baker

 CSS COUNTERS

  • CSS counters are like "variables". The variable values can be incremented by CSS rules (which will track how many times they are used).

There are four types of css counters properties:
  • Counter reset- Creates or resets a counter
  • Counter increment - Increments a counter value
  • Content - Inserts generated content
  • Counter() or Counters()function - Adds the value of a counter to an element

Nesting Counters

  • The following example creates one counter for the page (section) and one counter for each <h1> element (subsection). 
  • The "section" counter will be counted for each <h1> element with "Section <value of the section counter>.", and the "subsection" counter will be counted for each <h2> element with "<value of the section counter>.<value of the subsection counter>":

EXAMPLE

<!DOCTYPE html>
<html>
<head>
<style>
body
 {
  counter-reset: section;
}
h2::before 
{
  counter-increment: section;
  content: "Section " counter(section) ": ";
}
</style>
</head>
<body>
<h1>Using CSS Counters</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h2>JavaScript Tutorial</h2>
<h2>Python Tutorial</h2>
<h2>SQL Tutorial</h2>
</body>
</html>

OUTPUT:

Using CSS Counters

Section 1: HTML Tutorial

Section 2: CSS Tutorial

Section 3: JavaScript Tutorial

Section 4: Python Tutorial

Section 5: SQL Tutorial

        



Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send