Transforms and Transitions Using CSS3 in HTML5

Introduction

 
In this article, we will learn how to build an animation website using CSS3 transformations and transitions using HTML5. This new feature allows a web developer to build and use a combination of HTML5, CSS, and JavaScript to animate objects on the web. This feature makes HTML5 more and more popular. CSS transitions allow the browsers to animate the change of CSS properties where changes to the property from an initial value to a final value. Each CSS transition and transformation properties have their own prefix that we must use depending on the browser. All of the CSS and JavaScript examples currently work only in WebKit-based browsers, such as Safari on the iPad. However, Mozilla-based browsers have their own versions of WebKit-based extensions that use the '-moz' prefix, and that should behave similarly.
 

CSS3 Transforms  

 
CSS3 Transforms have three types of properties that we can use on HTML5 elements:
  • Rotation.
  • Scaling.
  • Translation.
Transforms are applied by setting the -webkit-transform CSS property with the desired list of transforms. CSS3 Transitions  CSS3 Transitions are used to give an animation effect on HTML5 elements using the CSS properties. The drawback of CSS3 Transitions is that we have to apply a different rule depending on the browsers that we use. Therefore, we have to use all the properties or vendor's prefixes for a smoothly functioning code. This makes the program code longer.
 
The syntax used while defining the CSS3 Transitions is:
  • -webkit-transition: property_name duration:  -webkit is used for browsers that have webkit features.
  • -moz-transition: property_name duration:    -moz is used to run the code on Mozilla.
  • -o-transition: property_name duration:       -o is used for Opera browsers.
  • -transition: property_name duration :
Example 1
 
In this example we use CSS3 Transitions for changing one image to another, on hover (transitions). We define two images markup in the div tag and the CSS transitions code fade out the one image to another on mouse hover on the image.
 
This is markup code of HTML5:
  1. <body>  
  2. <div id="mydiv">  
  3.         <img class="bottom" src="Desert.jpg" />  
  4.         <img class="top" src="Chrysanthemum.jpg" />  
  5.     <br />  
  6.     <br />  
  7. </div>  
  8. </body> 
This is the CSS Code:
  1. <style>  
  2. #mydiv {  
  3.   position: relative;  
  4.   height: 281px;  
  5.   width: 450px;  
  6.   margin: 0 auto;  
  7. }  
  8.   
  9. #mydiv img {  
  10.   position: absolute;  
  11.   left: 0;  
  12.   -moz-transition: opacity 1s ease-in-out;  
  13. }  
  14.   
  15. #mydiv img.top:hover {  
  16.   opacity: 0;  
  17. }}  
  18. </style> 
Here is the full Code:
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head>  
  3.     <title></title>  
  4.     <style>  
  5.     #cf {  
  6.         position:relative;  
  7.         height:281px;  
  8.         width:450px;  
  9.         margin:0 auto;   
  10. }  
  11. #cf img {  
  12.         position:absolute;  
  13.         left:0;  
  14.         -moz-transition: opacity 1s ease-in-out;         
  15. }  
  16. #cf img.top:hover {  
  17.         opacity:0;  
  18.       }                
  19.      }  
  20.     </style>  
  21. </head>  
  22. <body>  
  23. <div id="cf">  
  24.         <img class="bottom" src="Desert.jpg" />  
  25.         <img class="top" src="Chrysanthemum.jpg" />  
  26.     <br />  
  27.     <br />  
  28. </div>  
  29. </body>  
  30. </html> 
Output
 
1.gif
 
After the mouse hovers on the image this image fades out into another image.
 
2.gif
 
Example 2 
 
In this we use Transitions and 3d Transforms to flip an image by the z-axis when the mouse hovers on the image. Here is the markup code of HTML5:
  1. <body>  
  2. <div id="Main">  
  3.         <div id="Child" class="shadow">  
  4.                         <img src="Desert.jpg" style="height: 300px; width: 466px"/>  
  5.                 </div>  
  6. </div>  
  7. </body> 
Then, the CSS code:
  1. <style>#Main {  
  2.   positionrelative;  
  3.   margin10px auto;  
  4.   width450px;  
  5.   height281px;  
  6.   z-index1;  
  7. }  
  8.   
  9. #Main {  
  10.   -moz-perspective: 1000;  
  11. }  
  12.   
  13. #Child {  
  14.   width100%;  
  15.   height100%;  
  16.   -moz-transform-style: preserve-3d;  
  17.   -moz-transition: all 1.0s linear;  
  18. }  
  19.   
  20. #Main:hover #Child {  
  21.   -moz-transform: rotateZ(180deg);  
  22.   -moz-box-shadow: -5px 5px 5px #aaa;  
  23. }  
  24.   
  25. .face {  
  26.   positionabsolute;  
  27.   width100%;  
  28.   height100%;  
  29.   -moz-backface-visibilityhidden;  
  30. }  
  31.   
  32. .face.back {  
  33.   displayblock;  
  34.   -moz-transform: rotateZ(180deg);  
  35.   -moz-box-sizing: border-box;  
  36.   padding10px;  
  37.   colorwhite;  
  38.   text-aligncenter;  
  39.   background-color#aaa;  
  40. }</style> 
Here the is full code:
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.    <head>  
  3.       <title></title>  
  4.       <style>  
  5.          #Main {  
  6.          position: relative;  
  7.          margin: 10px auto;  
  8.          width: 450px;  
  9.          height: 281px;  
  10.          z-index: 1;  
  11.          }  
  12.          #Main {  
  13.          -moz-perspective: 1000;                          
  14.          }  
  15.          #Child {  
  16.          width: 100%;  
  17.          height: 100%;    
  18.          -moz-transform-style: preserve-3d;  
  19.          -moz-transition: all 1.0s linear;        
  20.          }  
  21.          #Main:hover #Child {  
  22.          -moz-transform: rotateZ(180deg);  
  23.          -moz-box-shadow: -5px 5px 5px #aaa;  
  24.          }  
  25.          .face {  
  26.          position: absolute;  
  27.          width: 100%;  
  28.          height: 100%;  
  29.          -moz-backface-visibility: hidden;  
  30.          }  
  31.          .face.back {  
  32.          display: block;  
  33.          -moz-transform: rotateZ(180deg);  
  34.          -moz-box-sizing: border-box;  
  35.          padding: 10px;  
  36.          color: white;  
  37.          text-align: center;  
  38.          background-color: #aaa;  
  39.          }  
  40.       </style>  
  41.    </head>  
  42.    <body>  
  43.       <div id="Main">  
  44.          <div id="Child" class="shadow">  
  45.             <img src="Desert.jpg" style="height: 300px; width: 466px"/>  
  46.          </div>  
  47.       </div>  
  48.    </body>  
  49. </html> 
Output
 
3 
 
After the mouse hovers on the image, this image is flipped by the Z-axis:
 
 4