Creating and Using CSS Counters: Generating Dynamic Numeric Values in Stylesheets
Learn how to use CSS counters to generate and increment numeric values within your stylesheets. This tutorial explains how to create, increment, and display counter values for tasks like automatically numbering sections or lists, adding dynamic functionality to your web designs.
Creating and Using CSS Counters
Understanding CSS Counters
CSS counters are a way to automatically generate and increment numeric values within your CSS. They're like variables that are managed by CSS, useful for creating ordered lists, numbering sections of a page, or other scenarios where you need to track the number of times an element appears. This allows for creating dynamic content that automatically updates as your HTML structure changes.
CSS Counter Properties
Several CSS properties work together to create and manage counters:
counter-reset
: Creates a counter or resets its value. You can specify the counter name and its initial value (if omitted, defaults to 0).counter-increment
: Increments the counter's value. You can specify the counter name and the increment value (if omitted, defaults to 1).content
: Inserts the counter value into the content of a pseudo-element (like `::before` or `::after`).counter()
orcounters()
function: Retrieves the value of a counter and inserts it into the content of an element.
Note: You must define a counter using `counter-reset` before using it with `counter-increment` and `content`.
Example: Basic Counter Implementation
This example shows a basic counter that numbers sections on a page. You'll need corresponding HTML elements (like headings) to see this. The `counter-reset` property initializes the counter, and `counter-increment` increases its value for each heading. The `content` property then displays the counter value.
Illustrative CSS
body {
counter-reset: section; /* Initialize counter named "section" */
}
h2::before {
counter-increment: section; /* Increment counter for each h2 */
content: "Section " counter(section) ": ";
}
This creates numbered sections using the counter "section". IE8 requires a `` declaration for counter support.
Nested Counters
You can create counters within counters, allowing you to generate nested numbering systems (like for lists within lists). This example uses nested counters.
Illustrative CSS
body {
counter-reset: section;
}
h2 {
counter-reset: subsection; /* Nested counter */
}
h2::before {
content: counter(section) ". ";
counter-increment: section;
}
h3::before {
content: counter(section) "." counter(subsection) ". ";
counter-increment: subsection;
}
This example creates section and subsection counters, numbering elements hierarchically. IE8 requires a `` for counter support.
Multi-level Nested Counters
Nested counters can create outlines or other hierarchical numbering schemes. You can add strings between counter levels for clarity. This example shows how to add a string between nested counter levels.
Illustrative CSS
ol {
counter-reset: item;
}
li::before {
counter-increment: item;
content: counter(item, decimal) ". ";
}
li ol {
counter-reset: item;
}
li ol li::before {
content: counter(item, lower-alpha) ") ";
}
This example creates a numbered list with lower-alpha nested sub-lists, illustrating multi-level numbering using nested counters. IE8 needs a `` declaration for support.