Ordered lists aren't the only elements that can be automatically numbered. Thanks to the various counter-related properties, any element can be.

body {
  counter-reset: my-awesome-counter;
section {
  counter-increment: my-awesome-counter;
section:before {
  content: counter(my-awesome-counter);

Each <section> will respectively start with "1", "2", "3", or "4".

You can control the style of the counter by comma separating the counter function. e.g. to make them use Roman numerals:

section:before {
  content: counter(my-awesome-counter, upper-roman);


On CodePen:

More Information

Browser Support

Chrome Safari Firefox Opera IE Android iOS
2+ 3.1+ Any 9.2+ 8+ TBD TBD


  1. Chris Bier

    The guy drove home without paying for the skirt steak! haha

  2. arnold

    hmm it looks like Android and iOS has a good support too.

  3. mahantesh
    Permalink to comment#

    Event without script we can change number of li with counter-increament

    • transGLUKator
      Permalink to comment#

      I have edited your code a bit so it is actually working.

      You have to move counter-increment property from li:before to li itself. Also in counter-reset property you cannot use commas, just spaces to separate values.

  4. Will
    Permalink to comment#

    Is it possible to use it like this:

    div:nth-child(counter(item)) {

    I can’t get it working, would be nice if it would work that way also.

    • James

      I’m almost certain that ‘counter’ only works within the ‘content’ property on pseudo elements (e.g. ::before, ::after)

  5. Shaun
    Permalink to comment#

    is there any technique or advice on how to make the counters clickable links? I can’t see how but I’ve been asked to do so on an already established design.

    • James

      Couldn’t you just wrap it up inside an anchor tag?

      <a class="counter-link"><a/>

      Then in your css, try something like..

      .counter-link {
          &::before {
              content: counter(NAME_OF_COUNTER);
  6. James

    I’ve run into an issue where I have code like so:

    #map { counter-reset: markerLabel; }
    #map .MarkerLabel {
        counter-increment: markerLabel;
        &::before {  content: ~"counter(markerLabel)";  }
        &:hover {  /* stuff */ }

    This works no problem on page load. The problem I’m running into is whenever I hover over .MarkerLabel, it seems to be inserting an additional ::before pseudo element inside the .MarkerLabel. So I end up with the original ::before pseudo element that has a counter value of 1 and then when I hover, a second pseudo element appears with a counter value (in this case) of 54, like it’s doing it all over again on hover. Make no sense to me. Anyone else run into issues with :hover and the CSS counter?

    • James

      It was an issue with the Google Maps API rendering two sets of markers. So I just limited the counter to the first set of .MarkerLabels.

  7. Rahul
    Permalink to comment#

    Here is youtube video tutorial, i explained this in little detail with examples. :)

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.