Use CSS counters
Use counter
Value of operation counter
counter-reset : section ;
counter-reset : section page 3 topic ;
h3::before {
counter-increment : section ; /* Increment the value of section counter by 1 */
}
Display counter
h3::before {
counter-increment : section ; /* Increment the value of section counter by 1 */
content : "Section " counter ( section ) ": " ; /* Display counter value in default style (decimal) */
}
1 One 1 Nested one 2 Nested two 2 Two 1 Nested one 2 Nested two 3 Nested three 3 Three
1 One 1.1 Nested one 1.2 Nested two 2 Two 2.1 Nested one 2.2 Nested two 2.3 Nested three 3 Three
Reverse counter
counter-reset : reversed ( section ) ;
List item counter
Example
Basic example
CSS
body {
counter-reset : section ; /* Set a counter named 'section', and its initial value is 0. */
}
h3::before {
counter-increment : section ; /* Increment the value of section counter by 1 */
content : "Section " counter ( section ) ": " ; /* Display the word 'Section ', the value of section counter, and a colon before the content of each h3 */
}
HTML
< h3 > Introduction </ h3 >
< h3 > Body </ h3 >
< h3 > Conclusion </ h3 >
result
Basic example: reverse counter
CSS
body {
counter-reset : reversed ( section ) ; /* Set a counter named 'section', and its initial value is 0. */
}
h3::before {
counter-increment : section -1 ; /* Decrement the value of section counter by 1 */
content : "Section " counter ( section ) ": " ; /* Display the word 'Section ', the value of section counter, and a colon before the content of each h3 */
}
HTML
< h3 > Introduction </ h3 >
< h3 > Body </ h3 >
< h3 > Conclusion </ h3 >
result
A more complex example
:root {
counter-reset : link ;
}
a[href] {
counter-increment : link ;
}
a[href]:empty::after {
content : "[" counter ( link ) "]" ;
}
HTML
< p > See < a href = " https://www.mozilla.org/ " > </ a > </ p >
< p > Do not forget to < a href = " contact-me.html " > leave a message </ a > ! </ p >
< p > See also < a href = " https://developer.mozilla.org/ " > </ a > </ p >
result
Example of counter nesting
CSS
ol {
counter-reset : section ; /* Creates a new instance of the section counter with each ol element */
list-style-type : none ;
}
li::before {
counter-increment : section ; /* Increments only this instance of the section counter */
content : counters ( section , "." ) " " ; /* Combines the values of all instances of the section counter, separated by a period */
}
HTML
< ol >
< li > item </ li > <!-- 1 -->
< li > item <!-- 2 -->
< ol >
< li > item </ li > <!-- 2.1 -->
< li > item </ li > <!-- 2.2 -->
< li > item <!-- 2.3 -->
< ol >
< li > item </ li > <!-- 2.3.1 -->
< li > item </ li > <!-- 2.3.2 -->
</ ol >
< ol >
< li > item </ li > <!-- 2.3.1 -->
< li > item </ li > <!-- 2.3.2 -->
< li > item </ li > <!-- 2.3.3 -->
</ ol >
</ li >
< li > item </ li > <!-- 2.4 -->
</ ol >
</ li >
< li > item </ li > <!-- 3 -->
< li > item </ li > <!-- 4 -->
</ ol >
< ol >
< li > item </ li > <!-- 1 -->
< li > item </ li > <!-- 2 -->
</ ol >
result
standard
|
---|
|