CSS3 2D Transforms
CSS3 Transforms
CSS3 transforms can move, scale, rotate, lengthen, or stretch an element.
How Does It Work?
The effect of a transform is to change the shape, size, and position of an element.
You can use 2D or 3D transforms to transform your elements.
Move the mouse over the elements below to see the 2D and 3D transform effects:
2D Transforms
3D Transforms
Browser support
The numbers in the table indicate the first browser version that supports this property.
A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.
| property | |||||
|---|---|---|---|---|---|
| transform | 36.0 | ||||
| 4.0 -webkit- | 10.0 | ||||
| 9.0 -ms- | 16.0 | ||||
| 3.5 -moz- | 3.2 -webkit- | 23.0 |
15.0 -webkit-
12.1
10.5 -o- |
| transform-origin
(two-value syntax) | 36.0
4.0 -webkit- | 10.0
9.0 -ms- | 16.0
3.5 -moz- | 3.2 -webkit- | 23.0
15.0 -webkit-
12.1
10.5 -o- |
Internet Explorer 10, Firefox, and Opera support the transform property.
Chrome and Safari require the -webkit- prefixed version.
Note: Internet Explorer 9 requires the -ms- prefixed version.
2D Transforms
In this chapter you will learn about the 2D transform methods:
- translate()
- rotate()
- scale()
- skew()
- matrix()
In the next chapter you will learn about 3D transforms.
Example
div
{
transform:
rotate
(
30
deg
)
; -
ms-transform:
rotate
(
30
deg
)
;
/*
IE 9
*/
webkit-transform:
rotate
(
30
deg
)
;
/*
Safari and Chrome
*/
}
Try it »
The translate() Method
The translate() method moves an element from its current position based on the parameters given for the left (X-axis) and top (Y-axis) positions.
Example
div
{
transform:
translate
(
50
px
,
100
px
)
; -
ms-transform:
translate
(
50
px
,
100
px
)
;
/*
IE 9
*/
webkit-transform:
translate
(
50
px
,
100
px
)
;
/*
Safari and Chrome
*/
}
Try it »
The translate value (50px, 100px) moves the element 50 pixels from the left and 100 pixels from the top.
The rotate() Method
The rotate() method rotates an element clockwise by a given number of degrees. Negative values are allowed, which rotate the element counterclockwise.
Example
div
{
transform:
rotate
(
30
deg
)
; -
ms-transform:
rotate
(
30
deg
)
;
/*
IE 9
*/
webkit-transform:
rotate
(
30
deg
)
;
/*
Safari and Chrome
*/
}
Try it »
The rotate value (30deg) rotates the element clockwise by 30 degrees.
The scale() Method
The scale() method increases or decreases the size of an element, depending on the parameters for width (X-axis) and height (Y-axis):
Example
-ms-transform:
scale
(2,3);
/*
IE 9
*/
-webkit-transform:
scale
(2,3);
/*
Safari
*/
transform:
scale
(2,3);
/*
Standard syntax
*/
Try it »
scale(2,3) makes the width twice the original size and the height three times the original size.
The skew() Method
Syntax
transform:skew( [,]);
It contains two parameter values, indicating the skew angles along the X-axis and Y-axis respectively. If the second parameter is empty, it defaults to 0. A negative parameter means skewing in the opposite direction.
- skewX(
); means skewing only along the X-axis (horizontal direction). - skewY(
); means skewing only along the Y-axis (vertical direction).
Example
div
{
transform:
skew
(
30
deg
,
20
deg
)
; -
ms-transform:
skew
(
30
deg
,
20
deg
)
;
/*
IE 9
*/
webkit-transform:
skew
(
30
deg
,
20
deg
)
;
/*
Safari and Chrome
*/
}
Try it »
skew(30deg,20deg) The element is used in X axis and Y axis, skewing 20 and 30 degrees.
The matrix() Method
The matrix() method combines all the 2D transform methods into one.
The matrix() method has six parameters, containing rotation, scaling, movement (translation), and skewing functions.
Example
Rotate a div element 30° using the matrix() method
div
{
transform:
matrix
(
0.866
,
0.5
,-
0.5
,
0.866
,
0
,
0
)
; -
ms-transform:
matrix
(
0.866
,
0.5
,-
0.5
,
0.866
,
0
,
0
)
;
/*
IE 9
*/
webkit-transform:
matrix
(
0.866
,
0.5
,-
0.5
,
0.866
,
0
,
0
)
;
/*
Safari and Chrome
*/
}
Try it »
New Transform Properties
The following lists all the transform properties:
| Property | Description | CSS |
|---|---|---|
| transform | Applies a 2D or 3D transform to an element. | 3 |
| transform-origin | Allows you to change the position of a transformed element. | 3 |
2D Transform Methods
| function | Description |
|---|---|
| matrix(n,n,n,n,n,n) | Defines a 2D transformation, using a matrix of six values. |
| translate(x,y) | Defines a 2D transform, moving the element along the X and Y axes. |
| translateX(n) | Defines a 2D transform, moving the element along the X axis. |
| translateY(n) | Defines a 2D transform, moving the element along the Y axis. |
| scale(x,y) | Defines a 2D scaling transform, changing the width and height of an element. |
| scaleX(n) | Defines a 2D scaling transform, changing the width of an element. |
| scaleY(n) | Defines a 2D scaling transform, changing the height of an element. |
| rotate(angle) | Defines a 2D rotation, with the angle specified in the parameter. |
| skew(x-angle,y-angle) | Defines a 2D skew transform, along the X and Y axes. |
| skewX(angle) | Defines a 2D skew transform, along the X axis. |
| skewY(angle) | Defines a 2D skew transform, along the Y axis. |