Skip to content
Related Articles

Related Articles

How to make CSS Animations ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 26 Mar, 2021

Animation is a way using which we can create the illusion of motion by placing still images one after another in a typical format. For example, we can have a ball rising up at some instant than falling down as an animation effect. CSS provides us some properties to control the animation effect by changing some of its variables like timing and keyframes etc. 

Some CSS properties are as follows:

Along with all these methods, there are some other methods also such as : animation-timing-function, @keyframes, animation-fill-mode etc.

Explanation: Basically in our CSS code first we have to specify the main effect which our animation should display by using “@keyframes” property. Then inside this block, we have to write the effect of animation i.e the change in size, from which color to which color changes must happen, change in opacity, and  many more. These all can be mentioned in terms of percentage of the total time slot or using the “from” and “to” keywords. Hence, this block contains the main animation code to be displayed.

Below are examples of creating animations. The first animation results in a change of opacity and the second one results in a change of background-color.

  • Example 1:


    @keyframes animation_example1{
      /* animation results in change of opacity*/

  • Example 2: In this code we created animations but to link these animations’ effect to the HTML tags (maybe img or other ones) we have to specify their name in the CSS style of that tag.


    @keyframes animation_example2{
      /* here the amount of total time is divided */

  • Example 3: In this code we are able to mention the details of the animation like the timing and delay period and also the iteration counts etc


    }/* This animation will continue 
        infinite number of times */

Below is the complete HTML code for which the above-mentioned CSS will be applied to produce the results.


      @keyframes animation_example2{
        /* here the amount of total time is divided */
      /* This animation will continue 
         infinite number of times */
        animation-name: animation_example2;
        animation-iteration-count: infinite;
  <div id="gfg" 
      border:1px solid #000;



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!