CodeTidbit by SamanthaMing.com

Use the counter property to turn any element into a numbered list. Similar to how the ordered list tag works <ol>. Very useful if you're creating a documentation site where you need to automatically number the headings or create a table of contents 👍

HTML

How the counter property work

There are 3 steps to get the counter to work:

  1. Define & Initialize Counter
  2. Increment Counter
  3. Display Counter

1. Define & Initialize Counter

There are 2 parts to this step. You need to define your counter and give it a name.

1a. Define Counter
I have named mine tidbit-counter. We give it a name so we can call it in the later steps.

1b. Initialize Counter

The next part is to initialize your counter. By default, the value of this is 0. Note, this number is not displayed. This is just where you setting the "starting" of your counter. So if I had set this number to be 20, then my output would go 21, 22, 23...etc. Assuming that my increment is 1 (more on this later).

Here’s an example:

HTML

Output

Where to apply the counter-reset property?

You want to apply the counter-reset property on the parent element. Here's what happens if you don't apply it to the parent.

And here’s the output. As you noticed, it doesn’t increment properly 😖

Also, it doesn’t have to be the direct parent. As long as it’s an HTML element that wraps your counter list. You’re good. Like this:

CSS

Output

2. Increment Counter

Once you set up your counter. Now you can start incrementing it. Here’s the syntax for this property:

As you noticed, it accepts an integer argument. That means you are not restricted to just increasing the counter value by 1. Below chart assumes counter-reset is 0.

And yes, you can also pass in a negative integer to decrease the counter. Okay, let’s see how that would be implemented:

HTML

Output

3. Display Counter

Finally, to display the counter, we need to pass the counter function as the value for our content property. The content property is often the way for us to display value in our HTML through CSS. Here's the syntax for our counter function.

By default, we have been working numbers. And that’s the default counter list style or in the docs, they call it style. But you can also pass in other styles.

You can see the full list of styles here

Example

HTML

Output

Multiple Counters

You can also set multiple counters just by simply defining another counter name.

HTML

Output

Nested Counter

You can also set a nested counter. Instead of using counter, you use the plural form counters. The counters accept an additional argument:

The string argument is a string separator that you use to indicated how you want to separate the list item for nested counters.

Let’s look at an example:

HTML

Output

counter vs <ol>

CSS counter doesn’t replace <ol>. If you have a numbered ordered list, then you should absolutely still use <ol> because it's important that you structure your HTML using the proper semantics. Semantic markup is crucial for Accessibility and SEO.

<ol> for the win

Here’s an example where I should use <ol>. In this instance, I'm just listing a bunch of rules. It makes semantic sense to use an ordered list <ol>.

CSS counter for the win

Here’s an example where I would use CSS counter. In this instance, I have a documentation page using headings h2 and paragraphs p. In this example, having a counter is more of a visual presentation. This example would make sense using CSS counter.

Output

☝️Can you tell I really like Vue.js 😝

Browser Support

The CSS counter is supported by all major browsers including Internet Explorer 8 and above.

Can I Use: CSS Counters

Thanks for reading ❤

Say Hello! Instagram | Facebook | Twitter | SamanthaMing.com | Blog

Frontend Developer sharing weekly JS, HTML, CSS code tidbits🔥 Discover them all on samanthaming.com 💛

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store