User Guide / Generated Content
Counters are the mechanism provided by CSS to perform numbering. They can be used to number list items, pages, footnotes, sections and any other document content that needs to be numbered.
Counter reset and increment
The counter-reset
property
applies to any element and initialises one or more counters to the specified
values, or to zero if no value is specified.
Unless the resetting of a counter creates a nested counter, the scope of the counter includes the current element and all of its following siblings.
The counter-increment
property
applies to any element and increments one or more counters by the specified
values, or by one if no value is specified.
The counter-increment
and counter-reset
properties are ignored
on elements whose display
property has the value none
.
CSS
div.example1 { counter-reset: h3 h4 } div.example1 h3 { counter-increment: h3 } div.example1 h4 { counter-increment: h4 } div.example1 h3::before { content: counter(h3) } div.example1 h4::before { content: counter(h4, lower-alpha) }
This creates two counters inside the scope of
a div
element. All h3
and h4
heading elements will be
numbered starting from 1 and the counter number will be placed before the
heading text.
CSS
div.example2 { counter-reset: h3 } div.example2 h3 { counter-increment: h3; counter-reset: h4 } div.example2 h4 { counter-increment: h4 } div.example2 h3::before { content: counter(h3) } div.example2 h4::before { counter(h4, lower-alpha) }
In this example the h4
counter will be reset at each h3
element,
to produce sub-section numbering that restarts at each new section.
Nested counters
If a counter is reset on an element and the same counter has also been reset on an ancestor of that element, a nested counter will be created. The scope of the nested counter is the current element only and its final value will not be carried onto its following siblings.
For example, a nested XHTML list with a ul
element inside a li
inside another ul
creates a nested list-item
counter.
CSS
ol { counter-reset: list-item } li { counter-increment: list-item } li::marker { content: counters(list-item, ".", decimal); color: brown; font-weight: bold }
Counter styles
Counter values are displayed as decimal numbers by default, but they may be displayed using other styles such as roman numerals or consecutive letters of the alphabet.
CSS
chapter { counter-increment: chapter-num } chapter::before { content: "Chapter " counter(chapter-num, upper-roman) }
This rule will generate text such as "Chapter IV" before each chapter, with the appropriate chapter number displayed in uppercase roman numerals.
The following table shows examples of the various counter styles:
decimal | 1, 2, 3, … 9, 10, 11, … |
decimal-leading-zero | 01, 02, 03, … 09, 10, 11, … |
lower-roman | i, ii, iii, iv, v, vi, … |
upper-roman | I, II, III, IV, V, VI, … |
lower-alpha lower-latin |
a, b, c, … z, aa, ab, … |
upper-alpha upper-latin |
A, B, C, … Z, AA, AB, … |
asterisks | *, **, ***, ****, … |
lower-hexadecimal | 1, 2, 3, … 9, a, b, c, … |
upper-hexadecimal | 1, 2, 3, … 9, A, B, C, … |
octal | 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, … |
binary | 1, 10, 11, 100, 101, 110, 111, … |
arabic-indic | ١, ٢, ٣, ٤, ٥, ٦, … |
persian / urdu | ۱, ۲, ۳, ۴, ۵, ۶, … |
cjk-decimal | 一, 二, 三, 四, 五, 六, … |
japanese-informal | 一, 二, 三, 四, 五, 六, … |
simp-chinese-informal | 一, 二, 三, 四, 五, 六, … |
trad-chinese-informal | 一, 二, 三, 四, 五, 六, … |
japanese-formal | 壱, 弐, 参, 四, 五, 六, … |
simp-chinese-formal | 壹, 贰, 叁, 肆, 伍, 陆, … |
trad-chinese-formal | 壹, 貳, 參, 肆, 伍, 陸, … |
lower-norwegian | a, b, c, … z, æ, ø, å, aa, ab, … |
upper-norwegian | A, B, C, … Z, Æ, Ø, Å, AA, AB, … |
lower-russian | а, б, в, г, д, е, ж, … |
upper-russian | А, Б, В, Г, Д, Е, Ж, … |
lower-ukranian | а, б, в, г, д, е, є, … |
upper-ukranian | А, Б, В, Г, Д, Е, Є, … |
lower-belarusian | а, б, в, г, д, е, ж, … |
upper-belarusian | А, Б, В, Г, Д, Е, Ж, … |
lower-bulgarian | а, б, в, г, д, е, ж, … |
upper-bulgarian | А, Б, В, Г, Д, Е, Ж, … |
lower-serbian | а, б, в, г, д, ђ, е, … |
upper-serbian | А, Б, В, Г, Д, Ђ, Е, … |
repeat(x, y, z) | x, y, z, xx, yy, zz, xxx, yyy, … |
symbols(x, y, z) | x, y, z, 4, 5, 6, … |