CSS Transforms

CSS 2D Transforms Tutorials and examples on inline, file, selector, background, border, display, float, font, margin, opacity, overflow, padding, position, text-align.

CSS Transforms

CSS Transforms

CSS3 supports transform property. This transform property facilitates you to translate, rotate, scale, and skews elements.

Transformation is an effect that is used to change shape, size and position.

There are two type of transformation i.e. 2D and 3D transformation supported in CSS3.


CSS 2D Transforms

The CSS 2D transforms are used to re-change the structure of the element as translate, rotate, scale and skew etc.

Following is a list of 2D transforms methods:

  • translate(x,y): It is used to transform the element along X-axis and Y-axis.
  • translateX(n): It is used to transform the element along X-axis.
  • translateY(n): It is used to transform the element along Y-axis.
  • rotate(): It is used to rotate the element on the basis of an angle.
  • scale(x,y): It is used to change the width and height of an element.
  • scaleX(n): It is used to change the width of an element.
  • scaleY(n): It is used to change the height of an element.
  • skewX(): It specifies the skew transforms along with X-axis.
  • skewY():It specifies the skew transforms along with Y-axis.
  • matrix(): It specifies matrix transforms.

Supporting browsers

Property  Chrome  IE  Firefox  Opera  Safari
transform
36.04.0 -webkit-
10.09.0 -ms-
16.03.5 -moz-

23.0

15.0 -webkit-

12.1

10.5 -o-
9.03.2 -webkit-

transform-origin

(two-value syntax)

36.0

4.0 -webkit-

10.0

9.0 -ms-
16.03.5 -moz-
9.03.2 -webkit-

23.0

15.0 -webkit-

12.110.5 -o-

The translate() method

The CSS translate() method is used to move an element from its current position according to the given parameters i.e. X-axis and Y-axis.

Let's take an example to translate a

element 50 pixels right, and 100 pixels down from its current position.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">  
  4. <style>  
  5. div {  
  6.     width: 300px;  
  7.     height: 100px;  
  8.     background-color: lightgreen;  
  9.     border: 1px solid black;  
  10.     -ms-transform: translate(100px,80px); /* IE 9 */  
  11.     -webkit-transform: translate(100px,80px); /* Safari */  
  12.     transform: translate(100px,80px); /* Standard syntax */  
  13. }  
  14. </style>  
  15. </head>  
  16. <body>  
  17. <div>  
  18. This div element is translated 50 pixels right, and 100 pixels down from its current position by using translate () method.  
  19. </div>  
  20. </body>  
  21. </html> 

The rotate() method

The CSS rotate() method is used to rotate an element clockwise or anti-clockwise according to the given degree.

Let's take an example to rotate a

element by 300.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. div {  
  6.     width: 300px;  
  7.     height: 100px;  
  8.     background-color: lightpink;  
  9.     border: 1px solid black;  
  10. }  
  11. div#myDiv {  
  12.     -ms-transform: rotate(30deg); /* IE 9 */  
  13.     -webkit-transform: rotate(30deg); /* Safari */  
  14.     transform: rotate(30deg); /* Standard syntax */  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div>  
  20. This a normal div element.  
  21. </div>  
  22. <div id="myDiv">  
  23. This is the 30 degree clockwise rotated div element by using rotate() method.    
  24. </div>  
  25. </body>  
  26. </html> 

The scale() method

The CSS scale() method is used to increase or decrease the size of an element according to the given width and height.

Let's take an example to increase the size of an

element by increasing its width and height two times.

