@counter-style
CSS
at-rule
lets you define counter styles that are not part of the predefined set of styles. A
@counter-style
rule defines how to convert a counter value into a string representation.
@counter-style thumbs {
system: cyclic;
symbols: "\1F44D";
suffix: " ";
}
ul {
list-style: thumbs;
}
The initial version of CSS defined a set of useful counter styles. However, although more styles were added to this set of predefined styles over the years, this system proved too restrictive to fulfill the needs of worldwide typography. The
@counter-style
at-rule addresses this shortcoming in an open-ended manner, by allowing authors to define their own counter styles when the pre-defined styles aren't fitting their needs.
每个
@counter-style
is identified by a name and has a set of descriptors.
system
Specifies the algorithm to be used for converting the integer value of a counter to a string representation.
negative
Lets the author specify symbols to be appended or prepended to the counter representation if the value is negative.
prefix
Specifies a symbol that should be prepended to the marker representation. Prefixes are added to the representation in the final stage, so in the final representation of the counter, it comes before the negative sign.
suffix
Specifies, similar to the prefix descriptor, a symbol that is appended to the marker representation. Suffixes come after the marker representation.
range
Defines the range of values over which the counter style is applicable. If a counter style is used to represent a counter value outside of its ranges, the counter style will drop back to its fallback style.
pad
Is used when you need the marker representations to be of a minimum length. For example if you want the counters to start at 01 and go through 02, 03, 04 etc, then the pad descriptor is to be used. For representations larger than the specified pad value, the marker is constructed as normal.
fallback
Specifies a system to fall back into if either the specified system is unable to construct the representation of a counter value or if the counter value outside the specified range. If the specified fallback also fails to represent the value, then the fallback style's fallback is used, if one is specified. If there are either no fallback systems described or if the chain of fallback systems are unable to represent a counter value, then it will ultimately fall back to the decimal style.
symbols
@counter-style
rule uses images instead of character symbols. Image values for symbols is currently an 'at risk' feature, and is not implemented in any browser.
@counter-style winners-list {
system: fixed;
symbols: url(gold-medal.svg) url(silver-medal.svg) url(bronze-medal.svg);
suffix: " ";
}
additive-symbols
speak-as
Describes how to read out the counter style in speech synthesizers, such as screen readers. For example, the value of the marker symbol can be read out as numbers or alphabets for ordered lists or as audio cues for unordered lists, based on the value of this descriptor.
@counter-style <counter-style-name> { [ system: <counter-system>; ] || [ symbols: <counter-symbols>; ] || [ additive-symbols: <additive-symbols>; ] || [ negative: <negative-symbol>; ] || [ prefix: <prefix>; ] || [ suffix: <suffix>; ] || [ range: <range>; ] || [ pad: <padding>; ] || [ speak-as: <speak-as>; ] || [ fallback: <counter-style-name>; ] }where
<counter-style-name> = <custom-ident>
@counter-style circled-alpha {
system: fixed;
symbols: Ⓐ Ⓑ Ⓒ Ⓓ Ⓔ Ⓕ Ⓖ Ⓗ Ⓘ Ⓙ Ⓚ Ⓛ Ⓜ Ⓝ Ⓞ Ⓟ Ⓠ Ⓡ Ⓢ Ⓣ Ⓤ Ⓥ Ⓦ Ⓧ Ⓨ Ⓩ;
suffix: " ";
}
The above counter style rule can be applied to lists like this:
.items {
list-style: circled-alpha;
}
Which will produce lists like this:
Ⓐ One
Ⓑ Two
Ⓒ Three
Ⓓ Four
Ⓔ FIve
...
...
Ⓨ Twenty Five
Ⓩ Twenty Six
27 Twenty Seven
28 Twenty Eight
29 Twenty Nine
30 Thirty
See more examples on the demo page .
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Counter Styles Level 3
The definition of 'counter-style' in that specification. |
候选推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
@counter-style
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
additive-symbols
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
fallback
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
negative
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
pad
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
prefix
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
range
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
speak-as
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
suffix
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
symbols
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
system
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 33 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 33 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
不支持
none
as a value of the
system
and
fallback
descriptors of
@counter-style
, which as per spec should result in an invalid at-rule. Firefox's new parallel CSS engine (also known as
Quantum CSS
or
Stylo
, released in Firefox 57) fixed this (
bug 1377414
).
list-style
,
list-style-image
,
list-style-position
,
list-style-type
symbols()
, the functional notation creating anonymous counter styles.
counter()
and
counters()
函数