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!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.adesignlink.com Chad P

    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.

  • http://www.craigmoussa.com Craig Moussa

    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.

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Thanks, Craig.

      This is CSS2 though. :)

      • http://www.thegreatbritishbakeoff.com Baker

        Even better! :D

  • Burak Erdem

    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).

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Yeah – 3n+1 will do the trick.

  • http:/rainrain.ie byronyasgur

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

  • Brad VanHorn

    I had no idea you could do this! Great tip

  • Shehroz Khan

    Nice

  • http://creatiface.com Thoriq

    I never thought CSS2 has such feature. Wow.

  • http://www.freshclickmedia.com/blog Shane

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

    Thanks for sharing.

  • Wouter J

    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!

  • http://www.riak.uk.com Web Design Crawley

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

  • http://vivekpedia.tumblr.com Vivek Shrinivasan

    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?

    • http://www.youtube.com/user/izvarzone AntoxaGray

      Nope.

      • Johan

        Yes.
        From ie8 and up. not nth-child though. That’s from ie9 and up, unless you use “selectivizr” (small jquery plugin, that makes ie6+ understand css3 selecters).

  • http://blizzard.gr Sotiris.k

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

  • http://www.bigswebdesign.co.uk/ Stewart

    Nice

  • http://www.mediamost.hu KarDave

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

  • http://eways.ph Eways

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

  • http://www.codehunterbd.wordpress.com Khairul Alam

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

  • http://nmecdesign.com/ Jonathan Warren

    Which browsers support this?

  • http://foundation.cegonsoft.org/ Cegonsoft

    Very informative thanks a lot for the nice tutorial…

  • http://www.wimbledon-it.co.uk Bjoern

    Thats a really nice one! Thanks for sharing.

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

  • http://blog.lastrose.com LastRose

    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

      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

    • http://www.lastrose.com LastRose

      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

  • http://elneco.biz jaime

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

    http://tinkerbin.com/umoZm7bs

  • http://www.google.com pushpendra

    give basic knowlage code in php….

  • komiska

    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?

    • http://blog.dalibor-sojic.info Dalibor Sojic

      komiska, at html editor, instead “format: Plain”, select “format: HAML”

  • http://blog.dalibor-sojic.info Dalibor Sojic

    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.

  • http://www.telordesign.com Vincent

    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.

  • jam

    very nice! :)

  • http://iryska.info/ Ira

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

  • http://bluecrow.org Mateusz Charytoniuk

    Step

    Step

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

    Produces something similar to:

    Step 1

    Step 2

  • Vibha

    Demo link didn’t work for me…

  • arnold

    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 ?

  • AbdulAziz AlGhamdi

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

  • http://www.fractalfragger.com Mark Baker

    this is really handy to know

    Thanks

  • BK

    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!

    • BK

      Found a way to fix it! Use li:nth-child(4n+1) instead of (4n) — or (4n+x) if you use a stepping of x… ;-)

  • Nicks

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

    • http://customwordpresstheme.co.uk/ WordPress Designer

      try to find some tutorials maybe on youtube..

  • http://www.tipstrikkomputer.com Kade

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

  • cdyak

    transitions won’t work on these properties in webkit. And your demo link is broken.