All major browsers except Firefox (coming soon!) support the @property at-rule. It enables you to do things you previously couldn't, like animating custom properties.
Let's say you have two animations. One fades an element; the other moves it. Based on their motion preference, users see one or the other.
.banner {
--animation: fade;
animation: var(--animation) 3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}
@media(prefers-reduced-motion: no-preference) {
.banner {
--animation: move;
}
}
@keyframes move {
from { translate: 100vw; }
to { translate: 0; }
}
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
That works nice and well, but if you use custom properties in your keyframe animations instead of redeclaring regular properties, it doesn't work anymore because you're trying to animate string values.
.banner {
--animation: fade;
translate: var(--position) 0;
opacity: var(--opacity);
animation: var(--animation) 3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}
@media(prefers-reduced-motion: no-preference) {
.banner {
--animation: move;
}
}
@keyframes move {
from { --position: 100vw; }
to { --position: 0; }
}
@keyframes fade {
from { --opacity: 0; }
to { --opacity: 1; }
}
With the @property at-rule you can turn the two string values into a <length> and a <number>, which makes them animatable.
@property --position {
syntax: "<length>";
inherits: false;
initial-value: 0;
}
@property --opacity {
syntax: "<number>";
inherits: false;
initial-value: 1;
}
@keyframes move {
from { --position: 100vw; }
to { --position: 0; }
}
@keyframes fade {
from { --opacity: 0; }
to { --opacity: 1; }
}
My blog doesn't support comments yet, but you can reply via blog@matuzo.at.





