CSS Animations enable the front-end developer to animate HTML elements which in turn give your website a fun and cool look. In this post, we will take a deep dive into how to use animations in CSS. When you are done, you should have a good understanding of the topic. Try typing the code into your code editor and see how it works. This will help the concepts sink in deeper. Now let us take a look at this example.
To use animations we have to use @keyframes. Keyframes are what CSS use to make animations.
@keyframes variable_name
{
0% {
opacity: 0;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
.class_name
{
animation-name: variable_name
animation-duration: 3s;
animation-delay: 3s;
animation-iteration-count: 3;
animation-timing-function: ease-in / ease-out / ease-in-out / linear / cubic-bezier(0, 0, 1, 1);
animation-play-state: paused / running;
animation-direction: normal / reverse / alternate / alternate-reverse
animation-fill-mode: none / forwards / backwards;
}
To make the @keyframes usable, you can declare it inside a class using the animation_name property name. This invokes the @keyframes that you have just created.
These two CSS properties are the only ones that are required to fully use @keyframes.
The animation-iteration-count specifies the number of times the @keyframes will happen.
The animation-timing-function describes how fast or how slow the parameters specified in the @keyframes will happen over time. Its sub-properties are:
The animation-play-state has the option of the paused and running
value. The running value makes the animation proceed as normal. The
paused value makes the animation pause and when the value is set back to running it continues from where it was left off.
The animation-direction determines whether an animation should play forwards, backwards or alternating back and forth.
Then animation-fill-mode displays the animation styles before and after the animation plays. Its values are:
There is a shortcut to declare animation property values. This is the
preferred arrangement of values when using the shortcut.
animation: animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction animation-fill-mode animation-play-state.
So we have taken a good look at animations. You have learned how to use
@keyframes and the different sub-properties of animation. It isn’t
difficult. Keep using them and they will seem easier with time.