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

Create line numbers on pre with CSS only

I try to style a code pre box with line numbers in front of each line. I prefer to do it with CSS only. This is what I have done pre { background: #303030; color: #f1f1f1; padding: 10px 16px; border-radius: 2px; border-top: 4px…
Tasos
  • 7,325
  • 18
  • 83
  • 176
35
votes
3 answers

Use css counter in calc

I have a list ul>li*5 (not always the same amount). I set a counter for which I get: 1 2 3 4 5 li:nth-child(n):before { counter-increment: skill; content: counter(skill); color: white; } The Question Can I use the counter(skill) inside a…
T04435
  • 12,507
  • 5
  • 54
  • 54
32
votes
7 answers

Can you count a particular class with CSS?

Lets say I have a simple list like so:
  1. one
  2. two
  3. three
  4. four
  5. blabla
  6. five
  7. Danield
    • 121,619
    • 37
    • 226
    • 255
28
votes
10 answers

How can I reset a CSS-counter to the start-attribute of the given list

I am using a self-styled, numbered list. How can I read the start-attribute and add it to the counter with CSS? ol { list-style-type: none; /* this does not work like I expected */ counter-reset: lis attr(start, number, 0); } li { …
Möhre
  • 929
  • 1
  • 6
  • 17
17
votes
1 answer

Is there any way to insert an element's index (child number) as text within the element solely using CSS?

My objective is to print a text file line by line and append the line number at the beginning. Like so:
1: asdf
2: asdfasdf
3: asdfasdfasdfasdf
JackHasaKeyboard
  • 1,599
  • 1
  • 16
  • 29
16
votes
3 answers

CSS counter-reset on nested list

I'm struggling with counter-reset when ol is in div. Red list in the snippet numbering should be: 1, 2, 3 not: 3.1, 3.2, 3.3, ol { counter-reset: item; list-style: none; } li:before { counter-increment: item; …
vrmtm
  • 163
  • 1
  • 6
11
votes
2 answers

How do I stop

In the code below, I need to use the div tag at the top of the HTML for styling. Without the div tag in place, the hx tags are outline numbered correctly, but with the div in place everything goes completely wrong. I need this to work like this, but…
Alby
  • 135
  • 6
11
votes
1 answer

CSS list with multiple counters doesn't count properly

I have a
    -list with two different counters: ul.numbered { counter-reset: alphaCounter, numberCounter; } ul.numbered li.numbered, ul.numbered li.lettered { padding-left: 1.8em; } ul.numbered li.numbered:before { position: absolute; …
R4ttlesnake
  • 1,661
  • 3
  • 18
  • 28
11
votes
1 answer

CSS counter-reset does not work inside pseudo elements

The following CSS counter example does not work as expected. The counter for sub-headings should reset after every main heading: body { font: smaller sans-serif; counter-reset: h1 h2; } h1:before { counter-reset: h2; content:…
Salman A
  • 262,204
  • 82
  • 430
  • 521
10
votes
2 answers

Using CSS to auto-number nested sections

Say I have an HTML or XML document like this:

This should be numbered 1

This should be numbered 1.1

blah

This should be…

Michael Dyck
  • 2,153
  • 1
  • 14
  • 18
9
votes
1 answer

CSS Animation to Increment Counter

I've been playing around with CSS counters lately and I wondered if it was possible to put them in a CSS animation loop and increment them. My code is fairly simple: div { animation-name: anim; animation-duration: 0.5s; …
Kenton de Jong
  • 1,001
  • 4
  • 17
  • 37
8
votes
3 answers

CSS list counter increase level

HTML
  1. item1
    1. item2
  2. item1
    1. item2
    2. item3
SCSS ol { counter-reset: item; li { display: block } li:before { content:…
simPod
  • 11,498
  • 17
  • 86
  • 139
7
votes
3 answers

Using css counters to generate ordinal values?

I'm guessing this isn't possible, but wondering if anybody has attempted to produce ordinal numbers (1st, 2nd, 3rd etc) using CSS Counters with any success? Obviously this would be trivial in JavaScript, but was hoping to find a style-only solution.
shennan
  • 10,798
  • 5
  • 44
  • 79
7
votes
1 answer

CSS to add leading zero to an ordered list custom counter

What I have: An ordered list with a custom counter: ol { /*list-style-type:decimal-leading-zero;*/ list-style: none; padding-left: 0px; counter-reset: item; } ol>li { display: table; counter-increment:…
Clarus Dignus
  • 3,847
  • 3
  • 31
  • 57
7
votes
5 answers

How can I increment the counter in my css code?

In my html code I have the following list defined:

first.

second.

Now, in css I have: .list { display: table; } .list:before { display: table-cell; counter-increment: counter; content:…
user3766930
  • 5,629
  • 10
  • 51
  • 104
1
2 3
10 11