Loading lesson path
Overview
With CSS counters, you can create dynamic numbering of elements (like headings, sections, or list items) without using JavaScript. CSS counters are "variables" maintained by CSS, and their values can be incremented (or decremented) by CSS rules.
CSS counters are like "variables". The variable values can be incremented (or decremented) by CSS rules. To work with CSS counters we will use the following properties:
Formula
counter - reset
- Creates or resets a counter counter - incrementFormula
To use a CSS counter, it must first be created with the counter - reset property.The following example creates a counter for the page (in the body selector), then it increments the counter value by 1 for each <h2> element:
Example body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}The counter-increment property has a second parameter. The default value is 1. To decrease the counter value, you can set it to -1.
Example body {
counter-reset: section;
}
h2::before {Formula
counter - increment: section-1;
content: "Section " counter(section) ": ";
}You can increment the counter by any value. Here we increment by 2:
Example body {
counter-reset: section;
}
h2::before {Formula
counter - increment: section2;
content: "Section " counter(section) ": ";
}