Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Topics

CSS counter-increment Property

Posted in CSS Tutorial
Updated on Oct 02, 2024
By Mari Selvan
👁️ 36 - Views
⏳ 4 mins
💬 1 Comment
CSS counter-increment Property

Photo Credit to CodeToFun

🙋 Introduction

The counter-increment property in CSS is used to increase the value of a CSS counter.

Counters are variables maintained by CSS whose values can be incremented or decremented by CSS rules.

They are useful for creating complex lists, numbering headings, and other content that requires incremental numbering.

💡 Syntax

The syntax for the counter-increment property allows you to specify the counter to increment and the amount by which to increment it.

Syntax
Copied
Copy To Clipboard
element {
  counter-increment: counter-name increment-value;
}

🎛️ Default Value

The default value for counter-increment is none, which means that no counter is incremented.

🏠 Property Values

ValueDescription
counter-nameThe name of the counter you want to increment. If the counter does not exist, it will be created.
increment-valueAn integer value that specifies how much the counter should be incremented by. The default value is 1.

📄 Example

In this example, we'll use the counter-increment property to create an ordered list with custom counters.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS counter-increment Example</title>
  <style>
    body {
      counter-reset: section;
    }
    h2::before {
      counter-increment: section;
      content: "Section " counter(section) ": ";
    }
  </style>
</head>
<body>
  <h2>Introduction</h2>
  <p>This is the introduction section.</p>
  
  <h2>Usage</h2>
  <p>This section explains how to use the counter-increment property.</p>
  
  <h2>Conclusion</h2>
  <p>This is the conclusion section.</p>
</body>
</html>

In this example, each h2 element is prefixed with "Section " followed by the current value of the section counter.

🖥️ Browser Compatibility

The counter-increment property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. Ensure to test your implementation across different browsers to maintain compatibility.

🎉 Conclusion

The counter-increment property is a powerful feature in CSS that allows for sophisticated control over content numbering and ordering.

By using this property in combination with counter-reset and the content property, you can create dynamic and automatically numbered elements in your web pages. Experiment with different counters and increment values to see how they can enhance your designs and user interfaces.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy