CSS Animations
CSS Animations
@keyframes
animation-name
animation-duration
animation-delay
animation-iteration-count
animation-direction
animation-timing-function
animation-fill-mode
animation
You can change as many CSS properties you want, as many times as you want.
To use CSS animation, you must first specify some keyframes for the animation.
Keyframes hold what styles the element will have at certain times.
The following example binds the "example" animation to the <div> element.
The animation will last for 4 seconds, and it will gradually change the
background-color of the <div> element from "red" to "yellow":
CSS Animation
CSS Animation property is used to create animation on the webpage. It
can be used as a replacement of animation created by Flash and JavaScript.
CSS3 @keyframes Rule
The animation is created in the @keyframe rule. It is used to control the
intermediate steps in a CSS animation sequence.
Animation This is a shorthand property, used for setting all the properties, except th
animation-play-state and the animation-fill- mode property.
animation- It specifies if or not the animation should play in reserve on alternate cycle.
direction
animation- It specifies the time duration taken by the animation to complete one cycle.
duration
animation-fill- it specifies the static style of the element. (when the animation is no
mode playing)
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. div {
6. width: 100px;
7. height: 100px;
8. background: red;
9. -webkit-animation: myfirst 6s; /* Chrome, Safari, Opera */
10. animation: myfirst 5s;
11. }
12./* Chrome, Safari, Opera */
13. @-webkit-keyframes myfirst {
14. from {background: red;}
15. to {background: green;}
16.}
17. /* Standard syntax */
18.@keyframes myfirst {
19. from {background: red;}
20. to {background: green;}
21. }
22.</style>
23. </head>
24.<body>
25. <p><b>Note:</b> The IE 9 and earlier versions don't support C
SS3 animation property. </p>
26.<div></div>
27. </body>
28.</html>
CSS animation example: Moving Rectangle
Let's take another example to display animation with percentage value.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. div {
6. width: 100px;
7. height: 100px;
8. background: red;
9. position: relative;
10. -webkit-animation: myfirst 5s; /* Chrome, Safari, Opera */
11. animation: myfirst 5s;
12.}
13. /* Chrome, Safari, Opera */
14.@-webkit-keyframes myfirst {
15. 0% {background:red; left:0px; top:0px;}
16. 25% {background:yellow; left:300px; top:0px;}
17. 50% {background:blue; left:200px; top:300px;}
18. 75% {background:green; left:0px; top:200px;}
19. 100% {background:red; left:0px; top:0px;}
20.}
21. /* Standard syntax */
22.@keyframes myfirst {
23. 0% {background:red; left:0px; top:0px;}
24. 25% {background:yellow; left:300px; top:0px;}
25. 50% {background:blue; left:300px; top:200px;}
26. 75% {background:green; left:0px; top:200px;}
27. 100% {background:red; left:0px; top:0px;}
28.}
29. </style>
30.</head>
31. <body>
32.<p><b>Note:</b> The Internet Explorer 9 and its earlier versions don't sup
port this example.</p>
33. <div></div>
34.</body>
35. </html>
Syntax
1. @keyframes animation-name {keyframes-selector {css-styles;}}
Property values
If the keyframe rule does not define the start and end states of animation,
then the browsers will use the existing styles of the element. The properties
get ignored that cannot be animated in keyframe rules.
linear: It means that the transition rate will be constant from start to end.
ease: It means that the animation starts slowly, and then after a time
period, the speed increases, and before end speed will again slow down.
Example
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. div
6. {
7. width:200px;
8. height:200px;
9. animation:demo 5s ease-in infinite, trans 5s ease-in-out infinite;
10.border-radius:40px;
11. }
12.
13. @keyframes demo
14.{
15. 0% {background:red;}
16. 50% {background:yellow; width:100px; height:100px;}
17. 100% {background:green; width:300px; height:300px;}
18.}
19. @keyframes trans
20.{
21. 0% {transform:translate(0px) scale(1.4) rotate(80deg);}
22. 50% {transform:translate(250px) scale(1.2) rotate(40deg);}
23. 100% {transform:translate(350px) scale(1) rotate(0deg);}
24.}
25.
26.</style>
27. </head>
28.<body>
29.
30.<div></div>
31. <p>After the completion of the Animation, the element retracts
to its original State </p>
32.
33. </body>
34.</html>
Example2
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h1 {
6. color: black;
7. text-align: center;
8. }
9. div {
10. position: relative;
11. animation: jtp 7s infinite;
12. }
13.
14. @keyframes jtp {
15. 0% {
16. top: 500px;
17. width: 0px;
18. font-size:10px;
19. transform: translate(0px) scale(1.4) rotate(80deg);
20. }
21. 25% {
22. top: 400px;
23. background: yellow;
24. font-size:20px;
25. width: 50px;
26. transform: translate(100px) scale(1.3) rotate(60deg);
27. }
28. 50% {
29. top: 300px;
30. background: orange;
31. font-size:30px;
32. width: 150px;
33. transform: translate(200px) scale(1.2) rotate(40deg);
34. }
35. 75% {
36. top: 200px;
37. background: pink;
38. width: 250px;
39. font-size:40px;
40. transform: translate(300px) scale(1.1) rotate(20deg);
41. }
42. 100% {
43. top: 100px;
44. background: red;
45. font-size:50px;
46. width: 500px;
47. transform: translate(400px) scale(1) rotate(0deg);
48. }
49. }
50. </style>
51. </head>
52.
53. <body>
54. <div>
55. <h1>javaTpoint</h1>
56. </div>
57. </body>
58.
59. </html>
Points to remember
Some of the important points about this rule are given as follows:
The following example will run the animation in reverse direction (backwards):
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-direction: reverse;
@keyframes example {
</style>
</head>
<body>
<h1>CSS Animation</h1>
<div></div>
</body>
</html>
Example2
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 2;
animation-direction: alternate;
@keyframes example {
</style>
</head>
<body>
<h1>CSS Animation</h1>
<div></div>
</body>
</html>
Specify the Speed Curve of the
Animation
The animation-timing-function property specifies the speed curve of the
animation.
ease - Specifies an animation with a slow start, then fast, then end slowly
(this is default)
linear - Specifies an animation with the same speed from start to end
ease-in - Specifies an animation with a slow start
ease-out - Specifies an animation with a slow end
ease-in-out - Specifies an animation with a slow start and end
cubic-bezier(n,n,n,n) - Lets you define your own values in a cubic-
bezier function
The following example shows some of the different speed curves that can be
used:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 50px;
background-color: red;
font-weight: bold;
position: relative;
@keyframes mymove {
to {left: 300px;}
</style>
</head>
<body>
<h1>CSS Animation</h1>
<p>The animation-timing-function property specifies the speed curve of the animation. The following
example shows some of the different speed curves that can be used:</p>
<div id="div1">linear</div>
<div id="div2">ease</div>
<div id="div3">ease-in</div>
<div id="div4">ease-out</div>
<div id="div5">ease-in-out</div>
</body>
</html>
Specify the fill-mode For an
Animation
CSS animations do not affect an element before the first keyframe is played or
after the last keyframe is played. The animation-fill-mode property can override
this behavior.
The animation-fill-mode property specifies a style for the target element when
the animation is not playing (before it starts, after it ends, or both).
none - Default value. Animation will not apply any styles to the element
before or after it is executing
forwards - The element will retain the style values that is set by the last
keyframe (depends on animation-direction and animation-iteration-count)
backwards - The element will get the style values that is set by the first
keyframe (depends on animation-direction), and retain this during the
animation-delay period
both - The animation will follow the rules for both forwards and
backwards, extending the animation properties in both directions
The following example lets the <div> element retain the style values from the
last keyframe when the animation ends:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-fill-mode: forwards;
}
@keyframes example {
</style>
</head>
<body>
<h1>CSS Animation</h1>
<p>Let the div element retain the style values set by the last keyframe when the animation ends:</p>
<div></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
body
{
background-color: gray;
}
ul{
display: flex;
list-style: none;
margin-left: 550px;
margin-top: 300px;
}
ul > li
{
font-size: 60px;
letter-spacing: 10px;
font-family: sans-serif;
color: white;
text-shadow: 1px 1px 4px white;
animation-name: apply;
animation-duration: 4s;
animation-iteration-count: infinite;
font-weight: bold;
animation-timing-function: ease-in;
}
@keyframes apply {
0%{
color: rgb(89, 0, 255);
}
25%
{
transform: translateX(-50px);
letter-spacing: 35px;
color: rgb(0, 255, 34);
opacity: 0.5;
}
70%{
transform: translateX(50px);
color: rgb(247, 14, 169);
opacity: 0.3;
}
100%
{
color: blue;
}
}
ul > li:nth-child(1)
{
animation-delay: 0s;
}
ul > li:nth-child(2)
{
animation-delay: 0.4s;
}
ul > li:nth-child(3)
{
animation-delay: 0.8s;
}
ul > li:nth-child(4)
{
animation-delay: 1.2s;
}
ul > li:nth-child(5)
{
animation-delay: 1.6s;
}
ul > li:nth-child(6)
{
animation-delay: 2.0s;
}
ul > li:nth-child(7)
{
animation-delay: 2.4s;
}
</style>
</head>
</head>
</head>
<body>
<ul>
<li>W</li>
<li>E</li>
<li>L</li>
<li>C</li>
<li>O</li>
<li>M</li>
<li>E</li>
</ul>
</body>
</html>
Hurt Animation
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
</title>
<style type="text/css">
.heart
{
border: 1px solid red;
width: 150px;
height: 150px;
background-color: red;
margin-left: 450px;
margin-top: 300px;
position: relative;
transform: rotate(45deg);
animation-name: heartbeat;
animation-duration: 1.3s;
animation-iteration-count: infinite;
}
.heart::before
{
position: absolute;
content: "";
border: 1px solid red;
width: 150px;
height: 150px;
background-color: red;
transform: translateY(-75px);
border-radius: 50%;
}
.heart::after
{
position: absolute;
content: "";
border: 1px solid red;
width: 150px;
height: 150px;
background-color: red;
transform: translateX(-75px);
border-radius: 50%;
}
@keyframes heartbeat {