1,051

CSS counters

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

Counters are a powerful feature of generated content. Through counters we can add an automatic numbering to the elements of a web document. Thanks to the :before and :after pseudo-elements, the numbering will appear before or after the actual content of an element, respectively. In this article I’ll explain how to use counters by providing some useful examples.

Table of contents

Adding counters to elements

The automatic numbering of CSS is controlled by two properties, counter-reset and counter-increment. Counters defined by these properties are then used with the counter() and counters() functions of the content property.

The counter-reset property can contain one or more names of counters (identifiers), optionally followed by an integer. The integer sets the value that will be incremented by the counter-increment property for any occurence of the given element. The default value is 0. Negative values are allowed.

The counter-increment property is similar to the previous property. The basic difference here is that this property actually increments a counter. Its default increment is 1. Negative values are allowed.

Now we are ready to create a practical example. Given the following markup:

Listing 1. A basic definition list

  1. <dl>
  2. <dt>term</dt>
  3. <dd>definition</dd>
  4. <dt>term</dt>
  5. <dd>definition</dd>
  6. <dt>term</dt>
  7. <dd>definition</dd>
  8. </dl>

View the example

we want to add a progressive numbering (1, 2, 3) to each definition term (dt) in the list. The relevant CSS is the following:

Listing 2. Adding counters to elements

  1. dl {
  2. counter-reset: term;
  3. }
  4. dt:before {
  5. counter-increment: term;
  6. content: counter(term);
  7. }

View the exampleView the screenshot

The first rule in the previous listing sets a counter for the definition list. This is called a scope. The name (an identifier) of the counter is term. Keep in mind that once we’ve chosen a name for our counter this must be the same also in the counter-increment property (of course se should use a meaningful name).

In the second rule we attach the :before pseudo-element to the dt element, since we want to insert the counter exactly before the actual content of the element. Now let’s take a closer look at the second declaration of the second rule. The counter() function accepts our identifier (term) as its argument and the content property actually generates the counter.

As you can see, there’s no space between the number and the content of the element. If we want to add more space and, say, a period (.) after the number, we could insert the following string in the content property:

Listing 3. Adding a period and a space to counters

  1. dt:before {
  2. content: counter(term) ". ";
  3. }

View the exampleView the screenshot

Note that the string inside the double quotes is treated literally, that is, the space after the period is inserted as we’ve typed it on the keyboard. In fact, the content property can be regarded as the CSS counterpart of the JavaScript document.write() method except that this property doesn’t add real content to the document. Simply put, the content property creates a mere abstraction on the document tree but it doesn’t modify it.

In case you’re wondering, we can add more styles to counters by applying other properties to the attached pseudo-element. For example:

Listing 4. Adding more styles to counters

  1. dt:before {
  2. content: counter(term);
  3. padding: 1px 2px;
  4. margin-right: 0.2em;
  5. background: #ffc;
  6. color: #000;
  7. border: 1px solid #999;
  8. font-weight: bold;
  9. }

View the exampleView the screenshot

We’ve just set a background color, added some padding and a right margin, made the font bold and outlined the counters with a thin solid border. Now our counters are a little more attractive.

Furthermore, counters can be negative. When dealing with negative counters, we should only keep in mind a little maths, namely the part concerning addition and subtraction of negative and positive numbers. For example, if we need a progressive numbering starting from 0, we could write the following:

Listing 5. A progressive numbering starting from 0

  1. dl {
  2. counter-reset: term -1;
  3. }
  4. dt:before {
  5. counter-increment: term;
  6. content: counter(term) ". ";
  7. }

View the exampleView the screenshot

By setting the counter-reset property to -1 and incrementing it by 1, the resulting value is 0 and the numbering will actually start from that value. Negative counters can combine with positive counters to create interesting results. Consider the following example:

Listing 6. Combination of negative and positive counters

  1. dl {
  2. counter-reset: term -1;
  3. }
  4. dt:before {
  5. counter-increment: term 3;
  6. content: counter(term) ". ";
  7. }

View the exampleView the screenshot

As you can see, addition and substraction of negative and positive numbers yield a wide range of combination between counters. With just a simple set of calculations we can get a complete control over this automatic numbering.

Nested counters

Another interesting feature of CSS counters lies in their capability of being nested. In fact, numbering may proceed also by using progressive sublevels, such as 1.1, 1.1.1, 2.1 and so on. For example, if we want to add a sublevel to the elements of our list, we could write the following:

