animation
Enables to animate an element. Every animation consists of the animation properties, e.g. delay and duration, and the related keyframes, which define its appearance.
Compatibility
-webkit-animationSafari 5+, Chrome 10+@-webkit-keyframes
animationFirefox 16+ Internet Explorer 10+ Opera 12.1+@keyframes
Not supported by Firefox prior to version 5, Internet Explorer prior to version 10 and Opera prior to version 12. For detailed compatibility info see caniuse.com.
- Firefox 16+
- Safari 5+, Chrome 10+
- Internet Explorer 10+
- Opera 12.1+
General description
Animation properties
Multiple animations can be specified comma separated. There is also the possibility for separated notations.
animation: 1 animationName 2 4s 3 linear 4 1.5s 5 infinite 6 alternate 7 none
- 1 The name of the animation as specified at the keyframes. Many of the available CSS properties can be animated. If set to
noneno animation is executed. - 2 The duration of the animation, in this case it lasts
4seconds. Defaults to0meaning that there is no animation at all (which is actually senseless). Negative values are not allowed. - 3 Optional. The timing-function, which describes how the animation will proceed over its duration. It applies to every single keyframe and not the whole animation and is specified using a bezier curve (and can also be set as one). In the current case the animation is played
linearwithout an acceleration or a slowdown. Defaults toeasewhich results in a fast beginning and a slowdown at the end.ease-outis quite the same but not so fast at the beginning.ease-inaccelerates the animation smoothly at the beginning with no slowdown at the end. At lastease-in-outstands for an acceleration at the beginning and a slowdown at the end.
There is also the possibility of step-values, which let the animation jump between the individual keyframes, wherebystep-startomits the first keyframe andstep-endthe last one.steps(X, start/end)in turn enables to define how many steps are shown between each keyframe, whereXstands for the steps andstart/enddefines if the first or the last keyframe should be omitted (defaults toendif omitted). - 4 Optional. The animation starts with a delay of
1.5seconds. If not set or set to0the animation will begin immediately, if negative it will begin part-way through its play cycle. - 5 Optional. The iteration-count defines how often the animation is played. The default value is
1, meaning that it will play from the beginning to the end once. A value ofinfinitemakes the animation to run forever, decimal values cause it to end in between its play cycle. - 6 Optional. The direction of the animation. If set to
alternateevery second iteration of the animation is played back to front. Defaults tonormal, meaning that every play cycle starts from the beginning. - 7 Optional. The fill mode. If set to
forwards, the last keyframe remains at the end of the animation, as far as the iteration-count (5) is uneven. If set tobackwardsand a delay (4) is specified likewise, the first keyframe is shown at the start of the animation instead of the normal appearance of the element.bothcombines these two value, the standard valuenoneomits them.
Keyframes
@keyframes 1animationName {
20% {top: 0px}
333% {top: 50px; 4animation-timing-function: ease-out}
5100% {top: 100px; left: 10px}
}
- 1 The name of the animation. Links the keyframes to the animation itself and must be the same as 1 at the animation properties.
- 2 A keyframe (as well as 3 and 4). Consits on one side of a percentage value – which determines the placement of the keyframe in the animation -, as well as one or more CSS properties and values that define the state of the animation at this point.
In this case it’s the starting point of the animation, since it has a percentage value of0(can also be defined by the keywordfrom). At this point thetopposition of the element is0px. - 3 Optional. At
33%of the animation’s time the element is at atopposition of50px, which brought it a bit downwards. - 4 Optional. Additionally it is possible to set a timing-function per keyframe which overwrites the general timing function set at 3 at the animation properties. Also takes the same values.
- 5 The end of the animation. It’s also possible to use the keyword
toinstead of100%. Finally thetopvalue of the element changes to80px, moving it further down. Furthermore it has adopted aleftposition of10pxwhich moved it a bit right.
Separated notations
Sets the same animation properties like above.
animation-name: animationName
animation-duration: 4s
animation-timing-function: linear
animation-delay: 1.5s
animation-iteration-count: infinite
animation-direction: alternate
animation-fill-mode: none
Example
Two animations specified at once
animation: 1 changeColor 3s infinite, 2 changeSize 2s ease-in 1s infinite
3@keyframes changeColor {
0% {
background-color: red;
border-color: black;
}
100% {
background-color: green;
border-color: yellow;
}
}
4@keyframes changeSize {
0% {transform: scale(1)}
65% {transform: scale(1.2)}
100% {transform: scale(1.4)}
}
- 1 The first animation for the element has a duration of 3 seconds for each iteration and repeats infinitely. Since there is no timing-function set it defaults to
linear. - 2 The second animation lasts 2 seconds and has a timing-function of
ease-in(smooth acceleration at the beginning, no slowdown at the end). It starts after a delay of 1 second and together with the duration of 2 seconds it also last 3 seconds. - 3 The keyframes for the first animation: The element starts with a
background-colorofredas well as aborder-colorofblackand finishes with abackground-colorofgreenand aborder-colorof yellow at the end. - 4 The keyframes for the second animation, which starts at the original size of the element, adopts
1.2times the size at65%and ends with1.4times the size.
Blinking animation
animation: 1 blink 1s step-end infinite
2@keyframes blink {
0% {background-color: blue}
50% {background-color: black}
}
- 1 One iteration of the animation lasts one second and changes immeditately between its keyframes without a transition. It runs infinitely.
- 2 The
background-colorchanges from blue to black.
Further reading
For more info see the W3C site, the Safari site and the site at MDN (including compatibility info for older browser versions).
