Tuesday April 27, 2021 By David Quintanilla
Understanding Easing Functions For CSS Animations And Transitions — Smashing Magazine

About The Creator

Adrian Bece is a flexible fullstack internet developer with intensive eCommerce expertise who’s at the moment working at PROTOTYP as a technical lead. He enjoys …
More about

Easing features can change the appear and feel of an animation by affecting the speed, or pace, of an animation. As human beings, we’re accustomed to a pure, non-linear movement. Utilizing these custom-easing features within the animations can result in an improved impression on customers and a extra pleasant consumer expertise. On this article, we’re going to take a deep dive into easing features and see how we are able to use them to create these pure and gorgeous animations.

Have you ever ever seen how clean and pleasant animations look on a well-made, skilled challenge? I’m reminded of the In Pieces web site the place animations are used not only for ornament, however additionally they convey the message in regards to the endangered species in an impactful method. Not solely is the animation design and magnificence stunning, however additionally they circulation properly and harmoniously. It’s exactly that circulation together with the design and presentation which makes the animation look gorgeous and pure. That’s the energy of easing features, that are additionally known as timing features.

When writing transition and animation properties in CSS, we normally go for the pre-defined easing features like ease-out as a result of it’s easy, they give the impression of being alright they usually work effectively for many instances. Nonetheless, having a dozen or extra parts on a web page with an animation that options the identical period and easing perform values could make the UI a bit bland and monotone. People reply higher to pure movement, so making the animation and transition extra various and pure will end in a greater consumer expertise.

If we return to the In Pieces instance and examine the stylesheet, we are able to discover that varied {custom} cubic-bezier easing features are utilized in mixture with the pre-defined linear and ease-in timing features to attain that lovely animation circulation. Animations wouldn’t look nearly as good if solely pre-defined easing features had been used as an alternative of {custom} easing features. Following video showcases In Items web site with all easing features set to ease-out. Regardless that there’s nothing improper with the animations, discover how they don’t seem to be as gorgeous nor thrilling as the unique animations.

On this article, we’re going to take a deep dive into CSS easing features, forms of easing features, and tips on how to create {custom} easing features utilizing Cubic Bézier curves.

Below The Hood

With a view to get a greater understanding of easing features, we have to take a step again and try the animation fundamentals in CSS.

Animation is outlined by keyframes which decide how a component ought to look and be positioned at sure factors. CSS Transitions use two keyframes (beginning and ending worth), whereas CSS animations permit extra exact management with the @keyframes rule.

Example of animation output (keyframes) of a box that moves from 0px (0% animation output) to 500px (100% animation output) from left to right
Instance of animation output (keyframes) of a field that strikes from 0px (0% animation output) to 500px (100% animation output) from left to proper. (Large preview)

Animation period determines the period of time for the animation to go from the primary keyframe to the final. The next graph exhibits the connection between the animation keyframes and period.

Example of animation output of a box that moves from 0px (0% animation output) to 500px (100% animation output) with 1000ms duration. The two points are connected by a straight line.
Instance of animation output of a field that strikes from 0px (0% animation output) to 500px (100% animation output) with 1000ms period. The 2 factors are linked by a straight line. (Large preview)

There are numerous methods wherein animation can progress between two keyframes. For instance, animation can have a relentless pace or it may transfer shortly at first and decelerate close to the tip, or transfer slowly at first after which pace up till it reaches the tip, and so forth. This rate, or speed is outlined with the easing features (timing features). If we check out the earlier graph, the easing perform is represented by the form of the road connecting the 2 factors. We’ve used the linear perform (straight line) for the earlier instance, however we are able to additionally use a curve to attach the keyframes.

Example of animation output of a box that moves from 0px (first keyframe) to 500px (last keyframe) with 1000ms duration. Animation will accelerate on the starting keyframe and decelerate near the last keyframe.
Instance of animation output of a field that strikes from 0px (first keyframe) to 500px (final keyframe) with 1000ms period. Animation will speed up on the beginning keyframe and decelerate close to the final keyframe. (Large preview)

