CSS - Easing functions


CSS easing function is used to control the speed of a transition or animation between two states of an element. It defines the rate of change of a property over time, affecting the acceleration and deceleration during the animation.

Following are some noteworthy points:

Types Of Easing Functions

There are three main types of easing functions that can be used in CSS as listed below:

Linear Functions

With the linear timing function, the animation is going through the keyframes at a constant speed. Following keywords can be passed :

Cubic Bezier Functions

This feature in CSS defines Bzier curves that affect the progression of the animation or transition with four control points: a start point, an end point and two control points.

It helps create custom easing effects. A cubic Bzier function is defined by four control points, and it allows for precise control over the acceleration and deceleration of an animation. The predefined keyword values include:

<cubic-bezier()> - This function utilizes four <number> values to define the curve's shape. cubic-bezier(x1, y1, x2, y2)

X coordinates (x1 and x2) represent time ratio and are limited to values between 0 and 1 (the animation cannot begin sooner or last longer than specified), while Y coordinates (y1 and y2) represent the animation output and their values.

Steps Functions

Steps functions enable animation to jump between the specific number of frames in a non-continuous way. You can think of it as a "ticking" animation. It accepts following keywords:

step-start - This keyword represents the easing functions steps(1, jump-start) or steps(1, start). It signals an abrupt jump to the final state of the interpolation and maintains this state until completion.

step-end - This keyword represents the easing functions steps(1, jump-end) or steps(1, end). It means that the interpolation retains its initial state until the end, where it quickly changes to the final state.

steps() - This function takes a positive <integer> and an optional <step-position>.

Syntax

/* linear function and keyword */
/* linear(<point-list>) */
linear(1, -0.5, 0)
linear

/* cubic-bezier function and keywords */
/* cubic-bezier(<x1>, <y1>, <x2>, <y2>) */
cubic-bezier(0.25, 0.1, 0.25, 1)
ease
ease-in
ease-out
ease-in-out

/* steps function and keywords */
/* steps(<number-of-steps>, <direction>) */
steps(4, end)
steps(10, jump-both)
step-start
step-end  

CSS easing-function - linear-easing

The following example creates a red box that moves horizontally using a linear-easing function. The animation has a duration of 4 seconds and repeats infinitely, resulting in a continuous back-and-forth movement of the box.

<html>
<head>
<style>
   .box {
      width: 100px;
      height: 100px;
      margin: 150px;
      background-color: red;
      text-align:center;
      font-size:20px;
      position: relative;
      display: flex;
  	   justify-content: center;
  	   align-items: center;
      animation: move 4s linear infinite;
   }
   @keyframes move {
      0% {
         left: 5%;
      }
      100% {
         left: 60%;
      }
   }
</style>
</head>
<body>
   <div class="box">Linear Easing Function</div>
</body>
</html>

CSS easing-function - linear-easing With Values

The following example demonstrates various linear-easing function which accepts values.

The animation starts at position 0 and moves straight forward to 0.25. Following that, it keeps going in a straight line until it hits 1. The animation's progression during its runtime is shown by the notation linear(0, 0.25 75%, 1).

<html>
<head>
<style>
   .box {
      width: 100px;
      height: 100px;
      margin: 150px;
      background-color: red;
      text-align:center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size:20px;
      position: relative;
      animation: move 4s linear infinite;
   }
   .linear-demo-1 {
      animation-name: linear-custom;
      animation-timing-function: linear(0, 0.25 75%, 1);
   }
    @keyframes linear-custom {
      from {transform: translateX(0);}
      to {transform: translateX(350px);}
   }
</style>
</head>
<body>
<div class="box linear-demo-1">Linear Custom</div>
</body>
</html>

CSS easing-function - cubic-bezier

The following example demonstrates various cubic-bezier functions.

The ease-in box starts slow and speeds up, the ease-out box starts fast and slows down, and the ease-in-out box starts slow, speeds up in the middle, and then slows down toward the end of the animation.

<html>
<head>
<style>
   .box {
      width: 100px;
      height: 100px;
      background-color: #0077FF;
      margin: 20px auto;
      animation-duration: 4s;
      animation-iteration-count: infinite;
      text-align: center;
      font-size: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
   }
   .ease-in-demo {
      animation-name: easeIn;
      animation-timing-function: ease-in;
   }
   .ease-out-demo {
      animation-name: easeOut;
      animation-timing-function: ease-out;
   }
   .ease-in-out-demo {
      animation-name: easeInOut;
      animation-timing-function: ease-in-out;
   }
   @keyframes easeIn {
      from {transform: translateX(0);}
      to {transform: translateX(400px);}
   }
   @keyframes easeOut {
      from {transform: translateX(0);}
      to {transform: translateX(400px);}
   }
   @keyframes easeInOut {
      from {transform: translateX(0);}
      to {transform: translateX(400px);}
   }
