CSS @property rule
Definition and Usage
CSS @property
Rules are used to define custom CSS properties directly in style sheets without running any JavaScript.
@property
Rules have data type checking and constraint features, can set default values, and define whether the property can inherit values.
Using @property
The Benefits:
- Type Checking and Constraints: It is necessary to specify the data type of the custom property, such as <number>, <color>, <length>, etc. This can prevent errors and ensure the correct use of custom properties.
- Setting Default Values: You can set a default value for the custom property. This ensures that if an invalid value is assigned later, the browser will use the defined fallback value.
- Setting Inheritance Behavior: It is necessary to specify whether the custom property can inherit values from its parent element.
Example: Define Custom Properties
@property --my-color { syntax: "<color>"; inherits: true; initial-value: lightgray; }
The above definition indicates that --my-color is a color property that can inherit values from its parent element and has a default value of lightgray.
Using Custom Properties in CSS:
body {}} backgound-color: var(--my-color); }
Instance
Example 1
Specify two custom properties for the gradient - and use them to animate the gradient:
@property --startColor { syntax: "<color>"; initial-value: #EADEDB; inherits: false; } @property --endColor { syntax: "<color>"; initial-value: #BC70A4; inherits: false; }
Example 2
Specify two custom properties: one for item size and one for item color:
@property --item-size { syntax: "<percentage>"; inherits: true; initial-value: 50%; } @property --item-color { syntax: "<color>"; inherits: false; initial-value: lightgray; }
CSS syntax
@property --propertyname { syntax: "<color>"; initial-value: red; inherits: false; }
Property value
Value | Description |
---|---|
--propertyname | Required. The name of the custom property. |
syntax |
It can be <length>, <number>, <percentage>, <length-percentage>, <color>, <image>, <url>, <integer>, <angle>, <time>, <resolution>, <transform-function>, or <custom-ident>, or a list of data types and keyword values. + (space-separated) and # (comma-separated) multipliers indicate a list of values is expected, for example, <color># indicates that the expected syntax is a comma-separated list of <color> values. The vertical bar (|) can create an 'or' condition for the expected syntax, for example, <length> | auto accepts <length> or auto, while <color># | <integer># expects a comma-separated list of <color> values or a comma-separated list of <integer> values. |
initial-value | Sets the initial value of the property. |
inherits | true or false. Controls whether the custom property specified by @property is inherited by default. |
Browser support
The numbers in the table represent the browser version that first fully supports the @ rule.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
85 | 85 | 128 | 16.4 | 71 |
Related Pages
Tutorial:CSS @property rule