CSS animation-direction property


The animation-direction property in CSS specifies the direction in which an animation should play. It defines whether the animation should play in a forward or backward direction, or alternate between these directions. This property is useful for creating animations that need to reverse direction or repeat in a different way.

Syntax

element { animation-direction: direction; }
  • direction: Defines the direction in which the animation should play. It can be one of the following values:
    • normal
    • reverse
    • alternate
    • alternate-reverse

Values

  1. normal:

    • The animation plays in the direction defined by the keyframes from start to end (0% to 100%).
    animation-direction: normal;
    • This is the default value.
  2. reverse:

    • The animation plays in the reverse direction, from end to start (100% to 0%).
    animation-direction: reverse;
  3. alternate:

    • The animation alternates between playing forward and backward. For example, it plays forward on the first iteration, then reverses direction for the second iteration, and so on.
    animation-direction: alternate;
  4. alternate-reverse:

    • The animation alternates between playing backward and forward. For example, it plays backward on the first iteration, then plays forward on the second iteration, and so on.
    animation-direction: alternate-reverse;

Example

HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Animation Direction Example</title> <style> @keyframes move { from { transform: translateX(0); } to { transform: translateX(100px); } } .box-normal { width: 100px; height: 100px; background-color: red; animation-name: move; animation-duration: 2s; animation-direction: normal; } .box-reverse { width: 100px; height: 100px; background-color: green; animation-name: move; animation-duration: 2s; animation-direction: reverse; } .box-alternate { width: 100px; height: 100px; background-color: blue; animation-name: move; animation-duration: 2s; animation-direction: alternate; } .box-alternate-reverse { width: 100px; height: 100px; background-color: orange; animation-name: move; animation-duration: 2s; animation-direction: alternate-reverse; } </style> </head> <body> <div class="box-normal"></div> <div class="box-reverse"></div> <div class="box-alternate"></div> <div class="box-alternate-reverse"></div> </body> </html>

Explanation:

  • @keyframes move:

    • Defines an animation that moves the element horizontally from 0px to 100px.
  • .box-normal:

    • Plays the animation from start to end (0% to 100%) in the normal direction.
  • .box-reverse:

    • Plays the animation in reverse, moving from 100px back to 0px.
  • .box-alternate:

    • Alternates between moving forward and backward with each iteration.
  • .box-alternate-reverse:

    • Alternates between moving backward and forward with each iteration.

Important Points

  1. Default Value:

    • The default value is normal, meaning the animation will play in the direction defined by the keyframes.
  2. Interaction with Iteration Count:

    • The animation-direction property works in conjunction with animation-iteration-count. For example, with animation-direction: alternate, if animation-iteration-count is 3, the animation will play forward, then backward, then forward again.
  3. Reversing Animation:

    • Using reverse can be useful when you want the animation to start at the end state and animate backward to the start state.
  4. Alternating Animations:

    • The alternate and alternate-reverse values are especially useful for creating animations that need to play back and forth, such as a bouncing effect or a sliding transition.