CSS @property Feature

css property feature guide
10 November 2024

In the world of CSS, new features are continuously added to improve the developer experience and enhance the capabilities of custom properties applied to styles. One of these attractive and expanded tools for designers is the @property feature. At first glance, it may seem that this feature is complex, but if we take a closer look, we will see how it can be utilized effectively.

Let’s begin by discussing the role of @property. This feature allows you to define how a CSS variable (which is also known as CSS Custom Properties or CSS Variables) behaves. This means you can specify a CSS variable's parameters and declare what type of values it can accept.

When you specify a CSS variable, you can ensure that the styles and animations used on web pages operate correctly and without disruption. Without using @property, a minor mistake in the way variables are utilized can lead to significant problems in rendering pages.

In addition, @property is a powerful tool for controlling the validation of inputs and ensuring that variables not only conform to the definitions but also are well-managed. By familiarizing yourself with its basic principles, you’ll want to consider how its implementation would look.

<style>
@property --my-color {
  syntax: '<color>';
  initial-value: black;
  inherits: false;
}

div {
  color: var(--my-color);
}
</style>


Line-by-Line Explanation of the Code Example


<style>
Starts the style section where we will use the @property feature.

@property --my-color
We define a new variable called --my-color that we will use in our styles.

syntax: '<color>';
Specifies that the type of data for this variable is a CSS color.

initial-value: black;
The default value of this variable is set to black.

inherits: false;
This specifies that the variable will not inherit and is only local.

color: var(--my-color);
Here, we use the variable --my-color to set the color of the text in the div.

</style>
Ends the style block.

FAQ

?

What is the purpose of the @property feature?

?

Can all browsers support @property?

?

How can I use @property in real projects?