DEV Community

Cover image for Advanced CSS Animation Using cubic-bezier()
Temani Afif for This is Learning

Posted on • Updated on • Originally published at css-tricks.com

Advanced CSS Animation Using cubic-bezier()

When dealing with complex CSS animations, there is a tendency to create expansive @keyframes with lots of declarations. There are a couple of tricks though that I want to talk about that might help make things easier, while staying in vanilla CSS:

  1. Multiple animations
  2. Timing functions

The first one is more widely used and familiar but the second one is less common. There could be good reasons for that — chaining animations with commas is relatively easier than grokking the various timing functions that are available to us and what they do. There's one especially neat timing function that gives us total control to create custom timing functions. That would be cubic-bezier() and in this post I will show you the power of it and how it can be used to create fancy animation without too much complexity.

Let's start with a basic example showing how we can move a ball around in interesting directions, like an infinity (∞) shape:

As you can see, there is no complex code — only two keyframes and a "strange" cubic-bezier() function. And yet, a pretty complex-looking final infinity-shape animation is what we get.

Cool, right? Let's dig into this!

The cubic-bezier() function

Let's start with the official definition:

A cubic Bézier easing function is a type of easing function defined by four real numbers that specify the two control points, P1 and P2, of a cubic Bézier curve whose end points P0 and P3 are fixed at (0, 0) and (1, 1) respectively. The x coordinates of P1 and P2 are restricted to the range [0, 1].

cubic bezier

The above curve defines how the output (y-axis) will behave based on the time (x-axis). Each axis has a range of [0,1][0, 1] (or [0%,100%][0\%, 100\%] ). If we have an animation that lasts two-second ( 2s2s ), then:

0(0%)=0s1(100%)=2s 0 (0\%) = 0s \newline 1 (100\%) = 2s

If we want to animate left from 5px to 20px, then:

0(0%)=5px1(100%)=20px 0 (0\%) = 5px \newline 1 (100\%) = 20px

X, the time, is always restricted to [0,1][0, 1] ; however, Y, the output, can go beyond [0,1][0, 1] .

My goal is to adjust P1 and P2 in order to create the following curves:

CSS cubic bezier

You may think this is impossible to achieve because, as stated in the definition, P0P_0 and P3P_3 are fixed at (0,0)(0,0) and (1,1)(1,1) meaning they cannot be on the same axis. That's true, and we will use some math tricks to "approximate" them.


Parabolic curve

Let's start with the following definition: cubic-bezier(0,1.5,1,1.5). That gives us the following curve:

cubic-bezier(0,1.5,1,1.5)

Our goal is to move (1,1)(1,1) and make it at (0,1)(0,1) which isn’t technically possible. So we will try to fake it.

We previously said that our range is [0,1][0, 1] (or [0%,100%][0\%, 100\%] ) so let's imagine the case when 0%0\% is very close to 100%100\% . If, for example, we want to animate top from 20px20px ( 0%0\% ) to 20.1px20.1px ( 100%100\% ) then we can say that both the initial and final states are equal.

Hm, but our element will not move at all, right?

Well, it will move a little because the Y value exceeds 20.1px20.1px ( 100%100\% ). But that's not enough to give us perceptible movement:

Let's update the curve and use cubic-bezier(0,4,1,4) instead. Notice how our curve is way taller than before:

Curve Output

But yet, still no movement — even if the top value is crossing 33 (or 300%300\% ). Let's try cubic-bezier(0,20,1,20):

Curve Output

Yes! it started to move a little. Did you notice the evolution of the curve each time we increase the value? It's making our point (1,1)(1,1) "visually" closer to (0,1)(0,1) when we zoom out to see the full curve and this is the trick.

By using cubic-bezier(0,V,1,V) where VV is some very big value and both the initial and final states are very close together (or almost equal), we can simulate the parabolic curve.

An example is worth a thousand words:

I applied the "magic" cubic-bezier function in there to the top animation, plus a linear one applied to left. This gives us the curve we want.

Digging into the math

For those of you math-minded folks out there, we can break that explanation down further. A cubic bezier can be defined using the following formula:

