CSS Counter Feature
The CSS counter feature generates numeric content. To create a counter, you use the counter-reset property and define the variable name for the counter:
counter-reset: myCounter;
By default, the counter is set to 1. A initial value can be set by adding a number after the counter name:
counter-reset: myCounter 10;
To define multiple counters, add the names and optional initial values to the same counter-reset declaration:
counter-reset: myCounter 10 othercounter;
The code above creates a counter called myCounter with an initial value of 10 and a counter called othercounter with an initial value of 1.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style type="text/css">
body {
counter-reset: myCounter;
}
p:before {
content: counter(myCounter) ". ";
counter-increment: myCounter;
}
</style>
</head>
<body>
<p>CSS</p>
<p>HTML</p>
</body>
</html>
The counter value default format is like:
1, 2, 3, ...
Other numeric formats can be defined as :
content: counter(myCounter, lower-alpha) ". ";
lower-alpha is the style you want to use.
The style value is the same with the list-style-type property.
Counters are incremented by one by default. To specify a different increment:
counter-increment: myCounter 2;
HTML CSS Book
CSS
- ::first-line Selector
- ::first-letter Selector
- :before Selector
- :after Selector
- CSS Counter Feature
- :root Selector
- Child Selectors
- :first-child Selector
- :last-child Selector
- :only-child Selector
- :only-of-type selector
- :nth-child(n)
- :nth-last-child(n)
- :nth-of-type(n)
- :nth-last-of-type(n)
- Enabled Elements with :enabled
- Disabled Elements with :disabled
- Checked Elements with :check
- Default Elements with :default
- Valid Elements with :valid
- Invalid input Elements with :invalid
- :in-range selector
- :out-of-range selector
- :required selector
- :optional selector
- :link selector matches hyperlinks.
- :visited selector matches visited hyperlinks.
- :hover Selector
- :active Selector
- :focus Selector
- Negation Selector not
- :empty Selector
- :lang Selector
- :target Selector