CSS counter-set Atrributo
- 上一頁 counter-reset
- 下一頁 @counter-style
定义和用法
counter-set
属性用于创建并将 CSS 计数器设置为特定值。
counter-set
属性通常与 counter-increment 属性和 content 属性一起使用。
实例
例子 1
创建一个计数器("my-counter"),将其设置为 5,并在每次出现 <h2> 选择器时将其增加 1:
body { /* 将 "my-counter" 设置为 5 */ counter-set: my-counter 5; } h2::before { /* 將 "my-counter" 增加 1 */ counter-increment: my-counter; content: "Section " counter(my-counter) ". "; }
例子 2
创建一个计数器("my-counter"),将其设置为 5,并在每次出现 <h2> 选择器时将其减少 1:
body { /* 将 "my-counter" 设置为 5 */ counter-set: my-counter 5; } h2::before { /* 将 "my-counter" 减少 1 */ counter-increment: my-counter -1; content: "Section " counter(my-counter) ". "; }
例子 3
使用 "Section 10:"、"10.1"、"10.2" 等格式为章节和子章节编号:
body { /* 将 "section" 设置为 9 */ counter-set: section 9; } h1 { /* 將 "subsection" 設為 0 */ counter-set: subsection 0; } h1::before { /* 將 "section" 增加 1 */ counter-increment: section; content: "Section " counter(section) ": "; } h2::before { /* 將 "subsection" 增加 1 */ counter-increment: subsection; content: counter(section) "." counter(subsection) " "; }
例子 4
創建一個計數器,將其設為 9,並在每次出現 <h2> 選擇器時將其增加 1(使用羅馬數字):
body { /* 將 "my-counter" 設為 9 */ counter-set: my-counter 9; } h2::before { /* 將 "my-counter" 增加 1 */ counter-increment: my-counter; content: counter(my-counter, upper-roman) ". "; }
CSS 語法
counter-set: none|counter-name number|initial|inherit;
屬性值
值 | 描述 |
---|---|
none | 默認值。不設置計數器。 |
counter-name number |
要設定的計數器名稱及其值。 每次出現選擇器時,計數器將被設定為該值。 默認數值為 0。 |
initial | 將此屬性設定為其默認值。參閱 initial。 |
inherit | 從其父元素繼承此屬性。參閱 inherit。 |
默認值: | none |
---|---|
繼承性: | 否 |
動畫製作: | 不支持。請參閱:動畫相關屬性。 |
版本: | CSS2 |
JavaScript 語法: | object.style.counterSet="4" |
瀏覽器支持
表格中的數字注明了完全支持該屬性的首個瀏覽器版本。
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
85 | 85 | 68 | 17.2 | 71 |
相關頁面
參考:::before 偽元素
參考:::after 偽元素
- 上一頁 counter-reset
- 下一頁 @counter-style