CSS3 Animations
CSS3 Animations
CSS3 can create animations, which can replace many animated web images, Flash animations, and effects implemented with JavaScript.
CSS3
Animation
CSS3 @keyframes rule
To create CSS3 animations, you need to understand the @keyframes rule.
The @keyframes rule is for creating animations.
Within the @keyframes rule you specify a CSS style, and the animation will gradually change from the current style to the new style.
Browser support
The numbers in the table indicate the first browser version that supports this property.
A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.
| property | |||||
|---|---|---|---|---|---|
| @keyframes | 43.0 | ||||
| 4.0 -webkit- | 10.0 | 16.0 | |||
| 5.0 -moz- | 9.0 | ||||
| 4.0 -webkit- | 30.0 |
15.0 -webkit-
12.0 -o- |
| animation | 43.0
4.0 -webkit- | 10.0 | 16.0
5.0 -moz- | 9.0
4.0 -webkit- | 30.0
15.0 -webkit-
12.0 -o- |
Example
@keyframes
myfirst
{
from
{
background:
red
;
}
to
{
background:
yellow
;
}
}
@-
webkit-keyframes
myfirst
/*
Safari and Chrome
*/
{
from
{
background:
red
;
}
to
{
background:
yellow
;
}
}
CSS3 Animations
Execute JavaScript when text is copied on the element: @keyframes To create an animation, bind it to a selector, otherwise the animation will have no effect.
Specify at least these two CSS3 animation properties when binding to a selector:
- Specifies the name of the animation
- Specifies the duration of the animation
Example
Bind the "myfirst" animation to the div element, with a duration of 5 seconds:
div
{
animation:
myfirst
5
s
; -
webkit-animation:
myfirst
5
s
;
/*
Safari and Chrome
*/
}
Try it »
**Note: **You must define the name and duration of the animation. If the duration is omitted, the animation will not run, because the default value is 0.
What Are CSS3 Animations?
An animation is the effect of gradually changing an element from one style to another.
You can change as many styles as you like, as many times as you like.
Use percentages to specify when the change occurs, or use the keywords "from" and "to", which are equivalent to 0% and 100%.
0% is the beginning of the animation, and 100% is its completion.
For the best browser support, you should always define the 0% and 100% selectors.
Example
Change the background color at 25% and 50%, then change it again when the animation is 100% complete:
@keyframes
myfirst
{
0%
{
background:
red
;
}
25%
{
background:
yellow
;
}
50%
{
background:
blue
;
}
100%
{
background:
green
;
}
}
@-
webkit-keyframes
myfirst
/*
Safari and Chrome
*/
{
0%
{
background:
red
;
}
25%
{
background:
yellow
;
}
50%
{
background:
blue
;
}
100%
{
background:
green
;
}
}
Try it »
Example
Change the background color and position:
@keyframes
myfirst
{
0%
{
background:
red
;
left:
0
px
;
top:
0
px
;
}
25%
{
background:
yellow
;
left:
200
px
;
top:
0
px
;
}
50%
{
background:
blue
;
left:
200
px
;
top:
200
px
;
}
75%
{
background:
green
;
left:
0
px
;
top:
200
px
;
}
100%
{
background:
red
;
left:
0
px
;
top:
0
px
;
}
}
@-
webkit-keyframes
myfirst
/*
Safari and Chrome
*/
{
0%
{
background:
red
;
left:
0
px
;
top:
0
px
;
}
25%
{
background:
yellow
;
left:
200
px
;
top:
0
px
;
}
50%
{
background:
blue
;
left:
200
px
;
top:
200
px
;
}
75%
{
background:
green
;
left:
0
px
;
top:
200
px
;
}
100%
{
background:
red
;
left:
0
px
;
top:
0
px
;
}
}
Try it »
CSS3 Animation Properties
The table below lists the @keyframes rule and all the animation properties:
| property | Description | CSS |
|---|---|---|
| @keyframes | Specifies the animation. | 3 |
| animation | Shorthand property for all the animation properties. | 3 |
| animation-name | Specifies the name of the @keyframes animation. | 3 |
| animation-duration | Specifies the number of seconds or milliseconds an animation takes to complete one cycle. The default is 0. | 3 |
| animation-timing-function | Specifies the speed curve of the animation. The default is "ease". | 3 |
| animation-fill-mode | Specifies the style to apply to the element when the animation is not playing (when it has finished, or when it has a delay and has not yet started). | 3 |
| animation-delay | Specifies when the animation will start. The default is 0. | 3 |
| animation-iteration-count | Specifies the number of times the animation is played. The default is 1. | 3 |
| animation-direction | Specifies whether the animation should play in reverse on alternate cycles. The default is "normal". | 3 |
| animation-play-state | Specifies whether the animation is running or paused. The default is "running". | 3 |
The following two examples set all the animation properties:
Example
Run the myfirst animation with all the properties set:
div
{
animation-name:
myfirst
;
animation-duration:
5
s
;
animation-timing-function:
linear
;
animation-delay:
2
s
;
animation-iteration-count:
infinite
;
animation-direction:
alternate
;
animation-play-state:
running
;
/*
Safari and Chrome:
*/
webkit-animation-name:
myfirst
; -
webkit-animation-duration:
5
s
; -
webkit-animation-timing-function:
linear
; -
webkit-animation-delay:
2
s
; -
webkit-animation-iteration-count:
infinite
; -
webkit-animation-direction:
alternate
; -
webkit-animation-play-state:
running
;
}
Try it »
Example
The same animation as above, but using the shorthand animation property:
div
{
animation:
myfirst
5
s
linear
2
s
infinite
alternate
;
/*
Safari and Chrome:
*/
webkit-animation:
myfirst
5
s
linear
2
s
infinite
alternate
;
}
Try it »