counter-set
counter-set CSS 属性用于将元素上的 CSS 计数器设置为指定值。
如果计数器不存在,counter-set 属性会为以空格分隔的计数器名称和值对列表中每个命名的计数器创建一个新的计数器。但是,建议使用 counter-reset CSS 属性来创建新的计数器。
如果列表中某个命名的计数器缺少值,则该计数器的值将被设置为 0。
试一试
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
/* Set "my-counter" to 0 */
counter-set: my-counter;
/* Set "my-counter" to -1 */
counter-set: my-counter -1;
/* Set "counter1" to 1, and "counter2" to 4 */
counter-set: counter1 1 counter2 4;
/* Cancel any counter that could have been set in less specific rules */
counter-set: none;
/* Global values */
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。
正式定义
正式语法
counter-set =
[ <counter-name> <integer>? ]+ |
none
示例
设置命名计数器
css
h1 {
counter-set: chapter section 1 page;
/* Sets the chapter and page counters to 0,
and the section counter to 1 */
}
规范
| 规范 |
|---|
| CSS 列表与计数器模块第 3 级 # propdef-counter-set |
浏览器兼容性
加载中…