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 |
瀏覽器相容性
載入中…