CSS Reference ยท Transforms, Transitions and Animations
The animation-direction property
Reference for the CSS animation-direction property: normal, reverse, alternate and alternate-reverse, how easing flips on reverse plays, with live examples.
The animation-direction property decides in which direction each iteration of a CSS animation plays. By default every iteration runs from the first keyframe to the last and then jumps back to the start. With reverse it runs backwards, and with the two alternate values it flips direction on every iteration so the motion goes back and forth smoothly.
It only matters when an animation repeats or when you want to play the keyframes backwards. For a single forward run, the default normal is what you want.
.pulse { animation: slide 1s ease-in-out infinite; animation-direction: alternate; }
Try it
The blue dot is the from keyframe and the red dot is the to keyframe. Watch where each row starts and how the first two rows jump back while the last two glide.
Syntax
animation-direction: normal | reverse | alternate | alternate-reverse; /* several animations, one value each */ animation-direction: normal, alternate;
- Initial value:
normal - Applies to: all elements,
::beforeand::after - Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
normal |
Every iteration plays forwards, from 0% to 100%. |
reverse |
Every iteration plays backwards, from 100% to 0%. |
alternate |
Odd iterations (1st, 3rd, ...) play forwards, even iterations (2nd, 4th, ...) play backwards. |
alternate-reverse |
Odd iterations play backwards, even iterations play forwards. |
Usage notes
- Counting starts at 1. In
alternate, the first iteration is forwards. Inalternate-reverse, the first iteration is backwards. A one-time animation (animation-iteration-count: 1) withalternatetherefore looks the same asnormal. - Easing is flipped too. When an iteration plays backwards, the timing function is reversed as well, so
ease-inbecomes anease-outon the way back. This is whyalternatelooks natural with easing such asease-in-out. - Pairs well with
infinite. Breathing, pulsing and ping-pong effects are the classic use. Useanimation-iteration-count: 2(or any even number) withalternateto end where you started. - Interacts with fill modes.
animation-fill-mode: backwardsshows the first keyframe in the direction of the first iteration, which is the 100% keyframe withreverse. See animation-fill-mode. - Lists are matched by position. With several animation names, the nth direction belongs to the nth name. A shorter list repeats.
- Reduced motion. Looping animations can bother some people. Wrap them in a
prefers-reduced-motion: no-preferencemedia query or switch them off for users who ask for less motion.
Browser support
Supported in every current browser, including alternate-reverse.
Related
animationโ the shorthand that includes the directionanimation-iteration-countโ how many times it repeatsanimation-fill-modeโ styles before and after the run@keyframesโ defining the steps- Lesson: CSS Animations