Understanding CSS3 Translation Methods

Introduction

 
In this article, we will learn about the translate method of CSS3. It's part of CSS3 2D and 3D transformations. This method comes with five variants out of which three methods are for 2D transform and the other two are for 3D transforms. We will learn about the following five variants of the translate method:
  1. Translation on X-axes: translateX(K)
  2. Translation on Y-axes: translateY(K) 
  3. Translation on X/Y-axes: translate(K1,K2) 
  4. Translation on Z-axes: translateZ(K) 
  5. Translation on X/Y/Z-axes: translate3d(K1,K2,K3)
Note that the sample code in this article will only work in the Chrome browser but see the bottom of this article for prefixes for Internet Explorer and Firefox. So let's start our discussion with the first variant of the translate method.
 

Translation on X-axes: translateX(K)

 
Translation stands for shifting an element by some distance. Let that distance be K. When the element is transferred along the X-axes it is known as a translation along the X-axes. It is similar to an absolute position with the left offset equal to K. K is any number given in pixels and it can vary from negative integers to positive integers.
 
Example 1
 
<div id="box-X"></div>
 
CSS
  1. #box-X{  
  2.   width:100px;  
  3.   height:100px;  
  4.   border:1px solid black;  
  5.   position:absolute;  
  6.   top:40px;  
  7.   left:40px;  
  8.   -webkit-transition:all 0.5s;  
  9. }  
  10.    
  11. #box-X:hover{  
  12.   -webkit-transform:translateX(200px);  

On running the preceding example we will see the translation of the box along the X-axes from the origin (0,0) to (200,0), in other words, translation by an amount of 200px along the X-axes. The same can also be done by setting the left property to 200px. The output of the code above is as follows
 
 

Translation on Y-axes: translateY(K)

 
This variant of the method is used for translating the element along the Y-axes. The element's X-coordinate remains the same but Y-coordinates change by an amount equal to K where K is an integer.
 
Example 2
 
<div id="box-X"></div>
 
CSS
  1. #box-X{  
  2.   width:100px;  
  3.   height:100px;  
  4.   border:1px solid black;  
  5.   position:absolute;  
  6.   top:40px;  
  7.   left:40px;  
  8.   -webkit-transition:all 0.5s;  
  9. }  
  10. #box-X:hover{  
  11.   -webkit-transform:translateY(200px);  
  12. }  
On executing the code above we will see the translation of the box along the Y-axes. The box will be translated from (0,0) to (0,200), in other words, a translation by an amount of 200px along the Y-axes. The same effect can be done by setting the "top" property equal to 200px.
 
 

Translation on X/Y-axes: translate(K1, K2)

 
This variant of method allows us to translate any element on the X-Y plane. Each X, Y coordinate can be translated independently of each other by an amount of K1 and K2 respectively.
 
Example 3
 
CSS
  1. #box-X{  
  2.   width:100px;  
  3.   height:100px;  
  4.   border:2px solid red;  
  5.   position:absolute;  
  6.   top:40px;  
  7.   left:40px;  
  8.   -webkit-transition:all 0.5s;  
  9. }  
  10. #box-X:hover{  
  11.   -webkit-transform:translate(200px,200px);  

On executing the code above, the element will be translated on the X-Y plane by an amount of 200px along the X-axes and 200px along the Y-axes. It is the same as the translation of an element from (0,0) to (200,200). The same can be done by setting both the "left" and "top" properties.
 
 
 

Translation on Z-axes: translateZ(K)

 
This variant of translation is mainly used in 3D transforms. It causes an element to translate along the Z-axes by an amount equal to K. To make this effect visible we need to enable the 3D settings of CSS3. This can be done by setting the "transform-style" to "preserve-3d" and perspective distance.
 
Example 4
 
CSS
  1. body{  
  2.   -webkit-perspective:1000px;  
  3. }  
  4. #box-X{  
  5.   width:100px;  
  6.   height:100px;  
  7.   border:2px solid red;  
  8.   position:absolute;  
  9.   top:40px;  
  10.   left:80px;  
  11.   -webkit-transform-style:preserve-3d;  
  12.   -webkit-transition:all 0.5s;  
  13. }  
  14. #box-X:hover{  
  15.   -webkit-transform:translateZ(100px);  

On executing the preceding we will see an element raised towards the user. This is because the element is translated along the Z-axes. The translation of the element on the Z-axes causes the distance between the user's eye and element to decrease and this decrement in distance causes the element to look larger than the original. In the code above the element center is transferred from (0,0,0) to (0,0,50). This effect cannot be done using absolute position settings.
 

Translation on X/Y/Z-axes: translate3d(K1,K2,K3)

 
This variant of translation is also from 3D transformations. By using this method the user can translate the element on the X-Y-Z plane along the X-axes, Y-axes, and Z-axes by an amount equal to K1, K2, and K3 respectively. This effect requires you to enable the 3D effect on the web page as we did in the previous method.
 
Example 5
 
HTML
  1. <div id="camera">  
  2. <div id="stub1"></div>  
  3. <div id="box-X"></div>  
  4. </div> 
CSS
  1. #camera{  
  2.   -webkit-perspective:1000px;  
  3. }  
  4. #box-X{  
  5.   -webkit-transform-style:preserve-3d;  
  6.   width:100px;  
  7.   height:100px;  
  8.   border:2px solid red;  
  9.   position:absolute;  
  10.   top:40px;  
  11.   left:40px;  
  12.   background-color:rgba(50,50,50,0.5);  
  13.   -webkit-transition:all 0.5s;  
  14.  -webkit-transform:rotateX(85deg);  
  15. }  
  16.    
  17. #stub1{  
  18.   position:absolute;  
  19.   top:40px;  
  20.   left:40px;  
  21.   width:200px;  
  22.   height:200px;  
  23.   border:1px solid black;  
  24.   background-color:rgba(50,50,50,0.5);  
  25.  -webkit-transform:rotateX(85deg);  
  26. }  
  27. #box-X:hover{  
  28.   -webkit-transform:rotateX(85deg)   translate3d(100px,50px,-50px);  

On executing the code above the box-x coordinate will be translated from (0,0,50) to (100,50,0). This effect can't be done using an absolute position or any other setting.
 
 
 
Demo List

Summary

 
That's all for this article. I hope you have enjoyed reading this article and found it useful. In case of any doubt feel free to ask in the comments section.
 
Note: the code above will work in the Chrome browser only. Since CSS3 is new and all of its features are not implemented yet, you need to use a browser-specific prefix if you are testing the code on various browsers. Some of the prefix codes are as follows:
  • Chrome  -webkit-
  • Internet Explorer -ie-
  • Firefox browser -moz-


Similar Articles