CSS counter() Function
Example
Create a counter in the page (in the body selector), increase the counter value of each
element, and add the text before each
element: "Section counter value":
body
{
counter-reset:
section
;
/*
Reset the counter to 0
*/
}
h3
:before
{
counter-increment:
section
;
/*
Increase the counter value
*/
content:
"
Section
"
counter
(
section
) ": "
;
/*
Display the counter
*/
}
Try it »
Definition and usage
The counter() function returns the current counter value as a string.
The counter value is displayed on the page by using counter-reset and counter-increment operations and applying the counter() function to content.
Supported version: CSS3
Browser support
| function | |||||
|---|---|---|---|---|---|
| counter() | Supported | Supported | Supported | Supported | Supported |
CSS Syntax
counter(countername, counterstyle)
| Value | Description |
|---|---|
| countername | Required. The name of the counter (the same name used by the counter-reset and counter-increment properties). |
| counterstyle | Optional. The style of the counter (can be a CSS list-style-type property value). |
More examples
Example
Set the counter style:
body
{
counter-reset:
section
;
}
h2
:
:before
{
counter-increment:
section
;
content:
"
Section
"
counter
(
section
,
upper-roman
) ": "
;
}
Try it »
Example
Set up nested counters:
ol
{
counter-reset:
section
;
/*
Create a new counter instance for each ol element
*/
list-style-type:
none
;
}
li
:before
{
counter-increment:
section
;
/*
Increase only the current instance of the counter
*/
content:
counters
(
section
, ".") " "
;
/*
Increase all counter instances with values separated by "."
*/
}
Try it »
CSS Functions