P=(1t)3P0+3(1t)2tP1+3(1t)t2P2+t3P3 P = (1−t)^3P_0 + 3(1−t)^2tP_1 + 3(1−t)t^2P_2 + t^3P_3

Each point is defined as follows:

P0=(0,0),P1=(0,V)P2=(1,V),P3=(1,1) P_0 = (0,0), P_1 = (0,V) \newline P_2 = (1,V), P_3 = (1,1)

This gives us the two functions for x and y coordinates:

X(t)=3(1t)t2+t3=3t22t3 X(t) = 3(1−t)t^2 + t^3 = 3t^2 - 2t^3

Y(t)=3(1t)2tV+3(1t)t2V+t3=t33Vt2+3Vt Y(t) = 3(1−t)^2tV +3(1−t)t^2V + t^3 = t^3 - 3Vt^2 + 3Vt

VV is our big value and tt is within the range [0,1][0, 1] . If we consider our previous example, Y(t)Y(t) will give us the value of top while X(t)X(t) is the time progress. The points (X(t),Y(t))(X(t),Y(t)) will then define our curve.

Let's find the maximum value of Y(t)Y(t) . For this, we need to find the value of tt that will give us Y(t)=0Y'(t) = 0 (when the derivative is equal to 00 ):

Y(t)=3t26Vt+3V Y'(t) = 3t^2 - 6Vt + 3V

Y(t)=0Y'(t) = 0 is a quadratic equation. I will skip the boring part and will give you the result, which is

t=VV2V t = V - \sqrt{V^2 - V}

When VV is a large value, tt will be equal to 0.50.5 . So, Y(0.5)=MaxY(0.5) = Max and X(0.5)X(0.5) will be equal to 0.50.5 . That means we reach the maximum value at the halfway point in the animation, which conforms to the parabolic curve we want.

Also, Y(0.5)Y(0.5) will give us 1+6V8\frac{1 + 6V}{8} and this will allow us to find the max value based on VV . And since we will always use a big value for VV , we can simplify to 6V8=0.75V\frac{6V}{8} = 0.75V .

We used V=500V = 500 in the last example, so the max value there would come out to 375375 (or 37500%37500\% ) and we get the following:

  • Initial state ( 00 ): top: 200px
  • Final state ( 11 ): top: 199.5px

There's a difference of 0.5px-0.5px between 00 and 11 . Let's call it the increment. For 375375 (or 3750037500% ) we have an equation of 3750.5px=187.5px375*-0.5px = -187.5px . Our animated element is reaching top: 12.5px ( 200px187.5px200px - 187.5px ) and gives us the following animation:

top: 200px (at 0% of the time ) → top: 12.5px (at 50% of the time) → top: 199.5px (at 100% of the time) 
Enter fullscreen mode Exit fullscreen mode

Or, expressed another way:

top: 200px (at 0%) → top: 12.5px (at 50%) → top: 200px (at 100%)
Enter fullscreen mode Exit fullscreen mode

Let's do the opposite logic. What value of VV should we use to make our element reach top: 0px? The animation will be 200px → 0px → 199.5px, so we need 200px-200px to reach 0px0px . Our increment is always equal to 0.5px-0.5px . The max value will be equal to 2000.5=400\frac{200}{0.5} = 400 , so 0.75V=4000.75V = 400 which means V=533.33V = 533.33 .

Our element is touching the top!

Here is a figure that sums up that math we just did:

CSS Parobilic curve


Sinusoidal curve

We will use almost the exact same trick to create a sinusoidal curve but with a different formula. This time we will use cubic-bezier(0.5,V,0.5,-V)

Like we did before, let's see how the curve will evolve when we increase the value:

CSS sinusoidal curve

I think you probably get the idea by now. Using a big value for VV gets us close to a sinusoidal curve.

Here's another one with a continuous animation — a real sinusoidal animation!

The math

Let's get in the math for this one! Folllowing the same formula as before, we will get the following functions:

