The CSS 2d transforms are the process of transforming an HTML element.Basically, it changes the shape or appearance of an HTML element. It is of two types.
2D Transformation
3D Transformation
It is used to transform the HTML element in two dimension directions such as x-axis
or y-axis
. It has following important methods.
translate()
scale()
skewX()
skewY()
rotate()
matrix()
The translate()
method is used to transform an HTML element in the X
or Y
axis.
Source Code
.box:hover{
transform:translate(100px,100px);
}
The translateX()
function is used to to move an element along the X-axis
only.
Source Code
.box:hover{
transform:translateX(220px);
}
The translateY()
function is used to move an element along Y-axis
only.
Source Code
.box:hover{
transform:translateY(220px);
}
The transform scale()
method is used to scale the HTML element along the X-axis
, Y-axis
, or both the axis. The scale contains one or two properties inside the parenthesis.
If the scale()
method contains single properties then it scales the HTML element on both the X
and Y
axis. If the scale()
method contains two properties then the first value is used to transform the HTML element in X-axis
and the second value transforms the HTML element in Y-axis
.
Source Code
.box1:hover{ transform:scale(2);}
.box2:hover{ transform:scale(2,2);}
The skewX()
function is used to skews an HTML element along the X-axis
by the specified angle.
Source Code
.box1:hover{
transform:skewX(45deg);
}
The skewY()
function is used to skew an HTML element along the Y-axis by the specified angle.
Source Code
.box1:hover{ transform:skewY(45deg);}
.box2:hover{ transform:skewY(-30deg);}
Transform skew() function is used to skew the HTML element on the X-axis and Y-axis by the given angles.
Source Code
img {
-webkit-transform: skew(-40deg, 15deg); /* Chrome, Safari, Opera */
-moz-transform: skew(-40deg, 15deg); /* Firefox */
-ms-transform: skew(-40deg, 15deg); /* IE 9 */
transform: skew(-40deg, 15deg); /* Modern Browsers */
}
The rotate()
function is used to rotate the HTML element along X-axis
,Y-axis
or Z-axis
around its origin by the given angles. The origin of the rotating element can be defined using the transform-origin property.
Please keep in mind that the default transform-origin is the center center.
Followings are the transform origin property:
transform:rotateZ()
transform:rotateX()
transform:rotateY()
Source Code
img {
-webkit-transform: rotate(50deg); /* Chrome, Safari, Opera */
-moz-transform: rotate(50deg); /* Firefox */
-ms-transform: rotate(50deg); /* IE 9 */
transform: rotate(50deg); /* Standard syntax */
}
Code Explanation
The function rotate(50deg)
rotates the image in the clockwise direction about its origin by the angle of 50
degrees. You can use negative values to rotate the element counter-clockwise.
Let us rotate HTML element along z-axis by 180deg.
Source Code
.btn:hover{
transform:rotateZ(180deg);
}
Let us rotate HTML element around X-axis
by 70deg.
Source Code
.btn:hover{
transform:rotateX(70deg);
}
Let us rotate, the HTML element around Y-axis
by 180deg.
Source Code
.btn:hover{
transform:rotateY(180deg);
}
The matrix()
function is a combination of scaleX()
, skewY()
, skewX()
, scaleY()
, translateX()
& translateY()
. It takes six parameters in the form of a matrix which can be written as matrix(a, b, c, d, e, f)
.
transform:matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY());
so, in simple terms, the syntax is like –
transform:matrix(a,b,c,d,e,f);
Here, a
& d
help in scaling up or down the elements. It is like scale(a,d)
.
Similarly, b
,c
skew the element in the X
, and Y
axis respectively. It is like skew(b,c)
.
While, e
, f
help to translate the element(move the element) from one position to another. It is like translate(e,f)
.
Source Code
img {
-webkit-transform:matrix(0.51, 0.5, 0, 0.9, 120, 0); /* Chrome, Safari, Opera */
-moz-transform:matrix(0.51, 0.5, 0, 0.9, 120, 0); /* Firefox */
-ms-transform:matrix(0.51, 0.5, 0, 0.9, 120, 0); /* IE 9 */
transform:matrix(0.51, 0.5, 0, 0.9, 120, 0); /* Standard syntax */
}