CSS Animation

CSS Animation property is used to create animation on the webpage. It can be used as a replacement of animation created by Flash and JavaScript.

CSS3 @keyframes Rule

The animation is created in the @keyframe rule. It is used to control the intermediate steps in a CSS animation sequence.

What animation does

An animation makes an element change gradually from one style to another. You can add as many as properties you want to add. You can also specify the changes in percentage.0% specify the start of the animation and 100% specify its completion.

How CSS animation works

When the animation is created in the @keyframe rule, it must be bound with selector; otherwise the animation will have no effect.

The animation could be bound to the selector by specifying at least these two properties:

  • The name of the animation
  • The duration of the animation

CSS animation properties

@keyframesIt is used to specify the animation.
animationThis is a shorthand property, used for setting all the properties, except the animation-play-state and the animation-fill- mode property.
animation-delayIt specifies when the animation will start.
animation-directionIt specifies if or not the animation should play in reserve on alternate cycle.
animation-durationIt specifies the time duration taken by the animation to complete one cycle.
animation-fill-modeit specifies the static style of the element. (when the animation is not playing)
animation-iteration-countIt specifies the number of times the animation should be played.
animation-play-stateIt specifies if the animation is running or paused.
animation-nameIt specifies the name of @keyframes animation.
animation-timing-functionIt specifies the speed curve of the animation.

CSS animation example: changing background color

Let’s see a simple CSS animation example that changes background color of rectangle from RED to BLACK.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>   
  5. div {  
  6.     width: 100px;  
  7.     height: 100px;  
  8.     background: red;  
  9.     -webkit-animation: myfirst 6s; /* Chrome, Safari, Opera */  
  10.     animation: myfirst 5s;  
  11. }  
  12. /* Chrome, Safari, Opera */  
  13. @-webkit-keyframes myfirst {  
  14.     from {background: red;}  
  15.     to {background: green;}  
  16. }  
  17. /* Standard syntax */  
  18. @keyframes myfirst {  
  19.     from {background: red;}  
  20.     to {background: green;}  
  21. }  
  22. </style>  
  23. </head>  
  24. <body>  
  25. <p><b>Note:</b> The IE 9 and earlier versions don’t support CSS3 animation property. </p>  
  26. <div></div>  
  27. </body>  
  28. </html>  

Test it Now

CSS animation example: Moving Rectangle

Let’s take another example to display animation with percentage value.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>   
  5. div {  
  6.     width: 100px;  
  7.     height: 100px;  
  8.     background: red;  
  9.     position: relative;  
  10.     -webkit-animation: myfirst 5s; /* Chrome, Safari, Opera */  
  11.     animation: myfirst 5s;  
  12. }  
  13. /* Chrome, Safari, Opera */  
  14. @-webkit-keyframes myfirst {  
  15.     0%   {background:red; left:0px; top:0px;}  
  16.     25%  {background:yellow; left:300px; top:0px;}  
  17.     50%  {background:blue; left:200px; top:300px;}  
  18.     75%  {background:green; left:0px; top:200px;}  
  19.     100% {background:red; left:0px; top:0px;}  
  20. }  
  21. /* Standard syntax */  
  22. @keyframes myfirst {  
  23.     0%   {background:red; left:0px; top:0px;}  
  24.     25%  {background:yellow; left:300px; top:0px;}  
  25.     50%  {background:blue; left:300px; top:200px;}  
  26.     75%  {background:green; left:0px; top:200px;}  
  27.     100% {background:red; left:0px; top:0px;}  
  28. }  
  29. </style>  
  30. </head>  
  31. <body>  
  32. <p><b>Note:</b> The Internet Explorer 9 and its earlier versions don’t support this example.</p>  
  33. <div></div>  
  34. </body>  
  35. </html>  

Was this article helpful?

Related Articles

Leave A Comment?