This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.
Smartphone icons created by Freepik - Flaticon
CSS counters are "variables" maintained by CSS whose values can be incremented by CSS rules (to track how many times they are used). Counters let you adjust the appearance of content based on its placement in the document.
CSS counters are like "variables". The variable values can be incremented by CSS
rules (which will track how many times they are used).
To work with CSS counters we will use the following properties:
To use a CSS counter, it must first be created with counter-reset.
The following example creates a counter for the page (in the body selector),
then increments the counter value for each <h2> element and adds "Section
<value of the counter>:" to the beginning of each <h2> element:
body { counter-reset: section; } h2::before { counter-increment: section; content: "Section " counter(section) ": "; }
body { counter-reset: section; } h1 { counter-reset: subsection; } h1::before { counter-increment: section; content: "Section " counter(section) ". "; } h2::before { counter-increment: subsection; content: counter(section) "." counter(subsection) " "; }
A counter can also be useful to make outlined lists because a new instance of a counter is automatically created in child elements. Here we use the
function to insert a string between different levels of nested counters:ol { counter-reset: section; list-style-type: none; } li::before { counter-increment: section; content: counters(section,".") " "; }