X(t)=32(1t)2t+32(1t)t2+t3=32t32t3+t3 X(t) = \frac{3}{2}(1−t)^2t + \frac{3}{2}(1−t)t^2 + t^3 = \frac{3}{2}t - \frac{3}{2}t^3 + t^3
Y(t)=3(1t)2tV3(1t)t2V+t3=(6V+1)t39Vt2+3Vt Y(t) = 3(1−t)^2tV - 3(1−t)t^2V + t^3 = (6V + 1)t^3 - 9Vt^2 + 3Vt

This time we need to find the minimum and maximum values for Y(t)Y(t) . Y(t)=0Y'(t) = 0 will give us two solutions. After solving for this:

Y(t)=3(6V+1)t218Vt+3V=0 Y'(t) = 3(6V + 1)t^2 - 18Vt + 3V = 0

…we get:

t=3V+3V²V6V+1,t=3V3V²V6V+1 t' = \frac{3V + \sqrt{3V² - V}}{6V + 1}, t'' = \frac{3V - \sqrt{3V² - V}}{6V + 1}

For a big value of VV , we have t=0.211t'=0.211 and t=0.789t''=0.789 . That means that Y(0.211)=MaxY(0.211) = Max and Y(0.789)=MinY(0.789) = Min . That also means that X(0.211)=0.26X(0.211)= 0.26 and X(0.789)=0.74X(0.789) = 0.74 . In other words, we reach the Max at 26%26\% of the time and Min at 74%74\% of the time.

Y(0.211)Y(0.211) is equal to 0.289V0.289V and Y(0.789)Y(0.789) to 0.289V-0.289V . We got those values with some rounding considering that VV is very big.

Our sinusoidal curve should also cross the x-axis (or Y(t)=0Y(t) = 0 ) at half the time (or X(t)=0.5X(t) = 0.5 ). In order to prove this, we use the second derivate of Y(t)Y(t) — which should be equal to 00 — so Y(t)=0Y''(t) = 0 .

Y(t)=6(6V+1)t18V=0 Y''(t) = 6(6V + 1)t - 18V = 0

The solution is 3V6V+1\frac{3V}{6V + 1} , and for a big VV value, the solution is 0.50.5 . That give us Y(0.5)=0Y(0.5) = 0 and X(0.5)=0.5X(0.5) = 0.5 which confirms that our curve crosses the (0.5,0)(0.5,0) point.

Now let's consider the previous example and try to find the value of VV that gets us back to top: 0%. We have:

  • Initial state ( 00 ): top: 50%
  • Final state ( 11 ): top: 49.9%
  • Increment: 0.1%-0.1\%

We need 50%-50\% to reach top: 0%, so 0.289V0.1%=50%0.289V*-0.1\% = -50\% which gives us V=1730.10V = 1730.10 .

As you can see, our element is touching the top and disappearing at the bottom because we have the following animation:

top: 50% → top: 0% → top: 50% → top: 100% → top: 50% → and so on ... 
Enter fullscreen mode Exit fullscreen mode

A figure to sum up the calculation:

CSS sinusoidal curve

And an example to illustrate all curves together:

Yes, you see four curves! If you look closely, you will notice that I am using two different animations, one going to 49.9%49.9\% (an increment of 0.01%-0.01\% ) and another going to 50.1%50.1\% (an increment of +0.01%+0.01\% ). By changing the sign of the increment, we control the direction of the curve. We can also control the other parameters of the cubic bezier (not the VV one that should remain a big value) to create more variations from the same curves.

And below, an interactive demo:


Getting back to our example

Let's get back to our initial example of a ball moving around in the shape of an infinity symbol. I simply combined two sinusoidal animations to make it work.

If we combine what we did previously with the concept of multiple animations, we can get astonishing results. Here again is the initial example, this time as an interactive demo. Change the values and see the magic:

Let's go further and add a little CSS Houdini to the mix. We can animate a complex transform declaration thanks to @property (but CSS Houdini is limited to Chrome and Edge support at the moment).

What kind of drawings can you make with that? Here is a few that I was able to make:

CSS alien drawing

And here is a spirograph animation:

And a version without CSS Houdini:

