CSS property


The counter-increment property in CSS is used to increment the value of a CSS counter. CSS counters are useful for automatic numbering of elements, such as list items or section headings. This property allows you to control the value of counters, which can be displayed using the content property.

Syntax

selector { counter-increment: counter-name [value]; }
  • selector: The element to which you want to apply the counter increment.
  • counter-name: The name of the counter to be incremented.
  • value (Optional): The amount by which the counter should be incremented. If omitted, the counter is incremented by 1 by default.

Values

  1. counter-name:

    • Description: Specifies the name of the counter to be incremented. This name must match the counter name used in counter-reset or other counter-related properties.
  2. value (Optional):

    • Description: Specifies the value by which the counter should be incremented. This can be a positive integer or a negative integer. If omitted, the counter is incremented by 1 by default.

Usage Example

Counters are often used with list items or sections to provide automatic numbering. Here’s an example of how to use counter-increment:

Example

/* Reset and initialize counters */ body { counter-reset: section; /* Initializes 'section' counter to 0 */ } /* Increment counter for each h2 element */ h2 { counter-increment: section; /* Increments 'section' counter by 1 */ } /* Display the counter value before each h2 element */ h2::before { content: "Section " counter(section) ": "; }

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 Increment Example</title> <style> /* Include the CSS from the example above here */ </style> </head> <body> <h2>Introduction</h2> <h2>Overview</h2> <h2>Conclusion</h2> </body> </html>

Explanation:

  1. counter-reset: section;:

    • Initializes a counter named section to 0 on the body element.
  2. counter-increment: section;:

    • Increments the section counter by 1 for each h2 element.
  3. content: "Section " counter(section) ": ";:

    • Uses the counter(section) function to display the current value of the section counter before each h2 element.

Important Points

  1. Counter Initialization:

    • The counter-increment property works in conjunction with counter-reset. You need to use counter-reset to initialize the counter before you can increment it.
  2. Counter Names:

    • The counter name in counter-increment must match the name used in counter-reset to ensure proper incrementing.
  3. Increment Value:

    • You can specify a positive or negative integer for the value to adjust the increment amount. For example, counter-increment: section 2; would increase the counter by 2 for each instance.
  4. Inheritance:

    • The counter-increment property does not inherit from parent elements. It applies only to the element where it is specified.
  5. Browser Compatibility:

    • CSS counters, including counter-increment, are well-supported across modern browsers. However, testing across different browsers is recommended to ensure consistent behavior.