Lists, counters and generated content
Definition and purpose
List items have a marker box that can display a number, bullet or other marker. Preserve ordered and unordered list semantics in HTML; CSS list-style-type changes the presentation of that marker, and ::marker allows a restricted set of styles. A CSS counter maintains an integer in a scope and can format that integer in generated content.
Style native ordered markers
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { list-style-type: lower-roman; } #a li::marker { color: navy; } </style> </head> <body> <ol id="a"> <li id="x">Wash the vegetables</li> <li id="y">Slice them</li> </ol> <section id="s"> <h2 id="h">Preparation</h2> <h2>Cooking</h2> </section> </body></html>Native numbering requests Roman markers; the marker color is navy.
Syntax and mental model
counter-reset establishes a counter, counter-increment advances it, and counter(name) inserts its formatted value into generated content. For example reset part to zero on the section, increment on each h2 and use ::before with counter(part). The actual heading remains present. The native list-item counter is tied to list numbering, but custom marker numbering with HTML start/value can have engine discrepancies; keep native numbering for essential sequence information. Our custom heading counter is separate from list-item, and its CSSOM content check does not prove the painted number.
Number headings decoratively
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #s { counter-reset: part; } #s h2 { counter-increment: part; } #s h2::before { content: counter(part) ". "; } </style> </head> <body> <ol id="a"> <li id="x">Wash the vegetables</li> <li id="y">Slice them</li> </ol> <section id="s"> <h2 id="h">Preparation</h2> <h2>Cooking</h2> </section> </body></html>The counter starts at zero and is incremented per heading; the content expression is retained in CSSOM.
Common mistakes
Do not replace meaningful words or essential numbering with generated content alone. ::marker is not a general-purpose box with arbitrary layout properties; use supported marker styling and test with assistive technology.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Paged media and print. Next lesson: Ruby, SVG and MathML styling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Lists, counters and generated content — knowledge check
Five questions on lists, counters and generated content.
What is a list marker?
What establishes a named counter?
How is the counter value requested in generated content?
Why retain native ordered list markup?
Does computed content resolve a counter to a painted number?