CSS – Animations

In CSS, we can easily perform animations on an element. Animation in this context means changing the style of an element. Remember, for Animations in CSS, we do not need Flash or JavaScript.

What is the CSS @keyframes Rule?

For animations in CSS, we use the concept of @keyframes. The @keyframes is a rule wherein you need to specify the CSS styles. Bind the animation to an element for it to work successfully.

Use the animation-name property to set the name of the @keyframes animation. The duration of the animation is set using the animation-duration property. Similarly, we have the following properties that will help you in performing the task of animations on a web page:

  • animation
  • animation-name
  • animation-duration
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-timing-function
  • animation-fill-mode

Let us now see the following animation examples:

  1. Change the background color
  2. Change the background color multiple times
  3. Animate and move the rectangular div
  4. Play animation in reverse order

CSS Animations Example – Change the background color

In this example, we will set animation. We have set the animation-duration property below to set the duration of the animation. The default is 0 seconds i.e., no animation. The from and to keywords are used in the below example to set the beginning and end.

Let us now see an example to change the background color:

Output

After 3 seconds, the above div will animate and the color will change:

CSS Animations Change the background color

Change the background color multiple times

In this example, we will change the background color of a div on a web page multiple times. The color will change and using the animation-duration property the total duration of the animation is set. Let us now see an example:

Output

First, the color changes to blue from yellow.

Then, the color changes to green from blue.

Then, the color changes to orange from green.

Then, the color changes to navy from orange.

Then, the color changes to cyan from navy:

CSS Animations Change the background color multiple time

Animate and move the rectangular div

In this example, we will move a rectangular div:

Output

After a second, the box will animate:

After a second, the box will animate again:

CSS Animate and move the rectangular div

Play animation in reverse order

To play the animation in reverse order, we will use the animation-direction property. With this, we can play animation forwards and backward alternatively.

The following can be the values of the animation-direction property:

  • normal: The animation is played forward i.e., the normal default way.
  • reverse: The animation is played backward
  • alternate: The animation is played forwards first, then backward
  • alternate-reverse: The animation is played backward first, then forwards i.e., the reverse order.

Let us now see an example. We have used the animation property as well. This is a shorthand property for animation:

Output

CSS Animations Play animation in reverse order

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


Read More:

CSS - Buttons
CSS Cheat Sheet by Studyopedia
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading