-
Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
170 lines (124 loc) · 5.33 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: counter-increment
slug: Web/CSS/counter-increment
page-type: css-property
browser-compat: css.properties.counter-increment
---
{{CSSRef}}
The **`counter-increment`** [CSS](/en-US/docs/Web/CSS) property can be used to increase or decrease the value of the named [CSS counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) by the specified values, or to prevent all counters or an individual counter's value from being changed.
If a named counter in the list of space-separated counters and values doesn't exist, it will be created. If no value is provided for a counter in the list of counters, the counter will be increased by `1`.
The counter's value can be reset to any integer value with the {{cssxref("counter-reset")}} CSS property.
{{InteractiveExample("CSS Demo: counter-increment")}}
```css interactive-example-choice
counter-increment: example-counter;
```
```css interactive-example-choice
counter-increment: example-counter 0;
```
```css interactive-example-choice
counter-increment: example-counter 5;
```
```css interactive-example-choice
counter-increment: example-counter -5;
```
```html interactive-example
<section class="default-example" id="default-example">
<div class="transition-all" id="example-element">Counter value:</div>
</section>
```
```css interactive-example
#default-example {
text-align: left;
counter-reset: example-counter;
}
#example-element::after {
content: counter(example-counter);
}
```
## Syntax
```css
/* Increases "my-counter" by 1 */
counter-increment: my-counter;
/* Decreases "my-counter" by 1 */
counter-increment: my-counter -1;
/* Increases "counter1" by 1 and decreases "counter2" by 4 */
counter-increment: counter1 counter2 -4;
/* Increases "page" by 1, "section" by 2, while "chapter" doesn't change */
counter-increment: chapter 0 section 2 page;
/* Do not increment/decrement anything: used to override less specific rules */
counter-increment: none;
/* Global values */
counter-increment: inherit;
counter-increment: initial;
counter-increment: revert;
counter-increment: revert-layer;
counter-increment: unset;
```
### Values
The `counter-increment` property takes as its value either a list of space-separated counter names specified as `<custom-ident>` with an optional `<integer>` value or the keyword `none`. You may specify as many counters to increment as you want, with each name or name-number pair separated by a space.
- {{cssxref("<custom-ident>")}}
- : Specifies the name of the counter to increase or decrease.
- {{cssxref("<integer>")}}
- : Specifies the value to add to the counter. If the integer is preceded by a `-` sign, the value will be subtracted from the counter. Defaults to `1` if no value is specified.
- `none`
- : Indicates that no counter must be increased or decreased. This value can also be used to cancel all counters from being increased or decreased in more specific rules. This is the default value of the property.
> [!NOTE]
> Using the `none` value prevents all counters from being increased or decreased for the selected elements where this rule applies. To prevent only specific counters from being increased or decreased, set the `integer` value as `0` on the relevant counter(s).
## Formal definition
{{cssinfo}}
## Formal syntax
{{csssyntax}}
## Examples
### Decreasing the counter value
In this example, we display a sequence of numbers counting backward. To do this, we use a counter to display numbers starting from 100 and decreasing by 7 each time.
#### HTML
```html
<div>
<i></i><i></i><i></i><i></i><i></i><i></i><i></i> <i></i><i></i><i></i><i></i
><i></i><i></i><i></i> <i></i><i></i><i></i><i></i><i></i><i></i><i></i>
<i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</div>
```
#### CSS
We set the initial value of the counter named `sevens` to `100` by using {{cssxref("counter-reset")}}. Then, for each {{HTMLElement("i")}}, we decrease the counter by `7`.
To set the first count at `100`, we target the first `<i>` element by using the {{cssxref(":first-of-type")}} pseudo-class and setting `counter-increment: none;`. Additionally, the {{cssxref("content")}} property is used in the {{cssxref("::before")}} pseudo-element to display the value of the counter using the [`counter()`](/en-US/docs/Web/CSS/counter) function.
```css
div {
counter-reset: sevens 100;
}
i {
counter-increment: sevens -7;
}
i:first-of-type {
counter-increment: none;
}
i::before {
content: counter(sevens);
}
```
```css hidden
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
width: 200px;
}
i {
flex: 0 0 2em;
}
```
#### Result
{{EmbedLiveSample("Decreasing the counter value", 140, 300)}}
Had we not used `counter-reset` (or {{cssxref("counter-set")}}) to create the counter and set the value to `100`, the `sevens` counter would still have been created, but with an initial value `0`.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- Counter properties: {{cssxref("counter-set")}}, {{cssxref("counter-reset")}}
- Counter at-rule: {{cssxref("@counter-style")}}
- Counter functions: {{cssxref("counter", "counter()")}}, {{cssxref("counters", "counters()")}}
- [Using CSS counters](/en-US/docs/Web/CSS/CSS_counter_styles/Using_CSS_counters) guide
- [CSS lists and counters](/en-US/docs/Web/CSS/CSS_lists) module
- [CSS counter styles](/en-US/docs/Web/CSS/CSS_counter_styles) module