CSS: typesafe variables using @property

29 Jul 2024

CSS @property allows developers to define variables with type checking and constraining, initial value and inherit rules.

@property --custom-variable {
  syntax: '<color>';
  inherits: false;
  initial-value: #000;
}

If we define a CSS variable without @propery, browser developer tools won’t give any feedback for the wrong values,

normal variable with error has no indicator in devtools

instead, if we use @property like

@property --container-border-color {
  syntax: '<color>';  
  inherits: true;  
  initial-value: #000;
}

and if we provide a wrong value dev tools can help us with proper error indicators

@property shows proper error in devtools

Hovering on the variables on devtools will give us more context as well.

at property shows more context in devtools

Hope that was helpful.
Thank You.

If you find my work helpful, You can buy me a coffee.