strong>See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. div {  
  6.     margin: 150px;  
  7.     width: 200px;  
  8.     height: 100px;  
  9.     background-color: lightpink;  
  10.     border: 1px solid black;  
  11.     border: 1px solid black;  
  12.     -ms-transform: scale(2.5,2); /* IE 9 */  
  13.     -webkit-transform: scale(2.5,2); /* Safari */  
  14.     transform: scale(2.5,2); /* Standard syntax */  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div>  
  20. This div element is scaled 2.5 times of its original width, and 2 times of its original height.  
  21. </div>  
  22. </body>  
  23. </html> 

The skewX() method

The CSS skewX() method is used to skew an element along the X axis according to the given angle. Let?s take an example to skew an

element 30 degrees along the X-axis.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. div {  
  6.     width: 300px;  
  7.     height: 100px;  
  8.     background-color: lightpink;  
  9.     border: 1px solid black;  
  10. }  
  11. div#myDiv {  
  12.     -ms-transform: skewX(30deg); /* IE 9 */  
  13.     -webkit-transform: skewX(30deg); /* Safari */  
  14.     transform: skewX(30deg); /* Standard syntax */  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div>  
  20. This a normal div element.  
  21. </div>  
  22. <div id="myDiv">  
  23. This div element is skewed 30 degrees along the X-axis.  
  24. </div>  
  25. </body>  
  26. </html> 

The skewY() method

The CSS skewY() method is used to skew an element along the Y axis according to the given angle. Let's take an example to skew an

element 30 degrees along the Y-axis.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. div {  
  6.     width: 300px;  
  7.     height: 100px;  
  8.     background-color: lightpink;  
  9.     border: 1px solid black;  
  10. }  
  11. div#myDiv {  
  12.     -ms-transform: skewY(30deg); /* IE 9 */  
  13.     -webkit-transform: skewY(30deg); /* Safari */  
  14.     transform: skewY(30deg); /* Standard syntax */  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div>  
  20. This a normal div element.  
  21. </div>  
  22. <div id="myDiv">  
  23. This div element is skewed 30 degrees along the Y-axis.  
  24. </div>  
  25. </body>  
  26. </html> 

The skew() method

The CSS skew() method is used to skew an element along with X-axis and Y-axis according to the given angle.

Let's take a <div> element and skew it 30 degree along the X-axis and 20 degree along the Y-axis.

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. div {  
  6.     width: 300px;  
  7.     height: 100px;  
  8.     background-color: lightpink;  
  9.     border: 1px solid black;  
  10. }  
  11. div#myDiv {  
  12.     -ms-transform: skew(30deg,20deg); /* IE 9 */  
  13.     -webkit-transform: skew(30deg,20deg); /* Safari */  
  14.     transform: skew(30deg,20deg); /* Standard syntax */  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <div>  
  20. This a normal div element.  
  21. </div>  
  22. <div id="myDiv">  
  23. This div element is skewed 30 degrees along the X-axis, and 20 degrees along the Y-axis.  
  24. </div>  
  25. </body>  
  26. </html> 

The matrix() method

The CSS matrix() method combines all the six 2D transform methods altogether. It allows you to rotate, scale, translate, and skew elements.

Syntax:

The parameters of matrix method: matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY())

See this example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <style>  
  5. div {  
  6.     width: 300px;  
  7.     height: 100px;  
  8.     background-color: lightpink;  
  9.     border: 1px solid black;  
  10. }  
  11. div#myDiv1 {  
  12.     -ms-transform: matrix(1, -0.3, 0, 1, 0, 0); /* IE 9 */  
  13.     -webkit-transform: matrix(1, -0.3, 0, 1, 0, 0); /* Safari */  
  14.     transform: matrix(1, -0.3, 0, 1, 0, 0); /* Standard syntax */  
  15. }  
  16. div#myDiv2 {  
  17.     -ms-transform: matrix(1, 0, 0.5, 1, 150, 0); /* IE 9 */  
  18.     -webkit-transform: matrix(1, 0, 0.5, 1, 150, 0); /* Safari */  
  19.     transform: matrix(1, 0, 0.5, 1, 150, 0); /* Standard syntax */  
  20. }  
  21. </style>  
  22. </head>  
  23. <body>  
  24. <p>The matrix() method combines all the 2D transform methods into one.</p>  
  25. <div>  
  26. This  is a normal div element.  
  27. </div>  
  28. <div id="myDiv1">  
  29. Using the matrix() method.  
  30. </div>  
  31. <div id="myDiv2">  
  32. Another use of the matrix() method.  
  33. </div>  
  34. </body>  
  35. </html>