CSS can keep count of numbers without writing any additional JavaScript.
It does this by taking note the amount of time a CSS block affects a page then incrementing the counter for that block if the counter-increment
rule is implemented.
For example:
With no JavaScript at all, CSS understands that whenever there is an invalid element, it should increment the count for the invalid-count
identifier.
Structure of CSS counters
CSS counter is a CSS rule that specify the increment-counter
property and an indentifier for the counter to increment.
To know how many HTML
exists in a page:
Since CSS would match every h1
on the page, the increment-counter
would be executed, thereby increasing the count for the total-h1-counter
.
Getting back to 0
A counter can go back to the 0
state by declaring the counter-reset: counter-identifier
CSS
rule
A use case here would be always reseting the number of total-h1-counter
on the page when the body of the document is matched, but incrementing when H1s are matched.
The CSS below helps to achieve that:
Rendering counter on a page.
We can use CSS Generated Content to make the value of a counter visible on the page. We can achieve this using pseudo elements(::before
, ::after
) through the content property.
Salut!