CSS animation-duration

The animation-duration property in CSS specifies the length of time an animation should take to complete one cycle. It is an essential property for defining how long an animation effect lasts, providing control over the speed of the animation.


Syntax of animation-duration

</>
Copy
animation-duration: time | initial | inherit;

Values

ValueDescription
timeDefines the duration of the animation in seconds (s) or milliseconds (ms), e.g., 2s or 500ms. Default value is 0, meaning no animation will occur.
initialSets the property to its default value (0).
inheritInherits the value from the parent element.

Default Value

0


Examples for animation-duration

Using the CSS animation-duration Property

The following examples demonstrate how the animation-duration property can be used to define the length of an animation cycle.

</>
Copy
/* Set animation duration to 2 seconds */
.element {
  animation-duration: 2s;
}

/* Set animation duration to 500 milliseconds */
.element {
  animation-duration: 500ms;
}

/* Inherit the duration value from the parent element */
.element {
  animation-duration: inherit;
}

Example for Applying animation-duration

The following example demonstrates the use of animation-duration with different durations for animation effects.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<style>
  @keyframes slide {
    from {
      transform: translateX(0);
    }
    to {
      transform: translateX(200px);
    }
  }

  .fast {
    animation: slide 1s;
    background-color: lightgreen;
    padding: 10px;
    margin: 10px;
  }

  .slow {
    animation: slide 4s;
    background-color: lightblue;
    padding: 10px;
    margin: 10px;
  }

  .default {
    animation: slide;
    background-color: lightcoral;
    padding: 10px;
    margin: 10px;
  }
  
  div {
    width: 200px;
  }
</style>
</head>
<body>

<h1>CSS animation-duration Example</h1>

<div class="fast">Fast Animation</div>
<div class="slow">Slow Animation</div>
<div class="default">Default Animation (0s)</div>

</body>
</html>

Explanation of animation-duration for each class:

Fast Animation (.fast):

  1. The animation-duration is set to 1s, meaning the animation completes one cycle in 1 second.
  2. The slide animation moves the element from translateX(0) to translateX(200px) quickly within a 1-second interval.
  3. The result is a light green box that slides to the right quickly.

Slow Animation (.slow):

  1. The animation-duration is set to 4s, meaning the animation completes one cycle in 4 seconds.
  2. The slide animation moves the element from translateX(0) to translateX(200px) at a slower pace, taking 4 seconds to complete the motion.
  3. The result is a light blue box that slides to the right slowly.

Default Animation (.default):

  1. The animation-duration is not explicitly defined, so it defaults to 0s.
  2. With a duration of 0s, the animation executes instantly, without any visible transition or motion.
  3. The result is a light coral box that immediately appears at the final position (translateX(200px)) without any sliding effect.

Browser Support

The animation-duration property is supported in most modern browsers. Below is a compatibility table:

BrowserVersion
Chrome43
Edge10
Firefox16
Safari9
Opera30