An animation is the process of moving any drawing, image or changing it’s styles during movement. The best example of animation is cartoon characters.
This post describes about animation in CSS, how to apply animation to HTML elements with examples.
CSS animation |
What is animation in CSS?
In CSS animation, CSS styles of an html element change from one to another. For example, changing background color of an html element, moving element one direction to another,etc...
CSS animation includes @keyframes rules and properties of animation.
For css animation @keyframes rules specification is must necessary.
What is @keyframes rules in css animation?
@keyframes rules is most important part of css animation.
It contains animation name which you want like to give. This animation name is also declare in element’s styles. CSS styles changes at some period of time.
To make changes in styles “from” and “to” keywords are added before css property.
The “from” indicates the starting phase of animation and “to” indicates the ending phase of animation.
Let’s understand with example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!Doctype html> <html> <head> <title>Animation</title> <style> @keyframes sizechange{ from{font-size:26pt;} to{font-size:36pt;} } p{ margin:50px; color:blue; font-size:26pt; animation-name:sizechange; animation-duration:4s;} </style> </head> <body> <p>This is animation example.</p> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!Doctype html> <html> <head> <title>Animation</title> <style> @keyframes shapechange{ 0%{border-radius:10px;} 30%{border-radius:50px;} 60%{border-radius:100px;} 100%{border-radius:150px;}} div{ height:300px; width:300px; margin:50px; background-color:magenta; border-radius:10px; animation-name:shapechange; animation-duration:5s;} </style> </head> <body> <div></div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!Doctype html> <html> <head> <title>Animation</title> <style> @keyframes textanimate{ 0%{color:blue;margin-left:20px;margin-top:0px;} 40%{color:red;margin-left:50px;margin-top:30px;} 70%{color:yellow;margin-left:100px;margin-top:0px;} 100%{color:green;margin-left:150px;margin-top:100px;}} h1{ color:blue; animation-name:textanimate; animation-duration:5s; } </style> </head> <body> <h1>Animation</h1> </body> </html> |
In output color and margin of text changing.
(1) animation-name
(2) animation-duration
(3) animation-delay
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!Doctype html> <html> <head> <title>Animation</title> <style> @keyframes widthadd{ from{width:300px;} to{width:700px;}} .demo{ height:400px; width:300px; background:linear-gradient(to right,blue,red); animation-name:widthadd; animation-duration:5s; animation-delay:2s; } </style> </head> <body> <div class="demo"></div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!Doctype html> <html> <head> <title>Animation</title> <style> @keyframes widthadd{ from{width:300px;} to{width:700px;}} .demo{ height:400px; width:300px; background:linear-gradient(to right,blue,red); animation-name:widthadd; animation-duration:5s; animation-delay:-2s; } </style> </head> <body> <div class="demo"></div> </body> </html> |
Here duration of animation is 5 seconds, but animation starts after completion of 2 seconds in animation. (In animation 2 seconds already completed)
(4) animation-iteration-count
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!Doctype html> <html> <head> <title>Animation</title> <style> @keyframes widthadd{ from{width:300px;} to{width:700px;}} .demo{ height:400px; width:300px; background-color:blue; animation-name:widthadd; animation-duration:5s; animation-iteration-count:5; } </style> </head> <body> <div class="demo"></div> </body> </html> |
(5) animation-direction
..... ..... @keyframes demo{ 0%{background-color:slateblue;} 30%{background-color:tomato} 60%{background-color:lime;} 100%{background-color:purple;}} div{ width:200px; height:200px; background-color:slateblue; animation-name:demo; animation-duration:5s; animation-direction:reverse; } ... ... ... <div></div> .... ....
(6) animation-timing-function
..... ..... @keyframes speed{ 0%{top:10px;left:10px;} 50%{top:10px;left:200px;} 100%{top:100px;left:100px;}} .timing{ position:absolute; height:200px; width:200px; background-color:green; animation-name:speed; animation-duration:5s; animation-timing-function:linear; ... ... ... <div class="timing"></div> .... ....
..... ..... @keyframes speed{ 0%{top:10px;left:10px;} 50%{top:10px;left:200px;} 100%{top:100px;left:100px;}} .timing{ position:absolute; height:200px; width:200px; background-color:green; animation-name:speed; animation-duration:5s; animation-timing-function:cubic-bezier(0.3,0.5,0.7,0.3); ... ... ... <div class="timing"></div> .... ....
(7) animation-fill-mode
..... ..... @keyframes demo{ 0%{background-color:slateblue;} 30%{background-color:tomato;} 60%{background-color:lime;} 100%{background-color:purple;}} div{ width:400px; height:400px; background-color:slateblue; animation-name:demo; animation-duration:5s; animation-fill-mode:forwards; } ... ... ... <div class="demo"></div> .... ....
(8) animation-play-state
..... ..... @keyframes speed{ 0%{top:0px;left:400px;} 50%{top:200px;left:400px;} 100%{top:200px;left:0px;}} .timing{ position:absolute; height:400px; width:400px; background-color:green; animation-name:speed; animation-duration:5s; animation-timing-function:linear; animation-play-state:paused; ... ... ... <div class="timing"></div> .... ....
0 Comments