Often I’ve found myself in a spot where I’ve needed to style an ordered list in a way that I just couldn’t do with CSS. So I either had to live with it as it was or get tricky with JavaScript or something to create a custom list with the necessary markup. Then I stumbled across CSS Counters and that all changed.

CSS Counters are very similar to variables in other languages. You have to set the counter to zero for the element containing your list items and then increment it over the set which can be any tag. They don’t have to be used in an HTML list. Then using CSS generated content you can loop out whatever content you like. You are still bound to a single element because you’ll have to use a pseudo element but it helps to give you more control to style things consistently across browsers.

Here’s a Basic Example

This is the markup that I’ll be using:

First, Set Counter to Zero

First I have to set a counter variable to zero on the container element by doing something like this:

Next, Increment the Counter and Set Generated Content

Then I have to increment the count and set up the generated content for the items. In this case I’m setting it to the count plus a period and a paren:

The Demo

This is what the result looks like:

This is an Item Here
This is Another Item Here
This is Yet Another Item Here
This is Even Another Item Here

Making it Look Better

So, now that we have the incremented numbering in place, we can make it look how ever we want.

Squares

This is an Item Here
This is Another Item Here
This is Yet Another Item Here
This is Even Another Item Here

Circles

This is an Item Here
This is Another Item Here
This is Yet Another Item Here
This is Even Another Item Here

These are just a couple of examples but really you are only bound by your imagination on how you can style these things.

Use Nested Counters to Make a Table of Contents List

CSS Counters can be nested within each other. We can use this functionality do some pretty cool and complex stuff like create a unique table of contents.

The CSS

The HTML

The Demo

  1. This is the 1st Part
    1. This is a section
    2. This is a section
    3. This is a section
  2. This is the 2nd Part
    1. This is a section
    2. This is a section
    3. This is a section
  3. This is the 3rd Part
    1. This is a section
    2. This is a section
    3. This is a section

Browser Support:

ie8 and aboveChrome 31 and aboveFire Fox 26 and aboveOpera 19 and aboveSafari 5.1 and above

CSS Counters have good browser support working in all modern browsers. Not supported in Internet Explorer 7 and below.


Sources

Comments