English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS Reference Manual

CSS @rules (RULES)

Complete List of CSS Properties

CSS3 animation-Usage and examples of direction attribute

CSS animation-The direction attribute specifies whether the animation should play in reverse in an alternating loop.

The following table summarizes the usage context and version history of this attribute.

Default value:normal
Applies to:All elements ::before and ::after Pseudo-element
Inheritance:None
Animatable:No.See also Animation properties.
Version: CSS3New feature of
JavaScript syntax:JavaScript syntax: object.style.animationDirection="reverse"

animation-Syntax of using direction

The syntax of this attribute is as follows:

animation-direction: normal | reverse | alternate | alternate-reverse | initial | inherit

The following example demonstrates how to use animation-direction attribute.

.box {
    width: 50px;
    height: 50px;
    background: red;
    position: relative;
    /* Chrome, Safari, Opera */
    -webkit-animation-name: moveit;
    -webkit-animation-duration: 4s;
    -webkit-animation-iteration-count: 2;
    -webkit-animation-direction: alternate;
    
    animation-name: moveit;
    animation-duration: 4s;
    animation-iteration-count: 2;
    animation-direction: alternate;
}
 
/* Chrome, Safari, Opera */
@-webkit-keyframes moveit {
    from {left: 0;}
    to {left: 50%;}
}
 
@keyframes moveit {
    from {left: 0;}
    to {left: 50%;}
}
Test and see‹/›

Note:If the animation is set to play only once, then animation-The direction attribute is invalid, please seeanimation-iteration-countAttribute.

Attribute value

The following table describes the values of this attribute.

ValueDescription
normalThe animation should play in each cycle. This is the default value.
reverseThe animation should play backward in each loop.
alternateThe animation plays forward in the first loop, then backward, and then continues to alternate.
alternate-reverseThe animation plays backward in the first loop, then forward, and then continues to alternate.
initialSet this property to its default value.
inheritIf specified, the associated elements use the computed value of the animation of their parent element-direction.

Browser compatibility

animation-Browser compatibility of the direction attribute, all major browsers support this attribute.

  • Firefox 5+ -moz-,15 +

  • Google Chrome 4+ -webkit-

  • Internet Explorer 10+

  • Apple Safari 4+ -webkit-

  • Opera 12+ -o-,15+ -webkit-

Further reading

Please refer to the following tutorials:CSS3Animation.

Related attributes and rules:animation,animation-name,animation-delay,animation-timing-function,animation-iteration-count,animation-direction,animation-fill-mode,animation-play-state,@keyframes.