--- id: css-counters title: "CSS Counters" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["CSS counters", "counter-reset", "counter-increment", "automatic numbering", "counter() function"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["css", "web", "frontend", "w3schools", "counters", "generated-content"] raw_sources: ["https://www.w3schools.com/css/css_counters.asp"] applied_in: [] github_commit: "" --- # [[CSS Counters]] ## π― ν μ€ ν΅μ°° (One-line insight) CSS counters are "variables" maintained by CSS whose values can be incremented (or decremented) by CSS rules, enabling automatic numbering of elements like section headings β no JavaScript required. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **What counters are** β CSS counters are "variables" maintained by CSS, and their values can be incremented (or decremented) by CSS rules. [S1] - **`counter-reset`** β creates or resets a counter. [S1] - **`counter-increment`** β increments (or decrements) a counter value. [S1] - **`content`** β inserts generated content (used with `::before`/`::after`). [S1] - **`counter()`** β adds/returns the value of a named counter to an element. [S1] - **Increment direction & amount** β `counter-increment` takes a second parameter (default 1); a negative value decrements, and any value can be used to step by more than 1. [S1] ## π§© μΆμΆλ ν¨ν΄ (Extracted patterns) - **Reset on container, increment on item** β set `counter-reset` on a container (e.g., `body`) and `counter-increment` on the repeated element's `::before`. [S1] - **Compose generated labels** β combine string literals with `counter(name)` inside `content` to produce labels like `"Section 1: "`. [S1] - **Tune the step** β change numbering direction/granularity purely by the second argument of `counter-increment` (e.g., `-1`, `2`). [S1] ## π μΈλΆ λ΄μ© (Details) **CSS Automatic Numbering With Counters** CSS counters are "variables" maintained by CSS, and their values can be incremented (or decremented) by CSS rules. The properties/functions used are: `counter-reset` (creates or resets a counter), `counter-increment` (increments/decrements a counter value), `content` (inserts generated content), and `counter()` (adds the value of a counter to an element). [S1] **CSS Increase and Decrease Counter** The following example creates a counter for the page (in the `body` selector), then increments the counter value by 1 for each `