negative

Baseline 2023
新推出

自 2023 年 9 月起,此功能可在最新的设备和浏览器版本上使用。此功能可能无法在较旧的设备或浏览器上使用。

@counter-style at-rule 的 negative 描述符允许你定义在定义自定义计数器样式时,负计数器值的表示方式。negative 描述符的值定义了当计数器值为负时,要添加到计数器表示之前和之后的符号。

语法

css
/* One <symbol> value */
negative: "--"; /* Adds '--' before if counter value is negative */

/* Two <symbol> values */
negative: "(" ")"; /* Adds '(- before and ')' after if counter value is negative */

negative 描述符接受最多两个 <symbol> 值。

<symbol>

如果只指定一个值,则当计数器为负时,它会被添加到计数器表示之前。如果指定两个值,则第一个值会在计数器为负时添加到计数器表示之前,第二个值会添加到计数器表示之后。

描述

如果计数器值为负,则 negative 描述符指定的 <symbol> 会添加到计数器表示之前,替换负值默认的 -。第二个 <symbol>(如果指定)会添加到计数器表示之后。

negative 描述符在两种情况下相关:如果计数器样式具有 symbolicalphabeticnumericadditivesystem 值且计数为负;以及如果 system 值为 extends 且扩展的计数器样式本身使用负号。对于不支持负计数器值的系统,指定 negative 描述符没有效果并会被忽略。

正式定义

相关的 at-rule@counter-style
初始值"-" 连字符-减号
计算值同指定值

正式语法

negative = 
<symbol> <symbol>?

<symbol> =
<string> |
<image> |
<custom-ident>

<image> =
<url> |
<image()> |
<image-set()> |
<cross-fade()> |
<element()> |
<gradient>

<image()> =
image( <image-tags>? [ <image-src>? , <color>? ]! )

<image-set()> =
image-set( <image-set-option># )

<cross-fade()> =
cross-fade( <cf-image># )

<element()> =
element( <id-selector> )

<image-tags> =
ltr |
rtl

<image-src> =
<url> |
<string>

<image-set-option> =
[ <image> | <string> ] [ <resolution> || type( <string> ) ]?

<cf-image> =
[ <image> | <color> ] &&
<percentage [0,100]>?

<id-selector> =
<hash-token>

示例

渲染负计数器

此示例扩展decimal列表样式。negative描述符用于在负计数器值之前和之后添加(-)

HTML

html
<ol start="-3">
  <li>Negative three</li>
  <li>Negative two</li>
  <li>Negative one</li>
  <li>Zero</li>
  <li>One</li>
</ol>

CSS

css
@counter-style neg {
  system: extends decimal;
  negative: "(-" ")";
  suffix: ": ";
}

ol {
  list-style: neg;
}

结果

只有当计数器值小于零时,才会在标记中添加列为 negative 描述符值的前缀后缀

规范

规范
CSS Counter Styles Level 3
# counter-style-system

浏览器兼容性

另见