- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - animation-direction Property
The CSS property animation-direction decides the direction that an animation follows: either forwards, backwards or alternating between these two directions, creating a back and forth movement.
Using the shorthand property animation is a common and simple way to configure all animation properties at once.
Possible Values
normal - The animation progresses in the forward direction with each cycle. When the animation has completed a cycle, it essentially resets to the initial state and starts again. This behavior is the default setting.
reverse - During each cycle, the animation runs backwards. This means that once a cycle has been completed, the animation is reset to its final state and starts again.
The steps of the animation run backwards, including the reversal of easing functions.
alternate - Each cycle of the animation alternately changes direction, starting with the first iteration, which moves forward. The count to distinguish between even and odd cycles starts at one.
alternate-reverse - In each cycle of the animation, the direction changes, starting with the first iteration that moves backwards. The count to differentiate between even and odd cycles starts at one.
Note: When multiple comma-separated values on an animation-* property is specified, the values are applied to the animations in the order in which the animation-names appear.
Note: While defining the CSS scroll-driven animations, the value of animation-iteration-count determines the number of times the animation is repeated over the course of timeline's progress. In case no value is specified for animation-iteration-count, the animation is executed only once. infinte is a valid value, but may result in an unworkable animation in case of CSS scroll-driven animation.
Syntax
animation-direction = <single-animation-direction># <single-animation-direction> = normal | reverse | alternate | alternate-reverse
Applies To
All the HTML elements, ::before and ::after pseudo-elements.
CSS animation-direction - normal Value
The following example demonstrates animation-direction with value as normal.
The CSS animation-direction property determines how an animation runs through its keyframes.
In this example, the value normal is used.
The normal causes the animation to run forward.
<html> <head> <style> /* Centering the content vertically and horizontally */ body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } /* Styling the container for the boxes */ .container { display: flex; justify-content: space-around; align-items: center; } /* Styling for the box with 'normal' animation */ .normal-box { width: 100px; height: 100px; margin: 20px; background-color: #3498db; animation-duration: 2s; animation-timing-function: linear; animation-iteration-count: infinite; animation-name: moveNormal; animation-direction: normal; /* Animation moves continuously from start to end */ } /* Keyframes for the 'moveNormal' animation */ @keyframes moveNormal { from { transform: translateX(0); } to { transform: translateX(200px); /* Moves the box horizontally to the right */ } } </style> </head> <body> <div class="container"> <div class="normal-box"></div> <!-- Box with 'normal' animation --> </div> </body> </html>
CSS animation-direction - alternate Value
The following example demonstrates animation-direction with value as alternate.
The CSS animation-direction property determines how an animation runs through its keyframes.
In this example, the value alternate is used.
The alternate causes the animation to run back and forth.
<html> <head> <style> /* Centering the content vertically and horizontally */ body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } /* Styling the container for the boxes */ .container { display: flex; justify-content: space-around; align-items: center; } /* Styling for the box with 'alternate' animation */ .alternate-box { width: 100px; height: 100px; margin: 20px; background-color: #e74c3c; animation-duration: 2s; animation-timing-function: linear; animation-iteration-count: infinite; animation-name: moveAlternate; animation-direction: alternate; /* Animation oscillates back and forth */ } /* Keyframes for the 'moveAlternate' animation */ @keyframes moveAlternate { from { transform: translateX(0); } to { transform: translateX(200px); /* Moves the box back and forth horizontally */ } } </style> </head> <body> <div class="container"> <div class="alternate-box"></div> <!-- Box with 'alternate' animation --> </div> </body> </html>
CSS animation-direction - reverse Value
The following example demonstrates animation-direction with value as reverse.
The CSS animation-direction property determines how an animation runs through its keyframes.
In this example, the value reverse is used.
The reverse causes the animation to run backwards each cycle.
<html> <head> <style> /* Centering the content vertically and horizontally */ body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } /* Styling the container for the boxes */ .container { display: flex; justify-content: space-around; align-items: center; } /* Styling for the box with 'reverse' animation */ .reverse-box { width: 100px; height: 100px; margin: 20px; background-color: #e74c3c; animation-duration: 2s; animation-timing-function: linear; animation-iteration-count: infinite; animation-name: moveReverse; animation-direction: reverse; /* Animation oscillates backwards */ } /* Keyframes for the 'moveReverse' animation */ @keyframes moveReverse { from { transform: translateX(0); } to { transform: translateX(200px); /* Moves the box back and forth horizontally */ } } </style> </head> <body> <div class="container"> <div class="reverse-box"></div> <!-- Box with 'reverse' animation --> </div> </body> </html>
CSS animation-direction - alternate-reverse Value
The following example demonstrates animation-direction with value as alternate-reverse.
The CSS animation-direction property determines how an animation runs through its keyframes.
In this example, the value alternate-reverse is used.
The reverse causes the animation to reverse its direction each cycle.
<html> <head> <style> /* Centering the content vertically and horizontally */ body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } /* Styling the container for the boxes */ .container { display: flex; justify-content: space-around; align-items: center; } /* Styling for the box with 'alternate-reverse' animation */ .alt-reverse-box { width: 100px; height: 100px; margin: 20px; background-color: #e74c3c; animation-duration: 2s; animation-timing-function: linear; animation-iteration-count: infinite; animation-name: moveAltReverse; animation-direction: alternate-reverse; /* Animation oscillates backwards and then changes the direction */ } /* Keyframes for the 'moveAltReverse' animation */ @keyframes moveAltReverse { from { transform: translateX(0); } to { transform: translateX(200px); /* Moves the box back and forth horizontally */ } } </style> </head> <body> <div class="container"> <div class="alt-reverse-box"></div> <!-- Box with 'alternate-reverse' animation --> </div> </body> </html>