CSS animation-direction
The animation-direction
property in CSS defines whether an animation should play forward, backward, alternate between forward and backward, or alternate with a reverse starting point. This property allows you to control the flow of the animation cycle and create more complex animation effects.
Syntax of animation-direction
</>
Copy
animation-direction: normal | reverse | alternate | alternate-reverse | initial | inherit;
Values
Value | Description |
---|---|
normal | The animation plays in the normal forward direction. This is the default value. |
reverse | The animation plays in reverse direction. |
alternate | The animation alternates between playing forward and backward, starting in the forward direction. |
alternate-reverse | The animation alternates between playing forward and backward, starting in the reverse direction. |
initial | Sets the property to its default value (normal ). |
inherit | Inherits the value from the parent element. |
Default Value
normal
Examples for animation-direction
Using the CSS animation-direction Property
The following examples demonstrate how the animation-direction
property can be used to control the direction of an animation.
</>
Copy
/* Play animation in normal direction */
.element {
animation-direction: normal;
}
/* Play animation in reverse direction */
.element {
animation-direction: reverse;
}
/* Alternate between forward and backward direction */
.element {
animation-direction: alternate;
}
/* Alternate between forward and backward, starting with reverse */
.element {
animation-direction: alternate-reverse;
}
Example for Applying animation-direction
The following example demonstrates the use of animation-direction
with different values.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<style>
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
.normal {
animation: slide 2s infinite;
animation-direction: normal;
}
.reverse {
animation: slide 2s infinite;
animation-direction: reverse;
}
.alternate {
animation: slide 2s infinite;
animation-direction: alternate;
}
.alternate-reverse {
animation: slide 2s infinite;
animation-direction: alternate-reverse;
}
.box {
width: 50px;
height: 50px;
margin: 10px;
background-color: red;
display: inline-block;
}
</style>
</head>
<body>
<h1>CSS animation-direction Example</h1>
<div><div class="box normal">Normal</div></div>
<div><div class="box reverse">Reverse</div></div>
<div><div class="box alternate">Alternate</div></div>
<div><div class="box alternate-reverse">Alt Reverse</div></div>
</body>
</html>
.normal
- The
normal
value makes the animation progress in the default forward direction. - The animation starts at the
from
keyframe and moves to theto
keyframe in each cycle. - For the
slide
animation, the box starts at its initial position (translateX(0)
) and moves totranslateX(100px)
repeatedly. - The result is a box that slides to the right and immediately resets to the starting position before repeating.
.reverse
- The
reverse
value makes the animation progress in the backward direction. - The animation starts at the
to
keyframe and moves to thefrom
keyframe in each cycle. - For the
slide
animation, the box starts attranslateX(100px)
and moves back totranslateX(0)
repeatedly. - The result is a box that slides to the left and resets to its end position before repeating.
.alternate
- The
alternate
value alternates the direction of the animation for each cycle. - In one cycle, the animation progresses forward (from
translateX(0)
totranslateX(100px)
), and in the next cycle, it moves backward (fromtranslateX(100px)
totranslateX(0)
). - This creates a back-and-forth motion for the animation.
- The result is a box that slides to the right and then slides back to the left repeatedly in a smooth transition.
.alternate-reverse
- The
alternate-reverse
value alternates the animation direction likealternate
, but it starts in the reverse direction. - In the first cycle, the animation progresses backward (from
translateX(100px)
totranslateX(0)
), and in the next cycle, it moves forward (fromtranslateX(0)
totranslateX(100px)
). - This creates a similar back-and-forth motion as
alternate
, but with the initial direction reversed. - The result is a box that slides to the left first and then slides back to the right repeatedly.
Browser Support
The animation-direction
property is supported in most modern browsers. Below is a compatibility table:
Browser | Browser Version |
---|---|
Chrome | 43 |
Edge | 10 |
Firefox | 16 |
Safari | 9 |
Opera | 30 |