Favorites

counter() CSS Function

counter()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The counter() function inserts the current value of a CSS counter as text, usually in the content property, such as counter(section).

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

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. An optional second argument chooses a different counter style, such as upper-roman, lower-alpha, a style defined with @counter-style, or an inline style made with symbols(), for example counter(section, upper-roman). To show nested counters such as "1.2.3", use counters() instead.

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.

Interactive Demo

  1. Mix the dry ingredients
  2. Add the milk and eggs
  3. Bake for 30 minutes
Demo Code
HTML Code
<ol class="demo-element">
	<li>Mix the dry ingredients</li>
	<li>Add the milk and eggs</li>
	<li>Bake for 30 minutes</li>
</ol>
CSS Code
.demo-element {
	width: 260px;
	margin: 0;
	padding: 10px 14px;
	list-style: none;
	background: #e7ecff;
	border: 2px solid #4361ee;
	font-size: 15px;
	line-height: 1.8;
	color: #1b1f3b;
}
.demo-element ol {
	margin: 0;
	padding-left: 20px;
	list-style: none;
}
.demo-element, .demo-element ol {
	counter-reset: item;
}
.demo-element li {
	counter-increment: item;
}
.demo-element li::before {
	color: #f72585;
	font-weight: bold;
	content: counter(item) ". ";
}

Syntax

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

Values

  • <counter-name>The name of the counter, a case-sensitive <custom-ident> that has been created with counter-reset, counter-increment or counter-set. The name list-item refers to the built-in counter used by ordered lists. If no counter with that name exists, one is created with the value 0.
  • <counter-style>Optional. The style used to display the value: a list-style-type keyword such as decimal (the default), decimal-leading-zero, lower-roman, upper-roman, lower-alpha or upper-alpha, the name of an @counter-style rule, or a symbols() function. The value none displays nothing.

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

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.