Questions tagged [css-counter]

CSS counters are variables that are maintained and manipulated by CSS rules to count the number of times they have been used within the document using the counter-increment, counter-set, counter-reset properties, and read with counter() and counters(). Their value is inaccessible outside of CSS and are generally used as pseudo-element content. Add this tag for questions relating to the usage of CSS counters. For other types of counters, use the [css] tag.

Description

CSS counters are variables that are maintained and manipulated by CSS rules to count the number of times they have been used within the document.


Counter Manipulation

The value of the counter variable can be modified by using either of the following properties:

Counter Reset - This sets the value of the counter to either 0 (default) or a specified value. This is the first step in the usage of counters as it initializes the counter variable.

Syntax is counter-reset: [counter-name] [value].

Multiple counters can also be reset at the same time by providing the counter names and its values in a space separated manner (like counter-reset: counter1 3 counter2 5;).

Counter Increment - This increments the value of counter every-time the CSS selector rule is matched. The default incrementation is by 1 but similar to the counter reset property, here also the incrementation factor can be specified.

Syntax is counter-increment: [counter-name] [incrementation-factor].

Again similar to the counter reset property, here also multiple counters can be incremented at the same time by using a space separated list.


Counter Value Display

All counter values can currently be displayed only by using the content property of a pseudo-element. It is also possible to style the counter by specifying the style as the second argument. The list of supported values are the same as for the list-style-type property.

Syntax is content: counter([counter-name], [counter-style]).

The counter values are not accessible outside of CSS (that is, their value currently cannot be read using JavaScript or the likes).


Example: Below is a sample snippet which counts the no. of rows in a table and dynamically adds the row number to every row. Here is a sample fiddle which shows the below code in action.

table {
    counter-reset: rows; /* initialize the counter */
}
tr {
    counter-increment: rows; /* increment the counter for every tr encountered */
}
td:first-child:before {
    content: counter(rows)". "; /* display the value of the counter */
}

References & Useful Links

155 questions
3
votes
1 answer

header tag interfering with css counters headings

When using the usual way to add section numbers to headings by using CSS counters I run into problems when using the header tag. It somehow seems to interfere with the counter(s) body { counter-reset: h1 } h1 { counter-reset: h2 } h2 { …
ingo
  • 117
  • 10
3
votes
3 answers

How to wrap text correctly in nested list with counter-increment property?

I have ordered list with counter-increment property. How to make long text, if it breaks into multiple lines, to have same offset from left as first line? Now second line is shown under the numbers. Here is the fiddle (look at 2.3 item):…
user1131522
  • 157
  • 9
3
votes
1 answer

Count hidden elements with CSS Counters

How can I not exclude a element from counting if it's set display: none;? body { counter-reset: section; } .variant--name::before { counter-increment: section; content: counter(section) ": "; } .hidden { display: none; }
Alexander Hein
  • 960
  • 3
  • 19
  • 44
3
votes
1 answer

Is there a CSS counter equivalent for list-style-type?

CSS has a nice collection of values for list-style-type, Is there a way to use them as the value of content: counter(steps)? I want to use list-style-type: persian in counter value. Result would be: ۱ // (one) ۲ // (two) ۳ // (three) ۴ // (four) ۵…
Morteza
  • 2,097
  • 5
  • 28
  • 47
3
votes
1 answer

Ordered List in Arabic not working

I need show data in Order List in order. example 1, 2, 3, 3.1, 3.2, 4..... When I use following CSS, it shows table of content in proper order for English and if I have to show similar thing in Arabic then it doesn't work in Arabic. It shows Arabic…
Learning
  • 19,469
  • 39
  • 180
  • 373
3
votes
2 answers

How to add numbered list style in div tag

I have div tags with span tag. How to add numbered list for the div tag using classname in CSS or JavaScript
nde
Smila
  • 1,130
  • 8
  • 16
3
votes
1 answer

CSS counter on hidden submenu

I'm trying to make a dropdown menu using nested
    , every
  • displaying a number generated with CSS Counters. Sub-menus are hidden with display:none when not hovered. My problem is that counters are not incremented when an element has display…
FLX
  • 2,626
  • 4
  • 26
  • 57
2
votes
1 answer

How does one add something as a dot after a counter inside a content property in TailwindCSS?

I have this numbered list (I have omitted the counter-reset and counter-increment for legibility):
  1. I like cheese
which results in 1I like cheese I would like to add a…
roye7777777
  • 394
  • 1
  • 2
  • 13
2
votes
2 answers

Increment Counter in CSS for a list with :before

I tried to increment a counter for a list with CSS. However, even though I can see the list numbers, those don't increment. I used :before and the CSS property counter. Here's the code: .services__details--content__step>ul>li:before { content:…
Sofiane R
  • 93
  • 10
2
votes
2 answers

CSS Nested Ordered Lists Counter Manipulation

I would like to start nested ordered lists with specific numbers while keeping numbering normal (based on the first starting number) for nested list items. Rather than send people down the wrong path and show the numerous versions of scripts I have…
2
votes
1 answer

HTML Heading counter within table

I need to have text formatted in table where second column contains the text, some rows contains heading H1 - H5. I would like to use auto numbering for the heading but the increment does not work within table. Problem is that the H2 level counter…
Marek
  • 23
  • 3
2
votes
0 answers

include css counters in anchor links?

Problem summary I'm creating a print stylesheet for a simple html document with id-referenced headers and anchors to reference these headers, like

The Document

Background

Dominik Schreiber
  • 2,629
  • 1
  • 23
  • 33
2
votes
1 answer

Returned wrong value when trying to animate as "Counter"

I'm trying to animate with "Counter" animation some values that I get from my API by using the following method every time the data from API is changed: $('.count').each(function() { var $this = $(this); jQuery({ Counter: 0 }).animate({ …
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
2
votes
1 answer

Is Firefox violating spec by its odd cumulative application of counting to LI markers when there's a counter-reset declaration?

Usually when CSS counters are used on lists, the OL list-style-type is set to none and and ::before pseudo-elements are used instead of true list markers. However, I noticed that in Firefox (69.0, target:x86_64-apple-darwin, running on MacOS Mojave)…
Jacob C.
  • 345
  • 1
  • 16
2
votes
1 answer

Using CSS counter-reset in :host declaration of a Custom Element

[run the code snippet] I want my DIV number display to start at 0 , so I want to start the counter at -1 using: counter-reset : square -1; Yet, this setting is ignored when used in :host counter-reset works fine when all DIVs are wrapped in an extra…
Danny '365CSI' Engelman
  • 16,526
  • 2
  • 32
  • 49