HTML - DOM Style Object transform Property



HTML DOM Style Object transform property is used to apply 2D or 3D transformation to transform the object. It lets us transform the object or element using matrix, skew, translate, rotate, scale properties.

Syntax

Set the transform property:
object.style.transform= "none | transform-functions | initial | inherit";
Get the transform property:
object.style.transform;

Property Values

Value Description
none It is the default value which specifies no transformation.
matrix(x, x, x, x, x, x) It specifies a 2D matrix transformation. It takes six values as input.
matrix3d(x, x, x, x,..,x) It specifies a 3D matrix transformation. It uses a 4x4 matrix of 16 values.
translate(x, y) It specifies a 2D translation.
translate3d(x, y, z) It specifies a 3D translation.
translateX(x) It specifies translation in one direction only that is along X-axis.
translateY(y) It specifies translation in one direction only that is along Y-axis.
translateZ(z) It specifies a 3D translation along Z-axis only.
rotate(angle) It specifies a 2D rotation using angle propvided in parameter.
rotate3D(angle) It specifies a 3D rotation.
rotateX(angle) It specifies a 3D rotation along X-axis only.
rotateY(angle) It specifies a 3D rotation along Y-axis only.
rotateZ(angle) It specifies a 3D rotation along Z-axis only.
scale(x, y) It specifies a 2D scale transformation.
scale3d(x, y, z) It specifies a 3D scale transformation.
scaleX(x) It specifies a scale transformation in one direction only that is along X-axis.
scaleY(y) It specifies a scale transformation in one direction only that is along Y-axis.
scaleZ(z) It specifies a scale transformation in one direction only that is along Z-axis.
skew(angle, angle) It specifies a 2D skew transformation along X and Y axis.
skewX(angle) It specifies a 2D skew transformation along X-axis.
skewY(angle) It specifies a 2D skew transformation along Y-axis.
perspective(x) It specifies a perspective view for 3D transformed element.
initial It is used to set this property to it's default value.
inherit It is used to inherit the property of it's parent element.

Return Value

It returns a string value which represents the transform property of an element.

Examples of HTML DOM Style Object 'transform' Property

The following examples illustrates different transformation properties like translation, scale, rotation, skew and matrix to div element.

Apply matrix transform Property to div Element

The following example applies matrix and matrix3D transformation properties to div element.

<!DOCTYPE html>]
<html lang="en">
<head>
    <title>
        HTML DOM Style Object transform Property
    </title>
    <style>
        #transform {
            border: 1px solid rgb(77, 73, 73);
            width: 200px;
            height: 50px;
            padding: 5px;
            background-color: #04af2f;
            color: white;
        }
    </style>
</head>
<body>
    <button onclick="fun()">
        2D Matrix
    </button>
    <button onclick="funTwo()">
        3D Matrix
    </button>
    <br><br><br>
    <br><br><br><br>
    <div id="transform">
        <p>
            Welcome To Tutorials Point.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("transform")
                .style.transform = "matrix(0, 1, 1, 0, 0, 0)";
        }
        function funTwo() {
            document.getElementById("transform").style.transform = 
            "matrix3d(0.5,0,0.8,0,0.6,1.2,-1,0,1,0,0.5,0,30,0,10,1)";
        }
    </script>
</body>
</html>

Apply translate transform Property to div Element

The following example applies translate and translate3D transformation properties to div element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object transform Property
    </title>
    <style>
        #transform {
            border: 1px solid rgb(77, 73, 73);
            width: 200px;
            height: 50px;
            padding: 5px;
            background-color: #04af2f;
            color: white;
        }
    </style>
</head>
<body>
    <p>
        Click to apply Translation.
    </p>
    <button onclick="fun()">
        2D Translate
    </button>
    <button onclick="translateThreeD()">
        3D Translate
    </button>
    <button onclick="translateX()">
        Translate X
    </button>
    <button onclick="translateY()">
        Translate Y
    </button>
    <button onclick="funTwo()">
        Translate Z
    </button>
    <div id="transform">
        <p>
            Welcome To Tutorials Point.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("transform")
                .style.transform = "translate(100px, 15px)";
        }
        function translateThreeD() {
            document.getElementById("transform")
                .style.transform = "translate3D(25px, 15px, 40px)";
        }
        function translateX() {
            document.getElementById("transform")
                .style.transform = "translateX(40px)";
        }
        function translateY() {
            document.getElementById("transform")
                .style.transform = "translateY(50px)";
        }
        function funTwo() {
            document.getElementById("transform")
                .style.transform = "translateZ(80px)";
        }
    </script>
