This chapter covers the following properties:
The numbers in the table indicate the earliest browser version that fully supports the property.
An animation enables an element to smoothly transition from one style to another, allowing for the alteration of multiple CSS properties as desired.
To utilize CSS animation, you need to define keyframes, which specify the styles the element will have at specific points in time.
When CSS styles are defined within the @keyframes rule, the animation will smoothly transition from the current style to the defined style at specified intervals. To activate an animation, it must be associated with an element.
In the following example, the “example” animation is applied to the <div> element. The animation duration is set to 4 seconds, during which the background-color of the <div> element will transition gradually from “red” to “yellow”.
Example
/* The animation code */ @keyframes example { from {background-color: red;} to {background-color: yellow;} } /* The element to apply the animation to */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; } |
Note: The animation-duration property dictates the duration of an animation’s complete cycle. If animation-duration is not set, the animation won’t occur, defaulting to 0s (0 seconds).
In the provided example, we use “from” and “to” keywords to signify the initial and final states (0% and 100% completion). Alternatively, percentages can be used to specify multiple style changes throughout the animation.
In the following example, the background-color of the <div> element changes at 25%, 50%, and 100% completion of the animation.
Example
/* The animation code */ @keyframes example { 0% {background-color: red;} 25% {background-color: yellow;} 50% {background-color: blue;} 100% {background-color: green;} } /* The element to apply the animation to */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; } |
The provided example demonstrates altering both the background-color and position of the <div> element at 25%, 50%, and 100% completion of the animation.
Example
/* The animation code */ @keyframes example { 0% {background-color:red; left:0px; top:0px;} 25% {background-color:yellow; left:200px; top:0px;} 50% {background-color:blue; left:200px; top:200px;} 75% {background-color:green; left:0px; top:200px;} 100% {background-color:red; left:0px; top:0px;} } /* The element to apply the animation to */ div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; } |
The animation-delay property determines a delay before the initiation of an animation.
In the following example, there is a 2-second delay before the animation begins.
Example
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: 2s; } |
Negative values are also permitted. When using negative values, the animation begins as if it had been playing for N seconds prior.
In the following example, the animation starts as if it had been playing for 2 seconds already:
Example
div { |
The animation-iteration-count property determines how many times an animation should execute.
In the following example, the animation will run three times before stopping:
Example
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 3; } |
In the following example, the animation is set to continue indefinitely by using the value “infinite”:
Example
div { |
The animation-direction property determines the playback direction of an animation and offers these values:
In the following example, the animation is set to play in reverse direction (backwards):
Example
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-direction: reverse; } |
In the provided example, the “alternate” value is utilized to execute the animation in a forward direction initially, followed by a backward motion.
Example
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate; } |
In the given example, the “alternate-reverse” value is employed to initiate the animation with a backward motion first, followed by a forward movement.
Example
div { |
The animation-timing-function property defines the pace curve of an animation, offering various options:
The following example illustrates different speed curves available for use.
Example
#div1 {animation-timing-function: linear;} #div2 {animation-timing-function: ease;} #div3 {animation-timing-function: ease-in;} #div4 {animation-timing-function: ease-out;} #div5 {animation-timing-function: ease-in-out;} |
The animation-fill-mode property allows you to control how styles are applied to an element before and after the animation plays.
It can have the following values:
The following example demonstrates how the <div> element preserves style values from the last keyframe after the animation ends.
Example
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-fill-mode: forwards; } |
The following example allows the <div> element to adopt the style values set by the first keyframe before the animation starts, maintaining them during the animation-delay period.
Example
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: backwards; } |
The following example enables the <div> element to acquire the style values defined by the first keyframe before the animation commences, and to preserve the style values from the last keyframe once the animation concludes.
Example
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: both; } |
The example below utilizes six animation properties:
Example
div { animation-name: example; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; } |
You can achieve the same animation effect as above by using the shorthand animation property.
Example
div { animation: example 5s linear 2s infinite alternate; } |