<p>Uh, what’s @property? It’s a new CSS feature! It gives you superpowers. No joke, there is stuff that @property can do that unlocks things in CSS we’ve never been able to do before. </p> <p>While everything about @property is exciting, perhaps the most interesting thing is that it provides a way to specify a type for custom CSS properties. A type provides more contextual information to the browser, and that results in something cool: We can give the browser the information is needs to transition and animate those properties! </p> <p>But before we get too giddy about this, it’s worth noting that support isn’t quite there. As it current stands at the time of this writing, @property is supported in Chrome and, by extension, Edge. We need to keep an eye on browser support for when we get to use this in other places, like Firefox and Safari. </p> <p>First off, we get type checking </p> <p>@property &#8211;spinAngle {<br /> /* An initial value for our custom property */<br /> initial-value: 0deg;<br /> /* Whether it inherits from parent set values or not */<br /> inherits: false;<br /> /* The type. Yes, the type. You thought TypeScript was cool */<br /> syntax: &#8221;;<br /> } </p> <p>@keyframes spin {<br /> to {<br /> &#8211;spinAngle: 360deg;<br /> }<br /> } </p> <p>That’s right! Type checking in CSS. It’s sorta like creating our very own mini CSS specification. And that’s a simple example. Check out all of the various types we have at our disposal: </p> <p>lengthnumberpercentagelength-percentagecolorimageurlintegerangletimeresolutiontransform-listtransform-functioncustom-ident (a custom identifier string) </p> <p>Before any of this, we may have relied on using “tricks” for powering animations with custom properties. </p> <p>CSS variables are awesome, right? But scope power is often overlooked. For example, take this demo, 3 different animations but only 1 animation defined 💪 That means dynamic animations 😎 https://t.co/VN02NlC4G8 via @CodePen #CSS #animation #webdev #webdesign #coding pic.twitter.com/ig8baxr7F3— Jhey 🐻🛠 (@jh3yy) November 5, 2019 </p> <p>What cool stuff can we do then? Let’s take a look to spark our imaginations. </p> <p>Let’s animate color </p> <p>How might you animate an element either through a series of colors or between them? I’m a big advocate for the HSL color space which breaks things down into fairly understandable numbers: hue, saturation, and lightness, respectively. </p> <p>Animating a hue feels like something fun we can do. What’s colorful? A rainbow! There’s a variety of ways we could make a rainbow. Here’s one: </p> <p>CodePen Embed Fallback </p> <p>In this example, CSS Custom Properties are set on the different bands of the rainbow using :nth-child() to scope them to individual bands. Each band also has an &#8211;index set to help with sizing. </p> <p>To animate those bands, we might use that &#8211;index to set some negative animation delays, but then use the same keyframe animation to cycle through hues. </p> <p>.rainbow__band {<br /> border-color: hsl(var(&#8211;hue, 10), 80%, 50%);<br /> animation: rainbow 2s calc(var(&#8211;index, 0) * -0.2s) infinite linear;<br /> } </p> <p>@keyframes rainbow {<br /> 0%, 100% {<br /> &#8211;hue: 10;<br /> }<br /> 14% {<br /> &#8211;hue: 35;<br /> }<br /> 28% {<br /> &#8211;hue: 55;<br /> }<br /> 42% {<br /> &#8211;hue: 110;<br /> }<br /> 56% {<br /> &#8211;hue: 200;<br /> }<br /> 70% {<br /> &#8211;hue: 230;<br /> }<br /> 84% {<br /> &#8211;hue: 280;<br /> }<br /> } </p> <p>That might work out okay if you want a “stepped” effect. But, those keyframe steps aren’t particularly accurate. I’ve used steps of 14% as a rough jump. </p> <p>CodePen Embed Fallback </p> <p>We could animate the border-color and that would get the job done. But, we’d still have a keyframe step calculation issue. And we need to write a lot of CSS to get this done: </p> <p>@keyframes rainbow {<br /> 0%, 100% {<br /> border-color: hsl(10, 80%, 50%);<br /> }<br /> 14% {<br /> border-color: hsl(35, 80%, 50%);<br /> }<br /> 28% {<br /> border-color: hsl(55, 80%, 50%);<br /> }<br /> 42% {<br /> border-color: hsl(110, 80%, 50%);<br /> }<br /> 56% {<br /> border-color: hsl(200, 80%, 50%);<br /> }<br /> 70% {<br /> border-color: hsl(230, 80%, 50%);<br /> }<br /> 84% {<br /> border-color: hsl(280, 80%, 50%);<br /> }<br /> } </p> <p>Enter @property. Let’s start by defining a custom property for hue. This tells the browser our custom property, &#8211;hue, is going to be a number (not a string that looks like a number): </p> <p>@property &#8211;hue {<br /> initial-value: 0;<br /> inherits: false;<br /> syntax: &#8221;;<br /> } </p> <p>Hue values in HSL can go from 0 to 360. We start with an initial value of 0. The value isn’t going to inherit. And our value, in this case, is a number. The animation is as straightforward as: </p> <p>@keyframes rainbow {<br /> to {<br /> &#8211;hue: 360;<br /> }<br /> } </p> <p>Yep, that’s the ticket: </p> <p>CodePen Embed Fallback </p> <p>To get the starting points accurate, we could play with delays for each band. This gives us some cool flexibility. For example, we can up the animation-duration and we get a slow cycle. Have a play with the speed in this demo. </p> <p>CodePen Embed Fallback </p> <p>It may not be the “wildest” of examples, but I think animating color has some fun opportunities when we use color spaces that make logical use of numbers. Animating through the color wheel before required some trickiness. For example, generating keyframes with a preprocessor, like Stylus: </p> <p>@keyframes party<br /> for $frame in (0..100)<br /> {$frame * 1%}<br /> background &#8216;hsl(%s, 65%, 40%)&#8217; % ($frame * 3.6) </p> <p>We do this purely because this isn’t understood by the browser. It sees going from 0 to 360 on the color wheel as an instant transition because both hsl values show the same color. </p> <p>@keyframes party {<br /> from {<br /> background: hsl(0, 80%, 50%);<br /> }<br /> to {<br /> background: hsl(360, 80%, 50%);<br /> }<br /> } </p> <p>The keyframes are the same, so the browser assumes the animation stays at the same background value when what we actually want is for the browser to go through the entire hue spectrum, starting at one value and ending at that same value after it goes through the motions. </p> <p>Think of all the other opportunities we have here. We can: </p> <p>animate the saturationuse different easingsanimate the lightnessTry rgb()Try degrees in hsl() and declare our custom property type as </p> <p>What’s neat is that we can share that animated value across elements with scoping! Consider this button. The border and shadow animate through the color wheel on hover. </p> <p>CodePen Embed Fallback </p> <p>Animating color leads me think… wow! </p> <p>CodePen Embed Fallback </p> <p>Straight-up numbering </p> <p>Because we can define types for numbers—like integer and number—that means we can also animate numbers instead of using those numbers as part of something else. Carter Li actually wrote an article on this right here on CSS-Tricks. The trick is to use an integer in combination with CSS counters. This is similar to how we can work the counter in “Pure CSS” games like this one. </p> <p>The use of counter and pseudo-elements provides a way to convert a number to a string. Then we can use that string for the content of a pseudo-element. Here are the important bits: </p> <p>@property &#8211;milliseconds {<br /> inherits: false;<br /> initial-value: 0;<br /> syntax: &#8221;;<br /> } </p> <p>.counter {<br /> counter-reset: ms var(&#8211;milliseconds);<br /> animation: count 1s steps(100) infinite;<br /> } </p> <p>.counter:after {<br /> content: counter(ms);<br /> } </p> <p>@keyframes count {<br /> to {<br /> &#8211;milliseconds: 100;<br /> }<br /> } </p> <p>Which gives us something like this. Pretty cool. </p> <p>CodePen Embed Fallback </p> <p>Take that a little further and you’ve got yourself a working stopwatch made with nothing but CSS and HTML. Click the buttons! The rad thing here is that this actually works as a timer. It won’t suffer from drift. In some ways it may be more accurate than the JavaScript solutions we often reach for such as setInterval. Check out this great video from Google Chrome Developer about JavaScript counters. </p> <p>CodePen Embed Fallback </p> <p>What other things could you use animated numbers for? A countdown perhaps? </p> <p>Animated gradients </p> <p>You know the ones, linear, radial, and conic. Ever been in a spot where you wanted to transition or animate the color stops? Well, @property can do that! </p> <p>Consider a gradient where we‘re creating some waves on a beach. Once we’ve layered up some images we could make something like this. </p> <p>body {<br /> background-image:<br /> linear-gradient(transparent 0 calc(35% + (var(&#8211;wave) * 0.5)), var(&#8211;wave-four) calc(75% + var(&#8211;wave)) 100%),<br /> linear-gradient(transparent 0 calc(35% + (var(&#8211;wave) * 0.5)), var(&#8211;wave-three) calc(50% + var(&#8211;wave)) calc(75% + var(&#8211;wave))),<br /> linear-gradient(transparent 0 calc(20% + (var(&#8211;wave) * 0.5)), var(&#8211;wave-two) calc(35% + var(&#8211;wave)) calc(50% + var(&#8211;wave))),<br /> linear-gradient(transparent 0 calc(15% + (var(&#8211;wave) * 0.5)), var(&#8211;wave-one) calc(25% + var(&#8211;wave)) calc(35% + var(&#8211;wave))), var(&#8211;sand);<br /> } </p> <p>There is quite a bit going on there. But, to break it down, we’re creating each color stop with calc(). And in that calculation, we add the value of &#8211;wave. The neat trick here is that when we animate that &#8211;wave value, all the wave layers move. </p> <p>CodePen Embed Fallback </p> <p>This is all the code we needed to make that happen: </p> <p>body {<br /> animation: waves 5s infinite ease-in-out;<br /> }<br /> @keyframes waves {<br /> 50% {<br /> &#8211;wave: 25%;<br /> }<br /> } </p> <p>Without the use of @property, our waves would step between high and low tide. But, with it, we get a nice chilled effect like this. </p> <p>CodePen Embed Fallback </p> <p>It’s exciting to think other neat opportunities that we get when manipulating images. Like rotation. Or how about animating the angle of a conic-gradient… but, within a border-image. Bramus Van Damme does a brilliant job covering this concept. </p> <p>Let’s break it down by creating a charging indicator. We’re going to animate an angle and a hue at the same time. We can start with two custom properties: </p> <p>@property &#8211;angle {<br /> initial-value: 0deg;<br /> inherits: false;<br /> syntax: &#8221;;<br /> } </p> <p>@property &#8211;hue {<br /> initial-value: 0;<br /> inherits: false;<br /> syntax: &#8221;;<br /> } </p> <p>The animation will update the angle and hue with a slight pause on each iteration. </p> <p>@keyframes load {<br /> 0%, 10% {<br /> &#8211;angle: 0deg;<br /> &#8211;hue: 0;<br /> }<br /> 100% {<br /> &#8211;angle: 360deg;<br /> &#8211;hue: 100;<br /> }<br /> } </p> <p>Now let’s apply it as the border-image of an element. </p> <p>.loader {<br /> &#8211;charge: hsl(var(&#8211;hue), 80%, 50%);<br /> border-image: conic-gradient(var(&#8211;charge) var(&#8211;angle), transparent calc(var(&#8211;angle) * 0.5deg)) 30;<br /> animation: load 2s infinite ease-in-out;<br /> } </p> <p>Pretty cool. </p> <p>CodePen Embed Fallback </p> <p>Unfortunately, border-image doesn‘t play nice with border-radius. But, we could use a pseudo-element behind it. Combine it with the number animation tricks from before and we’ve got a full charging/loading animation. (Yep, it changes when it gets to 100%.) </p> <p>CodePen Embed Fallback </p> <p>Transforms are cool, too </p> <p>One issue with animating transforms is transitioning between certain parts. It often ends up breaking or not looking how it should. Consider the classic example of a ball being throw. We want it to go from point A to point B while imitating the effect of gravity. </p> <p>An initial attempt might look like this </p> <p>@keyframes throw {<br /> 0% {<br /> transform: translate(-500%, 0);<br /> }<br /> 50% {<br /> transform: translate(0, -250%);<br /> }<br /> 100% {<br /> transform: translate(500%, 0);<br /> }<br /> } </p> <p>But, we’ll soon see that it doesn’t look anything like we want. </p> <p>CodePen Embed Fallback </p> <p>Before, we may have reached for wrapper elements and animated them in isolation. But, with @property, we can animate the individual values of the transform. And all on one timeline. Let’s flip the way this works by defining custom properties and then setting a transform on the ball. </p> <p>@property &#8211;x {<br /> inherits: false;<br /> initial-value: 0%;<br /> syntax: &#8221;;<br /> } </p> <p>@property &#8211;y {<br /> inherits: false;<br /> initial-value: 0%;<br /> syntax: &#8221;;<br /> } </p> <p>@property &#8211;rotate {<br /> inherits: false;<br /> initial-value: 0deg;<br /> syntax: &#8221;;<br /> } </p> <p>.ball {<br /> animation: throw 1s infinite alternate ease-in-out;<br /> transform: translateX(var(&#8211;x)) translateY(var(&#8211;y)) rotate(var(&#8211;rotate));<br /> } </p> <p>Now for our animation, we can compose the transform we want against the keyframes: </p> <p>@keyframes throw {<br /> 0% {<br /> &#8211;x: -500%;<br /> &#8211;rotate: 0deg;<br /> }<br /> 50% {<br /> &#8211;y: -250%;<br /> }<br /> 100% {<br /> &#8211;x: 500%;<br /> &#8211;rotate: 360deg;<br /> }<br /> } </p> <p>The result? The curved path we had hoped for. And we can make that look different depending on the different timing functions we use. We could split the animation into three ways and use different timing functions. That would give us different results for the way the ball moves. </p> <p>CodePen Embed Fallback </p> <p>Consider another example where we have a car that we want to drive around a square with rounded corners. </p> <p>CodePen Embed Fallback </p> <p>We can use a similar approach to what we did with the ball: </p> <p>@property &#8211;x {<br /> inherits: false;<br /> initial-value: -22.5;<br /> syntax: &#8221;;<br /> } </p> <p>@property &#8211;y {<br /> inherits: false;<br /> initial-value: 0;<br /> syntax: &#8221;;<br /> } </p> <p>@property &#8211;r {<br /> inherits: false;<br /> initial-value: 0deg;<br /> syntax: &#8221;;<br /> } </p> <p>The car’s transform is using calculated with vmin to keep things responsive: </p> <p>.car {<br /> transform: translate(calc(var(&#8211;x) * 1vmin), calc(var(&#8211;y) * 1vmin)) rotate(var(&#8211;r));<br /> } </p> <p>Now can write an extremely accurate frame-by-frame journey for the car. We could start with the value of &#8211;x. </p> <p>@keyframes journey {<br /> 0%, 100% {<br /> &#8211;x: -22.5;<br /> }<br /> 25% {<br /> &#8211;x: 0;<br /> }<br /> 50% {<br /> &#8211;x: 22.5;<br /> }<br /> 75% {<br /> &#8211;x: 0;<br /> }<br /> } </p> <p>The car makes the right journey on the x-axis. </p> <p>CodePen Embed Fallback </p> <p>Then we build upon that by adding the travel for the y-axis: </p> <p>@keyframes journey {<br /> 0%, 100% {<br /> &#8211;x: -22.5;<br /> &#8211;y: 0;<br /> }<br /> 25% {<br /> &#8211;x: 0;<br /> &#8211;y: -22.5;<br /> }<br /> 50% {<br /> &#8211;x: 22.5;<br /> &#8211;y: 0;<br /> }<br /> 75% {<br /> &#8211;x: 0;<br /> &#8211;y: 22.5;<br /> }<br /> } </p> <p>Well, that’s not quite right. </p> <p>CodePen Embed Fallback </p> <p>Let’s drop some extra steps into our @keyframes to smooth things out: </p> <p>@keyframes journey {<br /> 0%, 100% {<br /> &#8211;x: -22.5;<br /> &#8211;y: 0;<br /> }<br /> 12.5% {<br /> &#8211;x: -22.5;<br /> &#8211;y: -22.5;<br /> }<br /> 25% {<br /> &#8211;x: 0;<br /> &#8211;y: -22.5;<br /> }<br /> 37.5% {<br /> &#8211;y: -22.5;<br /> &#8211;x: 22.5;<br /> }<br /> 50% {<br /> &#8211;x: 22.5;<br /> &#8211;y: 0;<br /> }<br /> 62.5% {<br /> &#8211;x: 22.5;<br /> &#8211;y: 22.5;<br /> }<br /> 75% {<br /> &#8211;x: 0;<br /> &#8211;y: 22.5;<br /> }<br /> 87.5% {<br /> &#8211;x: -22.5;<br /> &#8211;y: 22.5;<br /> }<br /> } </p> <p>Ah, much better now: </p> <p>CodePen Embed Fallback </p> <p>All that‘s left is the car‘s rotation. We‘re going with a 5% window around the corners. It’s not precise but it definitely shows the potential of what’s possible: </p> <p>@keyframes journey {<br /> 0% {<br /> &#8211;x: -22.5;<br /> &#8211;y: 0;<br /> &#8211;r: 0deg;<br /> }<br /> 10% {<br /> &#8211;r: 0deg;<br /> }<br /> 12.5% {<br /> &#8211;x: -22.5;<br /> &#8211;y: -22.5;<br /> }<br /> 15% {<br /> &#8211;r: 90deg;<br /> }<br /> 25% {<br /> &#8211;x: 0;<br /> &#8211;y: -22.5;<br /> }<br /> 35% {<br /> &#8211;r: 90deg;<br /> }<br /> 37.5% {<br /> &#8211;y: -22.5;<br /> &#8211;x: 22.5;<br /> }<br /> 40% {<br /> &#8211;r: 180deg;<br /> }<br /> 50% {<br /> &#8211;x: 22.5;<br /> &#8211;y: 0;<br /> }<br /> 60% {<br /> &#8211;r: 180deg;<br /> }<br /> 62.5% {<br /> &#8211;x: 22.5;<br /> &#8211;y: 22.5;<br /> }<br /> 65% {<br /> &#8211;r: 270deg;<br /> }<br /> 75% {<br /> &#8211;x: 0;<br /> &#8211;y: 22.5;<br /> }<br /> 85% {<br /> &#8211;r: 270deg;<br /> }<br /> 87.5% {<br /> &#8211;x: -22.5;<br /> &#8211;y: 22.5;<br /> }<br /> 90% {<br /> &#8211;r: 360deg;<br /> }<br /> 100% {<br /> &#8211;x: -22.5;<br /> &#8211;y: 0;<br /> &#8211;r: 360deg;<br /> }<br /> } </p> <p>And there we have it, a car driving around a curved square! No wrappers, no need for complex Math. And we composed it all with custom properties. </p> <p>CodePen Embed Fallback </p> <p>Powering an entire scene with variables </p> <p>We‘ve seen some pretty neat @property possibilities so far, but putting everything we’ve looked at here together can take things to another level. For example, we can power entire scenes with just a few custom properties. </p> <p>Consider the following concept for a 404 page. Two registered properties power the different moving parts. We have a moving gradient that’s clipped with -webkit-background-clip. The shadow moves by reading the values of the properties. And we swing another element for the light effect. </p> <p>CodePen Embed Fallback </p> <p>That’s it! </p> <p>It’s exciting to think about what types of things we can do with the ability to define types with @property. By giving the browser additional context about a custom property, we can go nuts in ways we couldn’t before with basic strings. </p> <p>What ideas do you have for the other types? Time and resolution would make for interesting transitions, though I’ll admit I wasn’t able to make them work that way I was hoping. url could also be neat, like perhaps transitioning between a range of sources the way an image carousel typically does. Just brainstorming here! </p> <p>I hope this quick look at @property inspires you to go check it out and make your own awesome demos! I look forward to seeing what you make. In fact, please share them with me here in the comments! </p> <p>The post Exploring @property and its Animating Powers appeared first on CSS-Tricks.<br /> You can support CSS-Tricks by being an MVP Supporter.</p>

Breakdown

Uh, what’s @property? It’s a new CSS feature! It gives you superpowers. No joke, there is stuff that @property can do that unlocks things in CSS we’ve never been able to do before.

While everything about @property is exciting, perhaps

...