</body>
</html>

Apply rotate transform Property to div Element

The following example applies rotate and rotate3D transformation properties to div element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object transform Property
    </title>
    <style>
        #transform {
            border: 1px solid rgb(77, 73, 73);
            width: 200px;
            height: 50px;
            padding: 5px;
            background-color: #04af2f;
            color: white;
        }
    </style>
</head>
<body>
    <p>
        Click to rotate.
    </p>
    <button onclick="fun()">
        2D Rotate
    </button>
    <button onclick="rotateThreeD()">
        3D Rotate
    </button>
    <button onclick="rotateX()">
        Rotate X
    </button>
    <button onclick="rotateY()">
        Rotate Y
    </button>
    <button onclick="funTwo()">
        Rotate Z
    </button>
    <div id="transform">
        <p>
            Welcome To Tutorials Point.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("transform")
                .style.transform = "rotate(40deg)";
        }
        function rotateThreeD() {
            document.getElementById("transform")
                .style.transform = "rotate3D(1, 1, 1, 30deg)";
        }
        function rotateX() {
            document.getElementById("transform")
                .style.transform = "rotateX(45deg)";
        }
        function rotateY() {
            document.getElementById("transform")
                .style.transform = "rotateY(45deg)";
        }
        function funTwo() {
            document.getElementById("transform")
                .style.transform = "rotateZ(80deg)";
        }
    </script>
</body>
</html>

Apply scale transform Property to div Element

The following example applies scale and scale3D transformation properties to div element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object transform Property
    </title>
    <style>
        #transform {
            border: 1px solid rgb(77, 73, 73);
            width: 200px;
            height: 50px;
            padding: 5px;
            background-color: #04af2f;
            color: white;
        }
    </style>
</head>
<body>
    <p>
        Click to scale.
    </p>
    <button onclick="fun()">
        2D Scale
    </button>
    <button onclick="scaleThreeD()">
        3D Scale
    </button>
    <button onclick="scaleX()">
        Scale X
    </button>
    <button onclick="scaleY()">
        Scale Y
    </button>
    <button onclick="funTwo()">
        Scale Z
    </button>   
    <br><br><br>
    <br><br><br><br>
    <div id="transform">
        <p>
            Welcome To Tutorials Point.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("transform")
                .style.transform = "scale(3, 2)";
        }
        function scaleThreeD() {
            document.getElementById("transform")
                .style.transform = "scale3D(3, 2, 2)";
        }
        function scaleX() {
            document.getElementById("transform")
                .style.transform = "scaleX(3)";
        }
        function scaleY() {
            document.getElementById("transform")
                .style.transform = "scaleY(3)";
        }
        function funTwo() {
            document.getElementById("transform")
                .style.transform = "scaleZ(2)";
        }
    </script>
</body>
</html>

Apply skew transform Property to div Element

The following example applies skew transformation property to div element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object transform Property
    </title>
    <style>
        #transform {
            border: 1px solid rgb(77, 73, 73);
            width: 200px;
            height: 50px;
            padding: 5px;
            background-color: #04af2f;
            color: white;
        }
    </style>
</head>
<body>
    <p>
        Click to apply skew transformation.
    </p>
    <button onclick="fun()">
        2D skew
    </button>
    <button onclick="skewX()">
        skew X
    </button>
    <button onclick="skewY()">
        skew Y
    </button>
    <div id="transform">
        <p>
            Welcome To Tutorials Point.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("transform")
                .style.transform = "skew(10deg, 15deg)";
        }
        function skewX() {
            document.getElementById("transform")
                .style.transform = "skewX(45deg)";
        }
        function skewY() {
            document.getElementById("transform")
                .style.transform = "skewY(25deg)";
        }
    </script>
</body>
</html>

Supported Browsers

Property Chrome Edge Firefox Safari Opera
transform Yes 36 Yes 12 Yes 16 Yes 9 Yes 23
html_dom.htm
Advertisements