--- title: counter() slug: Web/CSS/counter() tags: - CSS - CSS函数 - CSS计数器 translation_of: Web/CSS/counter() ---
/* 简单使用 */ counter(计数器名称); /* 更改计数器显示 */ counter(countername, upper-roman)
一个计数器本身没有可见的效果,而是通过counter()
函数(和counters()
函数)返回开发人员定义的字符串(或图像) 从而使计数器拥有很棒的作用。
语法
<custom-ident> 自定义标识
none
, unset
, initial
, 或 inherit
。<counter-style> 计数器样式
symbols()
函数,其中计数器样式名称是数字,字母或符号的简单预定义计数器样式,复杂的东亚或埃塞俄比亚长手预定义计数器样式,或其他预定义计数器样式。如果省略,则计数器样式默认为十进制。counter( <custom-ident>, <counter-style>? ) where<counter-style> = <counter-style-name> | symbols()
where<counter-style-name> = <custom-ident>
示例
<ol> <li></li> <li></li> <li></li> </ol>
ol { counter-reset: listCounter; } li { counter-increment: listCounter; } li::after { content: "[" counter(listCounter) "] == [" counter(listCounter, upper-roman) "]"; }
{{EmbedLiveSample("default_value_compared_to_upper_Roman", "100%", 150)}}
<ol> <li></li> <li></li> <li></li> </ol>
ol { counter-reset: count; } li { counter-increment: count; } li::after { content: "[" counter(count, decimal-leading-zero) "] == [" counter(count, lower-alpha) "]"; }
{{EmbedLiveSample("decimal-leading-zero_compared_to_lower-alpha", "100%", 150)}}
Specification | 状态 | 注释 |
---|---|---|
{{SpecName("CSS3 Lists", "#counter-functions", "CSS Counters")}} | {{Spec2("CSS3 Lists")}} | No change |
{{SpecName("CSS2.1", "generate.html#counter-styles", "CSS Counters")}} | {{Spec2("CSS2.1")}} | Initial definition |
{{Compat("css.types.counter")}}
另请参阅
counters()
函数