CSS @property or :root?

Could/Should I use @property indistinctly from declaring CSS variables using :root? Is there any downside to using only @property? Thanks in advance! 👍
39 Replies
ἔρως
ἔρως4d ago
both have different uses and different support with @property, you can drill a bit deeper and set a syntax and a type for the custom property you can't do that with variables
Pisandelli
PisandelliOP3d ago
Got it. So there's no such thing like "It's better to use @property for 'this kind of scenario' and :root for 'that kind'...", right? The choice is totally up to me. If I want more control over my variables @property is a good option... if I wanna something more simple, :root is fine. At the end the result will be the same. Thanks @ἔρως
ἔρως
ἔρως3d ago
there are things you cant do with just --x
Pisandelli
PisandelliOP3d ago
yeah I'm aware
ἔρως
ἔρως3d ago
thats just the difference
Pisandelli
PisandelliOP3d ago
My concern was just if I can use @property the same way I used :root
ἔρως
ἔρως3d ago
by the way, if you add a variable to :root, you should also add to ::backdrop as well you can use both
Pisandelli
PisandelliOP3d ago
true
ἔρως
ἔρως3d ago
you can do :root { --x: 5; } and have a @property for the same --x
Pisandelli
PisandelliOP3d ago
No problem using a global CSS using only @property instead of :root...
ἔρως
ἔρως3d ago
nope, you dont even need the @property if you set a default value
Pisandelli
PisandelliOP3d ago
And using @property I can animate gradient as well.. So I have good features
ἔρως
ἔρως3d ago
i think you can without it too but never tried to animate gradients
Pisandelli
PisandelliOP3d ago
I'm building a Design token parser (it will be part of another project). My idea was create a defaults.css with the CSS variables. These variables would be used for theme the app. I used to create this file using root.
:root {
--color-primary: blue;
--color-secondary: red;
}
:root {
--color-primary: blue;
--color-secondary: red;
}
Now I was wondering if I can create the same thing with more deep control using @property
@property --color-primary {
syntax: '<color>';
inherits: false;
initial-value: blue;
}


@property --color-secondary {
syntax: '<color>';
inherits: false;
initial-value: red;
}
@property --color-primary {
syntax: '<color>';
inherits: false;
initial-value: blue;
}


@property --color-secondary {
syntax: '<color>';
inherits: false;
initial-value: red;
}
ἔρως
ἔρως3d ago
you can, but reading that will require a state machine, while the first version wont which backend language are you using to generate the css files? also, why are you trying to read from a css file?
Pisandelli
PisandelliOP3d ago
I'm using JS and wanna create CSS file for a web app theme. The thing is change the theme using design tokens. Drop the token in a folder, and during the app building we read the token and generate the theme. I always used :root... but with @property I can have more control over properties types, etc... It was not clear to me if I can use it instead of :root without having any downside
ἔρως
ἔρως3d ago
the only downside is the browser support
Pisandelli
PisandelliOP3d ago
hum
ἔρως
ἔρως3d ago
if im not mistaken, some devices still supported by apple may have an ios version that is too old
Pisandelli
PisandelliOP3d ago
Modern browsers only
ἔρως
ἔρως3d ago
people skip updating ios because it tends to absolutely butcher the performance of older devices and the first versions tend to be really buggy
Pisandelli
PisandelliOP3d ago
Oh I see This is trully something to be on radar
ἔρως
ἔρως3d ago
firefox also only got support for it in july
Pisandelli
PisandelliOP3d ago
Well I think I'll be doing more tests!! Thanks @ἔρως
ἔρως
ἔρως3d ago
you should test with firefox esr
Pisandelli
PisandelliOP3d ago
I use Firefox Chrome is not my default
ἔρως
ἔρως3d ago
that's still on 126 while the @property came in 128
Pisandelli
PisandelliOP3d ago
huuum
ἔρως
ἔρως3d ago
esr is different from the main line version
Pisandelli
PisandelliOP3d ago
good point
ἔρως
ἔρως3d ago
esr is the extended support release, which gets updates for a long time and im not sure if it gets feature updates if i were you, i would just have the variables in a json file, and then you generate the css from it
Pisandelli
PisandelliOP3d ago
Firefox is on v134
ἔρως
ἔρως3d ago
and the theme could have a json file or a special comment or variable with the json embed, if you dont like separated files thats the main release, not the esr release
Pisandelli
PisandelliOP3d ago
yes is a good idea
ἔρως
ἔρως3d ago
it's a lot easier too you can just do import 'file.json' as theme; and it should work
Pisandelli
PisandelliOP3d ago
yep!
ἔρως
ἔρως3d ago
or you have to use import() or something i know it works amazingly
Pisandelli
PisandelliOP3d ago
I'll give it a try Thakns for the tip!
ἔρως
ἔρως3d ago
you're welcome by the way, store a version indicator in the file you will thank me for that

Did you find this page helpful?