As you may see, there are many attainable choices and variations for animation easing features and we’ll check out them subsequent.

Varieties Of Easing Features

There are three foremost forms of easing features that can be utilized in CSS:

  • Linear features (linear),
  • Cubic Bézier features (consists of ease, ease-in, ease-out and ease-in-out),
  • Staircase features (steps).

Linear Features

We’ve lined linear features in one of many earlier examples, so let’s do a fast recap. With the linear timing perform, the animation goes by way of the keyframes at a relentless pace. As you would possibly already know, the linear timing perform will be simply set in CSS through the use of the linear key phrase.

Example of animation output of a box that moves from 0px (0% animation output) to 500px (100% animation output) with 1000ms duration.
Instance of animation output of a field that strikes from 0px (0% animation output) to 500px (100% animation output) with 1000ms period. (Large preview)
Example of animation output of a box that moves from 0px (first keyframe) to 200px (second keyframe) and then to 500px (final keyframe) with 1000ms duration.
Instance of animation output of a field that strikes from 0px (first keyframe) to 200px (second keyframe) after which to 500px (last keyframe) with 1000ms period. (Large preview)

See the Pen [Animation – linear](https://codepen.io/smashingmag/pen/Bapbgxg) by Adrian Bece.

See the Pen Animation – linear by Adrian Bece.

Cubic Bézier Features

Though linear timing features have their use-cases, they’ll make the animations look bland and unnatural if used incorrectly or used too usually. As we’ve seen from the In Pieces instance, customers reply higher to pure movement, i.e. non-linear timing features which may speed up and decelerate.

Bézier curves are generally utilized in vector graphics, animations and robotics to simply create clean curves and trajectories. In CSS we’re utilizing Bézier curves outlined by 4 factors, that are often called Cubic Bézier curves.

Generally-used pre-defined easing features like ease, ease-in, ease-out, and ease-in-out belong to the Cubic Bézier features. They can be utilized as a fast solution to set a non-linear easing perform. Even a linear perform will be outlined utilizing a cubic-bezier perform.

Easing Perform cubic-bezier Worth Beginning Velocity Center Velocity Ending Velocity
linear cubic-bezier(0.0, 0.0, 1.0, 1.0) fixed fixed fixed
ease cubic-bezier(0.25, 0.1, 0.25, 1.0) quick acceleration quick acceleration sluggish acceleration
ease-in cubic-bezier(0.42, 0, 1.0, 1.0) sluggish acceleration quick acceleration full pace
ease-out cubic-bezier(0, 0, 0.58, 1.0) full pace sluggish acceleration sluggish acceleration
ease-in-out cubic-bezier(0.42, 0, 0.58, 1.0) sluggish acceleration full pace quick acceleration

Regardless that pre-defined values work effectively for a lot of instances, understanding tips on how to create {custom} Cubic Bézier features offers you much more management over the appear and feel of the animation which may make the animation look much more spectacular and impactful.

Within the following instance, I’ve edited the animations for In Items instance to make use of a special Cubic Bézier perform with totally different values. You may see how vastly totally different the animation appears and feels with this easing perform.

cubic-bezier(0, 1.2, 1, 0.2)

Let’s check out the cubic-bezier perform which is used to outline Cubic Bézier curves in CSS. Cubic Bézier perform is outlined by 4 factors (x and y coordinate pairs), however we solely outline 2 factors in cubic-bezier perform. Why is that?

Cubic Bezier curve example for CSS animation.
Cubic Bezier curve instance for CSS animation. (Picture supply: MDN Web docs) (Large preview)

It’s because the primary (P0) and final factors (P3) are fastened to the beginning (preliminary animation state) and the tip (last animation state) of the curve, because the animation wants to finish on a specified keyframe and inside the specified period. With the 2 remaining factors (P1 and P2), we are able to fine-tune the curve and easing of the perform, ensuing with non-linear animation pace.

cubic-bezier(x1, y1, x2, y2)

X coordinates (x1 and x2) symbolize time ratio and are restricted to values between 0 and 1 (the animation can’t start sooner or last more than specified), whereas Y coordinates (y1 and y2) symbolize the animation output and their values, that are normally set someplace between 0 and 1 however are usually not restricted to that vary. We will use the y1 and y2 values which can be outdoors the 0 and 1 vary to create bouncing results.

animation with bouncing effects
(Large preview)

If the animation consists of a number of keyframes, outlined in CSS @keyframes property, the easing perform will probably be utilized to every curve between the 2 factors. If we’re making use of ease-out perform to an animation with 3 keyframes, the animation will speed up at first of the primary keyframe, and decelerate close to the second keyframe and the identical movement will probably be repeated for the subsequent pair of keyframes (second keyframe and the final keyframe).

See the Pen [Cubic-bezier functions 2 keyframes](https://codepen.io/smashingmag/pen/zYNbVME) by Adrian Bece.

See the Pen Cubic-bezier functions 2 keyframes by Adrian Bece.

Discover how the easing perform is repeated between every keyframe pair — first and second keyframes (first pair), and second keyframe and final keyframe (second pair). The animation period is identical for each the earlier and the next examples.

See the Pen [Cubic Bezier functions 3 keyframes](https://codepen.io/smashingmag/pen/KKaEjbM) by Adrian Bece.

See the Pen Cubic Bezier functions 3 keyframes by Adrian Bece.

Creating these features could be a complicated job, so that you in all probability gained’t be adjusting the coordinates by guessing the cubic-bezier parameters. You’ll have to make use of a instrument that will help you nail these magic numbers with a view to create a timing perform that completely matches your animation. Fortunately, there are quite a few browser and on-line instruments to assist us out. We’ll speak about them in one of many following sections on this article.

Staircase Features

Staircase features allow animation to leap between the particular variety of frames in a non-continuous method. You may consider it as a “ticking” animation.

For instance, if we check out one of many earlier examples the place a field strikes from 0px to 500px and we restrict the animations to five steps, the animation will leap between the next 5 keyframes — 0px, 100px, 200px, 300px and 400px positions.

We will simply obtain this with steps perform in CSS.


This perform has an extra choice for controlling which keyframes are included. As you may have seen from the earlier instance, the animation of a field transferring from 0px to 500px with 5 steps will finish in a 400px place. If we would like the animation to begin from 100px and finish in a 500px place, we are able to use the leap time period choice as a second argument. Soar time period impacts how the keyframes will probably be chosen from the animation timeline.

steps(number_of_frames, jump_term)
steps(5, jump-start)

Following leap time period choices can be utilized in CSS steps perform:

  • jump-start
    Animation jumps proper from the place to begin and the place to begin shouldn’t be seen t. From our instance instance, keyframes will probably be 100px, 200px, 300px, 400px, 500px.
  • jump-end
    Final leap occurs when animation ends and isn’t seen. From our instance instance, keyframes will probably be 0px, 100px, 200px, 300px, 400px.
  • jump-both
    Each the primary and final leap will occur as animation begins and ends respectively, so that they gained’t be seen. All 5 jumps will occur between staring and ending factors. From our instance, these keyframes will probably be 80px, 165px, 250px, 335px, 420px.
  • jump-none
    Each first and final leap will probably be seen. From our instance, these keyframes will probably be 0px, 125px, 250px, 375px, 500px.
Timeline of step (5) animation with 1000ms duration
Timeline of step(5) animation with 1000ms period. (Large preview)

The next instance showcases how varied leap phrases have an effect on the animation conduct. Varied leap phrases are utilized to the 5-step animation with the identical period.

See the Pen [Step function](https://codepen.io/smashingmag/pen/ZELPdPK) by Adrian Bece.

See the Pen Step function by Adrian Bece.

Debugging Animations And Helpful Instruments

As we’ve seen from the Cubic Bézier instance, we’d like some sort of instrument that may assist us fine-tune the Cubic Bézier curve parameters so we are able to obtain the appear and feel of the animation that we would like.

On this part, we’ll check out the browser instruments, web sites, and CSS kinds that ought to assist us do exactly that.

Browser Instruments

Browser developer instruments present helpful easing perform enhancing options out of the field. Please be aware that solely Cubic Bézier features can be found for enhancing. These instruments supply a fast and easy animation preview in order that the developer can get instantaneous suggestions and fine-tune the easing perform.

Chrome, Safari, Firefox.
Chrome, Safari, Firefox. (Large preview)

Chrome, Safari and Firefox additionally supply a devoted Animations tab in developer instruments that provides a extra detailed overview, together with animation properties, period, timeline, keyframes, delay, and so forth.

Chrome, Safari, Firefox.
Chrome, Safari, Firefox. (Large preview)

Helpful Instruments And Web sites

There are many helpful on-line sources and easing presets that may give rather more selection to easing features.

Extra fashionable on-line sources embody Easing Functions Cheat Sheet by Andrey Sitnik and Ivan Solovev and CSS Easing Animation Tool by Matthew Lein. These instruments supply a variety of presets that you should utilize as a basis in your easing perform after which fine-tune the curve to suit your animation timeline.

Easing Functions Cheat Sheet contains 30 useful Cubic Bézier function presets
Easing Features Cheat Sheet accommodates 30 helpful perform presets. (Large preview)

Animations & Accessibility

When working with easing features and animations typically, it’s essential to handle accessibility necessities. Some people favor shopping the net with decreased movement, so we should always present a correct fallback. This may be simply finished with widely-supported prefers-reduced-motion media question. This media question permits us to both take away the animation or assign a special animation primarily based on consumer desire.

.animated-element {
  animation: /* Common animation */;

@media (prefers-reduced-motion) {
  .animated-element {
    /* Accessible animation with decreased movement */

I’ve modified an analog clock example by Alvaro Montoro to incorporate different animation for customers with prefers-reduced-motion flag set.

See the Pen [CSS Analog Clock with prefers reduced motion](https://codepen.io/smashingmag/pen/QWdoXPN) by Adrian Bece.

See the Pen CSS Analog Clock with prefers reduced motion by Adrian Bece.

On a default animation, the seconds hand of the clock is consistently transferring which can trigger difficulties for some customers. We will simply make the animation rather more accessible by altering the animation timing perform to steps. Within the following instance, customers with prefers-reduced-motion flag set will probably be displayed an animation the place seconds arm ticks each 5 seconds.

@media (prefers-reduced-motion) {
  .arm.second {
    animation-timing-function: steps(12);


Easing features, or timing features, change the animation’s appear and feel by affecting the animation charge (pace). Easing features allow us to create animations that resemble pure movement which may end up in improved, extra pleasant UX and having a greater impression on the customers. We’ve seen how we are able to use pre-defined values like linear, ease-out, ease, and so forth. to shortly add a timing perform and tips on how to create {custom} easing features with cubic-bezier perform for extra spectacular and impactful animations. We’ve additionally lined staircase features that can be utilized to create “ticking” animation and are not often used. When creating animations, it’s essential to maintain accessibility in thoughts and supply another, much less distracting animations with much less movement to customers with prefers-reduced-motion flag set.

There are many browser and on-line instruments that may simplify and streamline creating {custom} easing features, so creating animations with a stupendous circulation is less complicated than ever. When you haven’t finished so already, I’d suggest experimenting with varied easing features and creating your personal easing perform library.


Smashing Editorial
(vf, yk, il)

Source link