--- title: '初始值' slug: Web/CSS/@property/initial-value tags: - CSS - Reference - Web - Property - Houdini ---
initial-value CSS描述符在使用{{cssxref("@property")}} {{cssxref("at-rule")}}的时候是必需的,除非syntax属性接受了任何有效的token流。 它为属性设置初始值。
被选定为initial-value的参数,依照syntax描述符定义,必须可以正确地解析。 因此,如果syntax描述符为<color>,那么初始值必须是一个有效的{{cssxref("color")}}值。
@property --property-name {
syntax: '<color>';
inherits: false;
initial-value: #c0ffee;
}
@property --property-name {
syntax: '<color>';
inherits: true;
initial-value: #c0ffee;
}
对于设定的syntax,具有正确值的字符串。
{{cssinfo}}
{{csssyntax}}
为 --my-color {{cssxref('--*', '自定义属性')}}添加颜色值类型检测、设置默认值并且设置属性值不允许被继承。
使用 CSS {{cssxref('@property')}} 规则:
@property --my-color {
syntax: '<color>';
inherits: false;
initial-value: #c0ffee;
}
使用 JavaScript中的 {{domxref('CSS.registerProperty')}}函数:
window.CSS.registerProperty({
name: '--my-color',
syntax: '<color>',
inherits: false,
initialValue: '#c0ffee',
});
{{csssyntax}}
| 规范 | 状态 | 提案 |
|---|---|---|
| {{SpecName('CSS Properties and Values API', '#at-property-rule')}} | {{Spec2('CSS Properties and Values API')}} | 初始定义. |
{{Compat("css.at-rules.property")}}