CSS - animation-duration Property



The CSS property animation-duration defines how long an animation takes to complete a single cycle.

The shorthand property animation is often convenient to use, as it allows all animation properties to be set at the same time.

Possible Value

The CSS property animation-duration can have following value:

<time> - The duration of an animation that runs through a complete cycle can be specified in either seconds (s) or milliseconds (ms). It's important that the value is a positive number or zero, and specifying the unit is mandatory.

If no value is specified, the default value 0s is used so that the animation can be continued (triggering the animationStart and animationEnd events).

Whether the animation is visible with a duration of 0s depends on the animation-fill-mode value, which is described below:

  • The initial frame of the animation, as determined by animation-direction, will be displayed during the animation-delay countdown, if animation-fill-mode is set to backwards or both.

  • The last frame of the animation will be displayed, as determined by animation-direction, when the animation-delay expires if animation-fill-mode is set to forwards or both.

  • The animation will not be displayed if animation-fill-mode is set to none.

Note: The declaration gets ignored, when negative value is passed, as it is invalid.

Note: When multiple comma-separated values on an animation-* property is specified, the values are applied to the animations in the order in which the animation-names appear.

Note: While defining the CSS scroll-driven animations, the value of animation-iteration-count determines the number of times the animation is repeated over the course of timeline's progress. In case no value is specified for animation-iteration-count, the animation is executed only once. infinte is a valid value, but may result in an unworkable animation in case of CSS scroll-driven animation.

Syntax

animation-duration = <time [0s,∞]>#  

Applies To

All the HTML elements, ::before and ::after pseudo-elements.

CSS animation-duration - Basic Example

The following example demonstrates how to create a simple animation with a specified duration using the animation-duration property.

  • In this example, we have a red box that moves horizontally from left to right and then back to the left.

  • The animation-duration property is used to specify the duration of the animation in seconds or milliseconds.

  • Here, we have set the duration to 2 seconds (animation-duration: 2s;).

 
<html>
<head>
<style>
   .box {
      width: 100px;
      height: 100px;
      background-color: red;
      position: relative;
      animation-name: move;
      animation-duration: 4s;
      animation-iteration-count: infinite;
      animation-direction: alternate;
   }
   @keyframes move {
      0% {
      left: 0;
      }
      100% {
      left: 400px;
      }
   }
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>   
Advertisements