初始值

Baseline 2024
Newly available

Since July 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

initial-value CSS描述符在使用@property at-rule的时候是必需的,除非 syntax 属性接受了任何有效的 token 流。它为属性设置初始值。

被选定为initial-value的参数,依照syntax描述符定义,必须可以正确地解析。因此,如果syntax描述符为<color>,那么初始值必须是一个有效的color值。

语法

css
@property --property-name {
  syntax: "<color>";
  inherits: false;
  initial-value: #c0ffee;
}

@property --property-name {
  syntax: "<color>";
  inherits: true;
  initial-value: #c0ffee;
}

取值

对于设定的syntax,具有正确值的字符串。

形式定义

Related at-rule@property
初始值n/a (required)
计算值as specified

形式语法

initial-value = 
<declaration-value>?

实例

--my-color 自定义属性添加颜色值类型检测、设置默认值并且设置属性值不允许被继承。

使用 CSS @property 规则:

css
@property --my-color {
  syntax: "<color>";
  inherits: false;
  initial-value: #c0ffee;
}

使用 JavaScript 中的 CSS.registerProperty函数:

js
window.CSS.registerProperty({
  name: "--my-color",
  syntax: "<color>",
  inherits: false,
  initialValue: "#c0ffee",
});

规范

Specification
CSS Properties and Values API Level 1
# initial-value-descriptor

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
initial-value descriptor

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见