</style>
</head>
<body>
   <div class="box ease-in-demo">Ease-in</div>
   <div class="box ease-out-demo">Ease-out</div>   
   <div class="box ease-in-out-demo">Ease-in-out</div>
</body>
</html>

CSS easing-function - cubic-bezier With Values.

The following example demonstrates various cubic-bezier functions with values.

With this configuration, an animation effect is produced in which every box has a different motion behavior as it progresses 300 pixels to the right from its initial location using a different cubic-bezier timing function.

<html>
<head>
<style>
   .box {
      width: 120px;
      height: 110px;
      background-color: #e3bd56;
      margin: 20px auto;
      animation-duration: 4s;
      animation-iteration-count: infinite;
      text-align: center;
      font-size: 20px;
      left:10px;
      display: flex;
      justify-content: center;
      align-items: center;
   }
   .cubic-bezier-demo-1 {
      animation-name: cubicBezier1;
      animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
   }
   .cubic-bezier-demo-2 {
      animation-name: cubicBezier2;
      animation-timing-function: cubic-bezier(0.42, 0, 0.58, 1);
   }
   .cubic-bezier-demo-3 {
      animation-name: cubicBezier3;
      animation-timing-function: cubic-bezier(0.55, 0.055, 0.675, 0.19);
   }
   .cubic-bezier-demo-4 {
      animation-name: cubicBezier4;
      animation-timing-function: cubic-bezier(0.95, 0.05, 0.795, 0.035);
   }
   @keyframes cubicBezier1 {
      from {transform: translateX(0);}
      to {transform: translateX(300px);}
   }
   @keyframes cubicBezier2 {
      from {transform: translateX(0);}
      to {transform: translateX(300px);}
   }
   @keyframes cubicBezier3 {
      from {transform: translateX(0);}
      to {transform: translateX(300px);}
   }
   @keyframes cubicBezier4 {
      from {transform: translateX(0);}
      to {transform: translateX(300px);}
   }
</style>
</head>
<body>
   <div class="box cubic-bezier-demo-1">Cubic Bezier (0.25, 0.1, 0.25, 1)</div>
   <div class="box cubic-bezier-demo-2">Cubic Bezier (0.42, 0, 0.58, 1)</div>   
   <div class="box cubic-bezier-demo-3">Cubic Bezier (0.55, 0.055, 0.675, 0.19)</div>
   <div class="box cubic-bezier-demo-4">Cubic Bezier (0.95, 0.05, 0.795, 0.035)</div>
</body>
</html>

CSS easing-function - step-easing

The following example demonstrates the various step-easing functions.

<html>
<head>
<style>
   body {
      display: flex;
      justify-content: space-around;
      align-items: center;
      height: 100vh;
      margin: 0;
      display: flex;
      justify-content: center;
      align-items: center;
   }
   .box {
      width: 100px;
      height: 50px;
      background-color: red;
      display: flex;
      justify-content: center; 
      align-items: center; 
      text-align: center;
      font-size: 16px;
      margin: 10px;
      animation: move 6s infinite;
   }
   .jump-start {
      animation-timing-function:steps(6, jump-start);
   }
   .jump-end {
      animation-timing-function:steps(6, jump-end);
   }
   .jump-both {
      animation-timing-function:steps(6, jump-both);
   }
   .jump-none {
      animation-timing-function:steps(6, jump-none);
   }
   .step-start {
      animation-timing-function: step-start;
   }
   .step-end {
      animation-timing-function: step-end;
   }
   .start {
      animation-timing-function: steps(6, start);
   }
   .end {
   animation-timing-function: steps(6, end);
   }
   @keyframes move {
      0%, 100% {
      transform: translateY(0);
      }
      50% {
      transform: translateY(250px);
      }
   }
</style>
</head>
<body>
<div class="box jump-start">Jump-start</div>
<div class="box jump-end">Jump-end</div>
<div class="box jump-both">Jump-both</div>
<div class="box jump-none">Jump-none</div>
<div class="box step-start">Step-start</div>
<div class="box step-end">Step-end</div>
<div class="box start">Start</div>
<div class="box end">End</div>
</body>
</html>