An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
November 19, 2021 04:38 pm GMT
Original Link: https://dev.to/sowg/the-css-property-f07
The CSS @property
Have you every tried to animate a CSS custom variable and ended up getting this result:
Well then I got Good news for you there is a way to animate CSS Custom Properties!
It can be done with the CSS @property
.
I will show you how to use it with a simple example.
CSS WAY
1) Declare it(Its pretty hard but you will get he hang of it):
@property --c { syntax: '<color>'; inherits: false; initial-value: #f00;}
2) Use it in a property and add css animation property:
div { /*Some Styles*/ background: var(--c); animation: c 2s ease infinite alternate;}
3) Now create an animation with it:
@keyframes c { to { --c: pink } }
End Result:
JS Way
The CSS and JS way are pretty much the same except you declare the @property
in JS instead of CSS.
How you Declare it in JS:
window.CSS.registerProperty({ name: '--c', syntax: '<color>', inherits: true, initialValue: 'red'})
Example of the JS way:
That's it, Thanks for reading!
Original Link: https://dev.to/sowg/the-css-property-f07
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To