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

add padding for RTL content after use row_number CSS

I used below CSS code for add row number to a table rows: table { direction: rtl; counter-reset: line-number; } td:first-child { text-align: right !important; } td:first-child:before { content: counter(line-number) "."; …
b24
  • 2,425
  • 6
  • 30
  • 51
0
votes
2 answers

Separate ordered lists with continuous numbering by CSS counter

I need to create ordered lists for a specific type of genealogy report called a register. In a register, all children are numbered with lower-case Roman numerals, and those with descendants also get an Arabic numeral, like this: First Generation 1.…
Eric Stoltz
  • 101
  • 3
0
votes
1 answer

Using CSS counters to create sub-sections with a counter-reset

I'm trying to get CSS counters to increment items that indicate sub-items. Perhaps because the HTML is not nested, the reset doesn't look like it's working as I'd expect. Can anyone help me understand why? If I reset the sub-item counter in the…
end-user
  • 2,845
  • 6
  • 30
  • 56
0
votes
3 answers

How to properly apply CSS style to H elements in ordered list

I have this small problem, I am building a nested ordered list, and I want to be able to customize the styles of top level elements (note that bold is both on the counter and text). 1. AAA 1.1 Lorem ipsum 1.2 Lorem ipsum 1.3 Lorem ipsum 2. BBB 2.1…
Andy Andy
  • 279
  • 4
  • 7
  • 17
0
votes
2 answers

Is it possible to have a text box with CSS Counter increment value?

I have created a dynamic table, with PHP and jQuery and am using CSS for creating auto increment Serial Number. This is the code Requirement: At the moment, the column in which the serial numbers are auto generated are blank. Is it possible to…
SR1092
  • 555
  • 1
  • 7
  • 31
0
votes
1 answer

HTML - ordered list, styling the list numbers like (a), (i), (aa)

I have attached my HTML and CSS file. What I need: the "a." should be enclosed within () like (a) and the "i" in the next level as (i) and under the "i" the list number shown as "1" should be displayed as (aa) next as (ab) and so on. I want the…
Manoj Shenoy
  • 333
  • 1
  • 3
  • 12
0
votes
1 answer

Why are my
tags ignored by CSS counters?

In the code below, it looks like all the
tags are getting ignored by CSS counters. What's the reason for this?