CSS propertyAnimations
animation-direction
Sets the playback direction of the animation: forwards, backwards, or alternating.
- Initial value
normal- Inherited
- No
- Animatable
- no
- Applies to
- all elements and the ::before and ::after pseudo-elements
- JavaScript
element.style.animationDirection = "alternate"
Example
#CSS
.float {
animation: float 3s ease-in-out infinite alternate;
}
@keyframes float {
to { translate: 0 -8px; }
}Example HTML
<style>@keyframes float-y { to { translate: 0 -8px; } }</style>
<div class="box" style="width: 120px; animation: float-y 3s ease-in-out infinite alternate;">I float</div>Preview
Definition and usage
#The animation-direction property decides whether the animation runs from from to to (normal), from the end to the start (reverse), or alternates between both directions (alternate).
With a looping animation using normal, the element jumps back to the first keyframe after the last one, which can show up as a jolt. alternate makes it return smoothly along the same path.
alternate-reverse works like alternate, but starts with a backward run.
Syntax
#Syntax
animation-direction: normal | reverse | alternate | alternate-reverse;Values
#- From left to right, then a jump back to the start.
- From right to left, then a jump.
- Smoothly back and forth.
- Back and forth, starting from the end.
Values in practice
#normal
From left to right, then a jump back to the start.
.demo { animation-direction: normal; }reverse
From right to left, then a jump.
.demo { animation-direction: reverse; }alternate
Smoothly back and forth.
.demo { animation-direction: alternate; }alternate-reverse
Back and forth, starting from the end.
.demo { animation-direction: alternate-reverse; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#alternatealmost always fits calm, looping movements (floating, breathing).- In the
animationshorthand you can add the direction after the iteration count:β¦ infinite alternate.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.