Animation in CSS - CN
Animation in CSS - CN
Overview
CSS Animation property is used to create animation on the webpage. It can be used as a
replacement for animation created by Flash and JavaScript. An animation lets an
element gradually change from one style to another.
Property Description
1
@keyframes rule
Animation is created with a specific name and then its functionality is described using
the @keyframes rule. We need to mention the animation from and to.
Let’s take an example where a square div changes its shape to a circle.
@keyframes sample{
from{border-radius: 0;}
to{border-radius: 50%;}
}
Before : After :
Animation at stages
In the above example, we transformed a square to a circle with animation that you
cannot see in this doc but you must try the code in your editor and see the results on
the browser. We can also make multiple changes in animation with time
Let’s see that with an example where the animation duration is 3 seconds and at each
second colour of the div changes.
2
CSS : div {
width: 100px;
height: 100px;
background-color: red;
animation-name: sample;
animation-duration: 3s;
}
@keyframes sample {
0% {background-color: red;}
50% {background-color: blue;}
100% {background-color: green;}
}
animation-iteration-count
This property specifies the number of times animation should play.
In the below example the square changes to circle 3 times just like a loop.
@keyframes sample{
from{border-radius: 0;}
to{border-radius: 50%;}
}
3
Animation direction
This property specifies if or not the animation should play in reserve on an alternate
cycle or normally.
In the below example technically square should have been converted into a circle but
after applying the property “animation-direction : reverse”, the circle is converted to
a sqaure.
CSS : div{
height: 100px;
width: 100px;
border:2px solid black ;
animation-name: sample ;
animation-duration: 5s ;
animation-direction : reverse ;
}
@keyframes sample{
from{border-radius: 0;}
to{border-radius: 50%;}
}
● ease : Animation with a slow start, then fast, then end slowly (Default)
● linear : Animation with the same speed from start to end
● ease-in : Animation with a slow start
● ease-out : Animation with a slow end
● ease-in-out - Animation with a slow start and end
● cubic-bezier(n,n,n,n) : Define own values in a cubic-bezier function
4
Animation Shorthand Property
Example :