CSS Portal

counter() CSS Function

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
counter()
CSS FunctionThis function is supported by all modern browsers.
Updated 31st December 2025

Description

The counter() CSS function is used to retrieve the current value of a CSS counter-reset or counter-increment for a specified counter and display it in generated content, typically within the content property. It allows you to insert numeric values dynamically into your web page without altering the HTML structure, making it particularly useful for automatically numbered lists, headings, or sections.

Counters in CSS are essentially named variables that track numeric values. You can initialize a counter using counter-reset, increment it with counter-increment, and then display it using counter(). By default, counter() displays the value as a decimal number, but you can combine it with counters() or specify a list-style-type to change the formatting (e.g., Roman numerals, letters, etc.).

A simple example of counter() usage is numbering paragraphs within a div:

div {
  counter-reset: section;
}

div p::before {
  counter-increment: section;
  content: "Section " counter(section) ": ";
}

In this example:

  • The counter named section is reset at the start of each div.
  • Each p inside the div increments the counter.
  • The counter() function then displays the current counter value before the paragraph text.

This approach allows for automatic numbering without hardcoding numbers in the HTML, keeping content flexible and easier to maintain. You can also use counter() multiple times in the content property to display counters in complex formats, such as nested lists or multi-level headings.

Syntax

<counter()> = counter( <counter-name> , <counter-style>? )

Values

  • <counter-name>
  • <counter-style>

Example

<div class="container">
<h2>Introduction</h2>
<h2>Getting Started</h2>
<h2>Advanced Techniques</h2>
<h2>Conclusion</h2>
</div>
/* 1. Start the counter at 0 */
.container {
counter-reset: section-counter;
}

/* 2. Tell CSS which element triggers the count */
h2 {
counter-increment: section-counter;
display: flex;
align-items: center;
font-family: sans-serif;
}

/* 3. Show the number before the heading text */
h2::before {
/* This is where the magic happens */
content: "Section " counter(section-counter) ": ";

/* Styling the number specifically */
background-color: #007bff;
color: white;
padding: 2px 8px;
border-radius: 4px;
margin-right: 10px;
font-size: 0.8rem;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS counter() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee