CSS3 Animated Loading Icon

Let us create css3 animated loading icons that are used to make a beautiful loading effect for your websites.

First we will create view,

  1. <div class="spinner">  
  2.     <div class="bounce1"></div>  
  3.             <div class="bounce2"></div>  
  4. </div>   
There is one Parent div having class name “spinner” and two child divs having classname “bounce1 and bounce2”.

Now its time to play with css3

I will explain each class's  properties. First we will create style for spinner (parent div),
  1. .spinner  
  2.             {  
  3.                 height: 60px;  
  4.                 width: 60px;  
  5.                 position: relative;  
  6.                 margin: 100px 0;  
  7.             }  
For child divs (bounce1 and bounce2) 
  1. .bounce1, .bounce2  
  2.             {  
  3.                 width: 100%;  
  4.                 height: 100%;  
  5.                 position: absolute;  
  6.                 top: 0px;  
  7.                 left: 0px;  
  8.                 background: #FFF;  
  9.                 opacity: 0.6;  
  10.   
  11.                 -moz-animation: spinner 2.0s infinite ease-in-out;   
  12.                 animation: spinner 2.0s infinite ease-in-out;  
  13.             }  
Here I have used n animation property for the child divs. Animation name is “spinner” with delay “2.0s”. If we set delay  for both, there won't be any difference in both children so we will set different delay for both divs.
  1. .bounce2  
  2.             {  
  3.                 animation-delay: -1.0s;  
  4.             }  
Now the bounce1 have 2.0s delya and bounce2 have -1.0s delay. You can see the difference while you are previewing the page. Now let us create the spinner animation :
  1. @keyframes spinner  
  2.             {  
  3.                 0%,100%{  
  4.                     transform: scale(0.0)  
  5.                 }  
  6.                 50%  
  7.                 {  
  8.                     transform: scale(1.0);  
  9.                 }  
  10.             }  
I am using transform:scale() propery, it will scale the object(here divs), when it is 0, it will be none,i mean height:0 and width:0, when it is 1 the height and width will be 100% of parent div. Here the object will be scaled to 0% and 100%, between these it will scale 1 (50%). here the logic is while opening the page, first bounce1 will be scaled to 0 and it will increase the size (50% scaled to 1),and it will again scaled to 0 (100%). mean while bounce2 will work opposite of this. When bounce1 is scaled to 0 ,bounce2 will be 1 and vice verca.

So the complete code is as follows,
  1. <html>  
  2.     <head>  
  3.         <title>-- CSS3 Animations --</title>  
  4.         <style>  
  5.             body  
  6.             {  
  7.                 background: #D60;  
  8.             }  
  9.             .spinner  
  10.             {  
  11.                 height: 60px;  
  12.                 width: 60px;  
  13.                 position: relative;  
  14.                 margin: 100px 0;  
  15.             }  
  16.             .bounce1, .bounce2  
  17.             {  
  18.                 width: 100%;  
  19.                 height: 100%;  
  20.                 position: absolute;  
  21.                 top: 0px;  
  22.                 left: 0px;  
  23.                 background: #FFF;  
  24.                 opacity: 0.6;  
  25.   
  26.                 -moz-animation: spinner 2.0s infinite ease-in-out;   
  27.                 animation: spinner 2.0s infinite ease-in-out;  
  28.             }  
  29.             .bounce2  
  30.             {  
  31.                 animation-delay: -1.0s;  
  32.             }  
  33.             @keyframes spinner  
  34.             {  
  35.                 0%,100%{  
  36.                     transform: scale(0.0)  
  37.                 }  
  38.                 50%  
  39.                 {  
  40.                     transform: scale(1.0);  
  41.                 }  
  42.             }  
  43.              
  44.         </style>  
  45.       
  46.     </head>  
  47.         <div class="spinner">  
  48.             <div class="bounce1"></div>  
  49.             <div class="bounce2"></div>  
  50.         </div>  
  51.     <body>  
  52.           
  53.     </body>      
  54. </html>  
I hope it is useful. Please feel free to ask if you have any doubts. 

 

Ebook Download
View all
Learn
View all