counter-set
Baseline 2023Newly available
Since December 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
尝试一下
counter-set: none;
counter-set: chapter-count 0;
counter-set: chapter-count;
counter-set: chapter-count 5;
counter-set: chapter-count -5;
<section class="default-example" id="default-example">
<div class="transition-all" id="chapters">
<h1>Alice's Adventures in Wonderland</h1>
<h2>Down the Rabbit-Hole</h2>
<h2 id="example-element">The Pool of Tears</h2>
<h2>A Caucus-Race and a Long Tale</h2>
<h2>The Rabbit Sends in a Little Bill</h2>
</div>
</section>
#default-example {
text-align: left;
counter-set: chapter-count;
}
#example-element {
background-color: #37077c;
color: white;
}
h2 {
counter-increment: chapter-count;
font-size: 1em;
}
h2::before {
content: "Chapter " counter(chapter-count) ": ";
}
备注:
计数器的值可以通过 counter-increment
CSS 属性进行递增或递减。
语法
css
/* 将 "my-counter" 设置为 0 */
counter-set: my-counter;
/* 将 "my-counter" 设置为 -1 */
counter-set: my-counter -1;
/* 将 "counter1" 设置为 1,将 "counter2" 设置为 4 */
counter-set: counter1 1 counter2 4;
/* 取消在不太具体的规则中可能设置的任何计数器 */
counter-set: none;
/* 全局值 */
counter-set: inherit;
counter-set: initial;
counter-set: revert;
counter-set: revert-layer;
counter-set: unset;
counter-set
属性可以指定为以下之一:
- 一个命名计数器的
<custom-ident>
,后面可跟一个<integer>
。你可以指定要重置的计数器数量,每个名称或名称—数字对之间用空格分隔。 - 关键字值
none
。
值
<custom-ident>
-
要设置的计数器的名称。
<integer>
-
在元素每次出现时设置计数器的值。如果未指定,则默认为
0
。如果当前元素上没有给定名称的计数器,元素将创建一个给定名称的新计数器,起始值为 0(尽管它可能会立即将该值设置或增加为不同的值)。 none
-
不执行计数器设置。这可以用来覆盖在较不具体的规则中定义的
counter-set
。
形式定义
形式语法
示例
设置命名计数器
css
h1 {
counter-set: chapter section 1 page;
/* 将章节和页面计数器设置为 0,
将部分计数器设置为 1 */
}
规范
Specification |
---|
CSS Lists and Counters Module Level 3 # propdef-counter-set |