CSS counter-reset property
The counter-reset
property in CSS is used to initialize or reset the value of a CSS counter to a specific value. CSS counters are used to create automatic numbering for elements, such as list items in ordered lists or custom counters in other HTML elements.
Syntax
selector {
counter-reset: counter-name [initial-value];
}
selector
: The element to which you want to apply the counter reset.counter-name
: The name of the counter you want to reset.initial-value
: (Optional) The value to which the counter should be reset. If omitted, the counter is reset to0
.
Values
counter-name
:- Description: Specifies the name of the counter to be reset. This name must match the name used in
counter-increment
or other counter-related properties.
- Description: Specifies the name of the counter to be reset. This name must match the name used in
initial-value
(Optional):- Description: Specifies the value to which the counter should be reset. If not specified, the counter is reset to
0
.
- Description: Specifies the value to which the counter should be reset. If not specified, the counter is reset to
Usage Example
Counters are typically used with ordered lists, but they can also be applied to other elements. Here’s an example of how to use counter-reset
in combination with other counter properties:
Example
/* Resetting a counter named 'section' */
body {
counter-reset: section; /* Initializes 'section' counter to 0 */
}
h2::before {
counter-increment: section; /* Increment 'section' counter for each h2 */
content: "Section " counter(section) ": "; /* Display the counter value */
}
ul {
counter-reset: list; /* Initializes 'list' counter for ul */
}
li {
counter-increment: list; /* Increment 'list' counter for each li */
}
li::before {
content: counter(list) ". "; /* Display the counter value for each li */
}
HTML Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Counter Reset Example</title>
<style>
/* Include the CSS from the example above here */
</style>
</head>
<body>
<h2>Introduction</h2>
<h2>Overview</h2>
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ul>
</body>
</html>
Explanation:
counter-reset: section;
:- Initializes a counter named
section
to0
for thebody
element. This counter will be used in the::before
pseudo-element ofh2
tags.
- Initializes a counter named
counter-increment: section;
:- Increments the
section
counter by1
for eachh2
element.
- Increments the
content: "Section " counter(section) ": ";
:- Uses the
counter(section)
function to display the current value of thesection
counter before eachh2
element.
- Uses the
counter-reset: list;
:- Initializes a counter named
list
to0
for theul
element.
- Initializes a counter named
counter-increment: list;
:- Increments the
list
counter by1
for eachli
element.
- Increments the
content: counter(list) ". ";
:- Uses the
counter(list)
function to display the current value of thelist
counter before eachli
element.
- Uses the
Important Points
Counter Initialization:
counter-reset
initializes the counter to a specified value (or0
by default). This is especially useful when you need to reset counters at different sections or elements within your document.
Counter Names:
- The name of the counter in
counter-reset
must match the counter name used incounter-increment
and other related properties to ensure consistency.
- The name of the counter in
Inheritance:
- The
counter-reset
property does not inherit its value. It applies only to the element it is directly set on and does not affect child elements unless explicitly set.
- The
Browser Compatibility:
- CSS counters, including properties like
counter-reset
, are well-supported across modern browsers, but it's always a good practice to test your design in different browsers to ensure consistent behavior.
- CSS counters, including properties like