There's a few things to take away from these examples:

  • Each keyframe is defined using only one declaration that contain the increment.
  • The position of the element and the animation are independent. We can easily place the element anywhere without the need to adjust the animation.
  • We made no calculations. There isn't a ton of angles or pixel values. We only need a tiny value within the keyframe and a big value within the cubic-bezier() function.
  • The whole animation can be controlled just by adjusting the duration value.

What about transition?

The same technique can also be used with the CSS transition property since it follows the same logic when it comes to timing functions. This is great because we're able to avoid keyframes when creating some complex hover effect.

Here's what I made without keyframes. If you were following me you will remember that they are a part of my underline/overlay animation collection 😉

Mario is jumping thanks to the parabolic curve. We needed no keyframes at all to create that shake animation on hover. The sinusoidal curve is perfectly capable of doing all the work.

Here is another version of Mario, this time using CSS Houdini. And, yep, he's still jumping thanks to the parabolic curve:

For good measure, here are more fancy hover effects without keyframes (again, Chrome and Edge only). Spoiler for my next collection 😜


That's it!

Now you have some magic cubic-bezier() curves and the math behind them. The benefit, of course, is that custom timing functions like this let us do fancy animations without the complex keyframes we generally reach for.

I understand that not everyone is math-minded and that’s okay. There are tools to help, like Matthew Lein's Ceaser, which lets you drag the curve points around to get what you need. And, if you don't already have it bookmarked, cubic-bezier.com is another one. If you want to play with cubic-bezier outside the CSS world, I recommend desmos where you can see some math formulas.

Regardless of how you get your cubic-bezier() values, hopefully now you have a sense of their powers and how they can help make for nicer code in the process.

Top comments (17)

Collapse
 
icecoffee profile image
Atulit Anand

Lol to all those people who ones assumed, front end don't require any math.
haha

Collapse
 
afif profile image
Temani Afif

Front end require math and geometry ;) (like I am doing here: dev.to/afif/fully-responsive-css-f... and here: dev.to/afif/responsive-hexagon-gri...) and more Math-related posts are coming ;)

Collapse
 
icecoffee profile image
Atulit Anand

Looking forward to it

Collapse
 
icecoffee profile image
Atulit Anand

State of the art work.

Collapse
 
thormeier profile image
Pascal Thormeier

What a deep-dive, awesome article! Thank you for that one!

If I may add a suggestion: to make the maths part a bit more readable, the editor allows for KaTeX using liquid tags, so you can format the formulas in well-known notations, such as fractions, subscripts, superscripts, sum signs (capital sigma), etc.

Collapse
 
afif profile image
Temani Afif

thanks for the tip, now the article looks a lot better 👍

Collapse
 
thormeier profile image
Pascal Thormeier

Love it, thank you again for the great article 😀

Collapse
 
afif profile image
Temani Afif

Oh we have math here, thanks god! will edit right now ... it wasn't the case on CSS-tricks 😔

Collapse
 
madza profile image
Madza • Edited

Are you aware of 3Blue1Brown?
One of my fav YouTubers on awesome topics like these 👌💯

Collapse
 
afif profile image
Temani Afif

not really, my engineer days are behind me now but might be good to refresh my memory 😉

Collapse
 
madza profile image
Madza

Also the opensource animation engine he uses manim 🔥😉

Collapse
 
grahamthedev profile image
GrahamTheDev

Another great article!

Just commenting for the algorithm 😜

Collapse
 
gusgonnet profile image
Gustavo

wow, who says that advanced CSS tricks are not rocket science.
thank you for the deep dive
Gustavo.

Collapse
 
nikhil27b profile image
Nikhil Bobade

Great Article bro

Collapse
 
__manucodes profile image
manu

Nice math :D .
haha usually I use cubic-bezier.com because it takes much less time.

Yes, I also know desmos.. I use it for my assignments ;)

Collapse
 
arsalan0974 profile image
arsalan0974

Awesome very nice

Collapse
 
goodnewsdaniel profile image
Goodnews Daniel

This article makes me feel I don't know any CSS at all! It's scary :))