everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

Mastering CSS Counters: Auto-Increasing Numbers Without Using a List

Mastering CSS Counters: How to Auto-Increasing Numbers Without Using a List

With CSS counters, you can display dynamic, incremental values for anything you want, without being limited to using HTML lists (<ul> or <ol>). This means that you can create dynamic and customized content that is both visually appealing and functional.

Unlike traditional lists, which can only be added at the beginning of content, CSS counters give you the ability to insert counters anywhere within your content.

The following properties will be used when working with CSS counters:

CSS PropertyDescription
counter-resetresets a counter to an initial value, initial value = 0
counter-incrementincrease a counter by a specified value
counter()returns the value of the specific counter
counter-setsets the specific counter to a given value.

This tutorial will guide you step-by-step through the process of creating a CSS counter with examples. You’ll also learn how to create a CSS dynamic counter and gain a solid understanding of how it works in just 3 steps. Let’s get started!

Step 1: Create or reset a Counter

To use a CSS counter, you first need to define it. You can define a counter using the “counter-reset” property in your CSS code.

For example, let’s create a counter and name it “uipencil-counter“:

<!DOCTYPE html>
<html>
<head>
<style>
  .counter-wrap{
      counter-reset: uipencil-counter;
  }
</style>
</head>
<body>
  <div class="counter-wrap">
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
  </div>
</body>
</html>

This will create a counter called “uipencil-counter“.

Step 2: Increment the Counter

Once you’ve created a counter, you can increment it using the “counter-increment” property, for example:

<!DOCTYPE html>
<html>
<head>
<style>
  .counter-wrap{
      counter-reset: uipencil-counter;
  }
  .item {
      counter-increment: uipencil-counter;
  }
</style>
</head>
<body>
  <div class="counter-wrap">
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
  </div>
</body>
</html>

In this example, the value of the “uipencil-counter” counter will increase by 1 each time a list item is generated.

Note 1:
Initially, if we leave the increament value unset, it will default to 1. To specify a custom increment value, let’s say we want to increase the count by 3 for each item, we can set the CSS property to “counter-increment: uipencil-counter 3;
This will result in a list where each item is numbered by 3: 3, 6, 9, 12, and so on. You can adjust the increment value to any number you want, depending on your needs.
Note 2:
To generate a decremental list, we can set the increment count to a negative value, for example, “counter-increment: uipencil-counter -1;

Step 3: Display the Counter

Finally, the counter is all ready and you can display the counter using the “content” property with the “counter()” function.

For example, if you want to insert the counter value in the middle of the content for each list item:

<!DOCTYPE html>
<html>
<head>
<style>
  .counter-wrap{
      counter-reset: uipencil-counter;
  }
  .item {
      counter-increment: uipencil-counter;
  }
  .item span{
      position: relative;
      padding-left: 30px;
  }
  .item span::before {
      content: "00" counter(uipencil-counter);
      position: absolute;
      left: 0;
  }
</style>
</head>
<body>
  <div class="counter-wrap">
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
  </div>
</body>
</html>

In this example, we customize the counter value in a zero-leading format

Result Output
Item – this is the way
Item – this is the way
Item – this is the way
Item – this is the way
Item – this is the way

Others: counter-set

The counter-set property allows you to set the value of a counter to a specific number. This is useful when you want to start counting from a number other than 1 or when you need to reset a counter to a certain value. here’s the example:

<!DOCTYPE html>
<html>
<head>
<style>
  .counter-wrap{
      counter-reset: uipencil-counter;
  }
  .item {
    counter-increment: uipencil-counter;
  }
  .item span{
    position: relative;
    padding-left: 30px;
  }
  .item:nth-of-type(2) span::before {
    counter-set: uipencil-counter 5;
    content: "00" counter(uipencil-counter);
    color: red;
  }
  .item span::before {
    content: "00" counter(uipencil-counter);
    position: absolute;
    left: 0;
  }
</style>
</head>
<body>
  <div class="counter-wrap">
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
  </div>

</body>
</html>

In this example, we have applied “counter-set: uipencil-counter 5;” to the second item (refer to the highlighted row). This means that the counter has been set to 5 from there and will start counting from “5” for subsequent items.”

Result output
Item – this is the way
Item – this is the way
Item – this is the way
Item – this is the way
Item – this is the way

Note: (by March 2023) counter-set is having limited support in web and mobile Safari

Others: Nested Counter

A counter that is contained within another counter is called a nested counter. These counters are utilized to generate multiple levels of counting.

<!DOCTYPE html>
<html>
<head>
<style>
  .counter-wrap{
    counter-reset: uipencil-counter;
  }
  .item {
    counter-increment: uipencil-counter;
    counter-reset: uipencil-counter-sub;
  }
  .item_sub {
    counter-increment: uipencil-counter-sub;
    padding-left: 10px;
  }
  .item span,
  .item_sub span{
    position: relative;
    padding-left: 30px;
  }
  .item span::before {
    content: "00" counter(uipencil-counter);
    position: absolute;
    left: 0;
  }
  .item_sub span::before {
    content: "00" counter(uipencil-counter) "." counter(uipencil-counter-sub);
    position: absolute;
    left: 0;
  }
</style>
</head>
<body>
  <div class="counter-wrap">
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item_sub">sub item - <span></span></div>
    <div class="item_sub">sub item - <span></span></div>
    <div class="item_sub">sub item - <span></span></div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item_sub">sub item - <span></span></div>
    <div class="item_sub">sub item - <span></span></div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item">Item <span></span> - this is the way</div>
    <div class="item_sub">sub item - <span></span></div>
    <div class="item">Item <span></span> - this is the way</div>
  </div>

</body>
</html>
Result Output
Item – this is the way
sub item –
sub item –
sub item –
Item – this is the way
sub item –
sub item –
Item – this is the way
Item – this is the way
sub item –
Item – this is the way

Browser Compatibility

  • counter-reset, counter-increment and counter() are supported in most of the modern browsers
  • counter-set have limited support in Safari
ChromeEdge FirefoxSafari Opera Chrome Android WebView AndroidiOS Safari Firefox AndroidOpera Android Samsung Internet
counter-reset212139.2184.412510.11.0
counter-increment212139.2184.412510.11.0
counter()112139.2184.41410.11.0
counter-set858568718585686014.0

sources:

https://developer.mozilla.org/en-US/docs/Web/CSS/counter-reset#browser_compatibility
https://developer.mozilla.org/en-US/docs/Web/CSS/counter-increment#browser_compatibility
https://developer.mozilla.org/en-US/docs/Web/CSS/counter#browser_compatibility
https://developer.mozilla.org/en-US/docs/Web/CSS/counter-set#browser_compatibility

Summary

In conclusion, CSS counters offer a powerful way to create customized and dynamic content on your webpage. It also allow you to have more control and flexibility in styling your counter with CSS. By using CSS counters in your projects, you can add unique and personalized touches to your webpage that will captivate your audience. Happy coding!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

$5.00
$10.00
$15.00

Or enter a custom amount

$

Thank you for your contribution!

Donate

Topics:

CSS (31) css pseudo (3) debug (2) design (7) fonts (2) html (6) image (9) JavaScript (1) layout (5) MiniGame (3) optimization (10) performance (9) responsive (8)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements
%d bloggers like this: