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
4
votes
2 answers

Ordered list in HTML that is numbered using odd numbers

Using
    in HTML, I can get an ordered list to look like this: I like cheese Cookies are nice Cream is good ... How would I use
      to get an ordered list to only have odd numberings, like this? 1.I like cheese 3.Food is nice 5.I wish…
QCD_IS_GOOD
  • 435
  • 7
  • 22
4
votes
1 answer

CSS counter does not increment for

I am crafting a multicolumn, CSS-numbered, TOC like this: | 1. TOC Level-1 | 2. TOC Level-1 | | 1.1 TOC Level-2 | 2.1 TOC Level-2 | | 1.2 TOC Level-2 | 2.2 TOC Level-2 | | 1.3 TOC Level-2 | 3. TOC Level-1 | |…
Marc Smits
  • 53
  • 6

4
votes
1 answer

Custom, regular, ordered bullet list style

I want a list style like this (jsfiddle): As you can see, the list style is: not one of the styles provided by CSS, i.e., a custom made style is regular, i.e., I don't have to manually put values 1, 2, 3, etc. for the
  • s. So, if I have four…
  • Gaurang Tandon
    • 6,504
    • 11
    • 47
    • 84
    4
    votes
    1 answer

    Using CSS Counters

    How can I count a element if each of them is nested in a sepereate element ? .variant--name::before { counter-increment: section; content: "Abschnitt " counter(section) ": "; }
    Alexander Hein
    • 960
    • 3
    • 19
    • 44
    4
    votes
    1 answer

    Repeated series of number via CSS

    I'm trying to do something like this:

    1.1 Bananas

    1.1.1

    1.1.2

    1.1.3

    1.1 Apples

    1.1.1

    1.1.2

    1.1.3

    1.1…

    FlareforHelp
    • 85
    • 1
    • 10
    3
    votes
    1 answer

    Why are there two different results for the numbering of
    tags in Edge and Firefox?

    The following snippets produce different results in Edge and Firefox: dd { display: list-item; list-style-position: inside; list-style-type: decimal; counter-increment: 1; }
    • A
    • B
    • C
    JackFan
    • 41
    • 5
    3
    votes
    1 answer

    how to use same @keyframes and @property twice for more than 1 div?

    as you see in this simple css counter, https://codepen.io/bgbos/pen/pobZvwJ we have 4 div's each of them has a different initial value which must be appear at the end of animation but it don't , it all stop at one value and this is the original one…
    Sulay
    • 33
    • 2
    3
    votes
    1 answer

    How to fixed an element in css animation

    I want to do a CSS Countdown animation, which goes from 0 to 7, but when it reaches 7, it disappears, I would like to know how I can make 7 stay fixed. body { background: pink; } .wrapper { position: absolute; top: 50%; left:…
    PanteraSama
    • 85
    • 1
    • 3
    3
    votes
    2 answers

    HTML CSS Issues in implementing nested ordering which includes numbers, alphabets and roman numbers

    I have a requirment to implement a nested ordered structured html page very similar like this. Most of the part is done but I got stuck at 1 place in ordering of number. Please suggest. Below is the required structure and my code. 1.Main Line…
    Shashank
    • 712
    • 15
    • 33
    3
    votes
    1 answer

    Animated progressbar list Counter only count last aria-valuenow

    I'm currently doing my own online resume. I wanted to put several animated progress-bar for the skills section. I also integrated a counter to display the animation of the count percentage for each progress-bar, which is located in another div above…
    3
    votes
    1 answer

    CSS counter not incrementing

    The CSS counter in my example doesn't increment although I have specified it to do so. I've read some questions here regarding the nesting level of the elements that can cause this but in my case, all the elements are in the same level. The…
    Khalid Hussain
    • 345
    • 4
    • 16
    3
    votes
    1 answer

    How to increment Hex number

    I am trying to implement css counter in javascript. Till now i have seen that most of the counter has some intial hex value and increments by 1 accordingly. For cycle counters it repeats itself. but cjk-decimal is also type of number counter which…
    Pavan Tiwari
    • 3,077
    • 3
    • 31
    • 71
    3
    votes
    0 answers

    How to keep CSS counter consistant across tables?

    My HTML document is formatted to have its content printed. Each printed page is framed inside a table. The document also has headings laid out on the pages (h1...h5). The problem is when a a sub-heading (at level h2..h5) is continued on the next…
    Mahonri Moriancumer
    • 5,993
    • 2
    • 18
    • 28
    3
    votes
    6 answers

    How to store the last counter value in css and display as content in html?

    For example, my CSS looks like this: .page-number:after { counter-increment: page; } When show as content, for example 6 pages: .page-number:after { content: 'Page ' counter(page) ' of'; } I want to store the last counter -which is 6 - and…
    Crazy
    • 847
    • 1
    • 18
    • 41
    3
    votes
    3 answers

    How to copy value of div:after to other div

    I have a simple list of li elements that I want to count using the CSS counter() function. body {counter-reset: quantity} ul li {counter-increment: quantity} .sum:after {content: counter(quantity)}
    Marcin W
    • 57
    • 7
    1 2
    3
    10 11