Listing 7. Nested counters

  1. dl {
  2. counter-reset: term definition;
  3. }
  4. dt:before {
  5. counter-increment: term;
  6. content: counter(term) ". ";
  7. }
  8. dd:before {
  9. counter-increment: definition;
  10. content: counter(term) "." counter(definition) " ";
  11. }

View the exampleView the screenshot

This example is similar to the first one, but in this case we have two counters, term and definition. The scope of both counters is set by the first rule and “lives” in the dl element. The second rule inserts the first counter before each definition term of the list. This rule is not particularly interesting, since its effect is already known. Instead, the last rule is the core of our code because it:

  1. increments the second counter (definition) on dd elements
  2. inserts the first counter (term), followed by a period
  3. inserts the second counter (definition), followed by a space.

Note that steps #2 and #3 are both performed by the content property used on the :before pseudo-element attached to the definition term.

Another interesting thing to remember is that counters are “self-nesting” in the sense that resetting a counter on a descendant element (or pseudo-element) automatically creates a new instance of the counter. This is useful in the case of (X)HTML lists, where elements may be nested with arbitrary depth. However, it’s not always possible to specify a different counter for each list, since this approach might produce a really redundant code. In that vein, it’s useful to mention the counters() function. This function creates a string containing all the counters having the same name of the given counter in the scope. Counters are then separated by a string. For example, given the following markup:

Listing 8. Nested ordered lists

  1. <ol>
  2. <li>item</li>
  3. <li>item
  4. <ol>
  5. <li>item</li>
  6. <li>item</li>
  7. <li>item
  8. <ol>
  9. <li>item</li>
  10. <li>item</li>
  11. </ol>
  12. </li>
  13. </ol>
  14. </li>
  15. </ol>

View the example

The following CSS numbers the nested list items as 1, 1.1, 1.1.1, etc.

Listing 9. The counters() function

  1. ol {
  2. counter-reset: item;
  3. }
  4. li {
  5. display: block;
  6. }
  7. li:before {
  8. counter-increment: item;
  9. content: counters(item, ".") " ";
  10. }

View the exampleView the screenshot

In this example we have only the item counter for each nesting level. Instead of writing three different counters (e.g. item1, item2, item3) and thus creating three different scopes for each nested ol element, we can rely on the counters() function to achieve this goal. The second rule is really important and deserves a further explanation. Since ordered lists have a default marker (a number), we get rid of these marker by turning the list items into block-level elements. Keep in mind that only elements with display: list-items have markers. Now we can look carefully at the third rule that actually does the work. The first declaration increments the counter previously set on the outermost list. Then, in the second declaration, the counters() function creates all the counter’s instances for the innermost lists. The structure of this function is as follows:

  1. its first argument is the name of the given counter, immediately followed by a comma
  2. its second argument is a period inserted in double quotes.

Note that we’ve inserted a space after the counters() function in order to keep the numbers separate from the actual content of the list items.

Styles of counters

Counters are formatted with decimal numbers by default. However, the styles of the list-style-type property are also available for counters. The default notation is counter(name) (no style) or counter(name, 'list-style-type') if we want to change the default formatting. In practice, the recommended styles are:

  • decimal
  • decimal-leading-zero
  • lower-roman
  • upper-roman
  • lower-greek
  • lower-latin
  • upper-latin
  • lower-alpha
  • upper-alpha

because we should always bear in mind that we’re working with numeric systems. Furthermore, we should also be aware of the fact that the specifications don’t define the rendering of alphabetical systems at the end of the alphabet. For example, after 26 list items the rendering of lower-latin is undefined. Real numbers are thus recommended for long lists. Here’s an example:

Listing 10. Styles of counters

  1. dl {
  2. counter-reset: term definition;
  3. }
  4. dt:before {
  5. counter-increment: term;
  6. content: counter(term, upper-latin) ". ";
  7. }
  8. dd:before {
  9. counter-increment: definition;
  10. content: counter(definition, lower-latin) ". ";
  11. }

View the exampleView the screenshot

We can also add styles to the counters() function, as shown in the following example.

Listing 11. The counters() function with styles

  1. li:before {
  2. counter-increment: item;
  3. content: counters(item, ".", lower-roman) " ";
  4. }

View the exampleView the screenshot

Note that the counters() function can also accept a third argument (lower-roman) as the last member of its arguments list, separated by a second comma from the preceding period. However, the counters() function doesn’t allow us to specify different styles for each level of nesting.

Download examples

examples.zip

prajapat