Quick Tip: How to Keep the Count with CSS
basixvideos

Quick Tip: How to Keep the Count with CSS

Tutorial Details
  • Topic: CSS
  • Difficulty: Basix

Did you know that, with CSS, you can create a counter? This can be especially useful for instances where the count is purely needed for presentational purposes. I’ll show you how to use counter-increment in this useful quick tip.


Screencast

Choose 720p for the best picture.

How Does it Work?

It’s really quite simple. We’re not even talking CSS3 here; the counter-increment property has been around for a while now. It’s just that many of us aren’t familiar with it, or haven’t found a use for it.

We begin by applying the counter-increment property. Let’s imagine that we have a set of boxes, and each box should display a number that corresponds to the order that the box occurs it its sequence.

.box {
  counter-increment: boxes;
}

The counter-increment property can accept either one or two properties. The first is an id that you will later use to reference this specific counter. You may also pass a second parameter that refers to the increment. For example, instead of 1, 2, 3, 4, you could switch to 5, 10, 15, 20 by applying: counter-increment: boxes 5.

This code will now store a unique number for each element that has a class of box. But of course, we want to get this number on the page. Hopefully, we’ll, at some point in the future, be able to use the content property within standard selectors, but not quite yet. Instead, we’ll use pseudo elements to apply the content.

.box:after {
  content: counter(boxes);
}

This will apply a unique number – again, based on the element’s order in the sequence – to the .box element. Should we need to reset this order back to 1 at some point, we could usethe counter-reset: boxes property.


Why Do This, Again?

Now, you still might be thinking: “Why would I ever use this?” There’s lots of situations when it might be handy. For example, consider a comments section of a blog. If you wanted to provide
a number for each comment in the set – possibly slightly transparent – you could use this technique. The number isn’t vital to the markup, and is only used for presentation. In these cases, CSS
counters will do the trick nicely!

Add Comment

Discussion 44 Comments

  1. Chad P says:

    Wow.. First Comment. I have to say this is a first for me? Have Never seen this before. But now that I have. I can say that I can think of a few ways to use this.

  2. Craig Moussa says:

    Glad to see that CSS3 is becoming more vibrant with the new tips and tricks, cannot wait for more releases. By the way, Jeffery Way, I would like to personally thank you for your CodeIgniter tutorials. I now fluently work with CI constantly and I’m currently building a Social Network based around the environment.

  3. Burak Erdem says:

    Great tutorial but there seems to be a problem with li:nth-child(4n) code. It goes like that; 1-2-3-1-2-3-4-1. As you can see, first it counts to 3 and resets. And then it counts to 4 and resets. This is true for ever “Xn” counter.

    So instead of li:nth-child(4n), you better write li:nth-child(3n+1).

  4. byronyasgur says:

    Nice tip. Cant believe it’s CSS2 and I never heard of it before.

  5. Brad VanHorn says:

    I had no idea you could do this! Great tip

  6. Shehroz Khan says:

    Nice

  7. Thoriq says:

    I never thought CSS2 has such feature. Wow.

  8. Shane says:

    I started work for the day about 2 minutes ago… I’ve learned something already.

    Thanks for sharing.

  9. Wouter J says:

    I’ve never heard about counting in CSS. Nice tip!

    But, the nth-child is CSS3, so if you work with reset this don’t work well in old browsers.

    I’ve found an good example of use this on the w3.org site: CSS properties: Counter-increment
    If you work with chapters it will be very nice!

  10. This is new to me aswel! the comment idea is great and i might just steal it :-)

  11. Just a quick question. I haven’t tried this yet, nor should I bother to, but just for it’s sake.

    Does this also work on IE?

  12. Sotiris.k says:

    Thanks a lot Jeffrey. That was very helpful for me!

  13. KarDave says:

    Very useful for displaying line numbers, so you can easily copy a piece of code without the line numbers :)

  14. Eways says:

    another cool tutorial from jeffrey way :D thanks for sharing!

  15. Khairul Alam says:

    Very cool tutorial.Thanks a lot for sharing this tutorial.

  16. Which browsers support this?

  17. Cegonsoft says:

    Very informative thanks a lot for the nice tutorial…

  18. Bjoern says:

    Thats a really nice one! Thanks for sharing.

    This is my outcome http://tinkerbin.com/YTKXccRC : )

  19. LastRose says:

    Great trick, think I saw it once before and dismissed it, but great to see it in action. One question though, How does this compare to using an OL element?

    • Dave says:

      Last,
      I see what you mean. But there are many situations when you don’t want list items. Like if you’re displaying a bunch of photo boxes, or various other elements, a list would be a headache to style.

      Jeffrey, very interesting post! And I’ll second the request for knowing what browsers will support this!

      Thanks, Dave

    • LastRose says:

      despite questioning the use of it, I actually just used it. Needed to style an ordered list as inline block and lost my numbers. used this to get them back

  20. jaime says:

    I had no idea! Here’s my mess around with letters:

    http://tinkerbin.com/umoZm7bs

  21. pushpendra says:

    give basic knowlage code in php….

  22. komiska says:

    Thanks for this, Jeffrey and others !
    Always love your tuts!!!

    just a remark on something weird:
    when i click on your demos ( also the ones in the comments), i get this:
    http://tinypic.com/view.php?pic=2zhjrtl&s=5

    and have to re-edit the code manually , inserting tags, to get to the result .
    Am i doing something wrong, or did some Tinkerbin parser go wild?

  23. Recently, due project requirements, I had need of this technique. After that, I tried to explain it on my blog: Custom list order number position

    Very useful technique.

  24. Vincent says:

    Very good article. I will use it in the future for sure. I like the way you explained it, simple and also easy to understand.

  25. jam says:

    very nice! :)

  26. Ira says:

    Jeffrey, thank you, i’ve never heard about it – it’s great
    But unfortunatly it’s don’t work in ie7 :(

  27. Step

    Step

    fieldset {
    counter-increment: legends;
    }
    fieldset > legend:after
    content: counter(legends)
    }

    Produces something similar to:

    Step 1

    Step 2

  28. Vibha says:

    Demo link didn’t work for me…

  29. arnold says:

    Thanks JW,
    I think this is useful also in image galleries and I was shocked never knew this was CSS2,

    but I dont know why do you guys use instead of ?

  30. AbdulAziz AlGhamdi says:

    what if i need started from particular number and increment it one or more in each step?

  31. Mark Baker says:

    this is really handy to know

    Thanks

  32. BK says:

    Nice one! Using the counter reset, the first reset goes wrong though (leading to 1-2-3 / 1-2-3-4 / 1-2-3-4 /… with nth-child(4n) f.ex.). Happens in all browsers and with every stepping – check the output in the demo with some additional s…

    Thanks anyway, might come in handy!

  33. Nicks says:

    hey guys. i am learning CSS and it is quite dull to learn all alone. what should i do? :S

  34. Kade says:

    Wow, never tought that way..
    Nice tutorial for a newbie like me.

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.