Open In App

CSS transform-origin Property

The transform-origin property of CSS is used to specify the origin of the rotation of an element. It is the point at which an element is rotated. It can be used for both 2D and 3D rotations. 

Syntax:



transform-origin: position | initial | inherit

Property Values:

Example 1: Specifying the position in length units 






<!DOCTYPE html>
<html>
   
<head>
    <style>
        .outer {
            margin: 50px;
            border: 1px dotted;
            position: relative;
            height: 100px;
            width: 400px;
            background-color: lightgreen;
        }
 
        .box {
            position: absolute;
            border: 1px solid;
            background: url(
 
            background-size: cover;
            height: 100px;
            width: 400px;
            transform: rotate(15deg);
            transform-origin: 10px 30px;
        }
    </style>
</head>
 
<body>
    <h1>CSS transform-origin Property</h1>
    <p>
      The CSS transform-origin Property is used to set
        the origin of the element's transformation
      </p>
    <div class="outer">
        <div class="box"></div>
    </div>
</body>
   
</html>

Output: 

 Example 2: Specifying the position in percentage 




<!DOCTYPE html>
<html>
   
<head>
    <style>
        .outer {
            margin: 50px;
            border: 1px dotted;
            position: relative;
            height: 100px;
            width: 400px;
            background-color: lightgreen;
        }
 
        .box {
            position: absolute;
            border: 1px solid;
            background: url(
 
            background-size: cover;
            height: 100px;
            width: 400px;
            transform: rotate(15deg);
            transform-origin: 50% 75%;
        }
    </style>
</head>
 
<body>
    <h1>CSS transform-origin Property</h1>
    <p>
      The CSS transform-origin Property is
        used to set the origin of the element's
        transformation
      </p>
    <div class="outer">
        <div class="box"></div>
    </div>
</body>
   
</html>

Output:

Example 3: Specifying the position in keywords 




<!DOCTYPE html>
<html>
   
<head>
    <style>
        .outer {
            margin: 50px;
            border: 1px dotted;
            position: relative;
            height: 100px;
            width: 400px;
            background-color: lightgreen;
        }
 
        .box {
            position: absolute;
            border: 1px solid;
            background: url(
 
            background-size: cover;
            height: 100px;
            width: 400px;
            transform: rotate(15deg);
            transform-origin: bottom left;
        }
    </style>
</head>
 
<body>
    <h1>CSS transform-origin Property</h1>
    <p>
      The CSS transform-origin Property is
        used to set the origin of the element's
        transformation
      </p>
    <div class="outer">
        <div class="box"></div>
    </div>
</body>
   
</html>

Output: 

Example: In this example, we are using transform-origin: initial property.




<!DOCTYPE html>
<html>
   
<head>
    <style>
        .outer {
            margin: 50px;
            border: 1px dotted;
            position: relative;
            height: 100px;
            width: 400px;
            background-color: lightgreen;
        }
 
        .box {
            position: absolute;
            border: 1px solid;
            background: url(
                ) no-repeat;
 
            background-size: cover;
            height: 100px;
            width: 400px;
            transform: rotate(15deg);
            transform-origin: initial;
        }
    </style>
</head>
 
<body>
    <h1>CSS transform-origin Property</h1>
    <p>
      The CSS transform-origin Property is
        used to set the origin of the element's
        transformation
      </p>
    <div class="outer">
        <div class="box"></div>
    </div>
</body>
   
</html>

Output: 

Example: In this example, we are using the transform-origin: inherit property.




<!DOCTYPE html>
<html>
   
<head>
    <style>
        /* this acts as the parent */
         
        .outer {
            margin: 50px;
            border: 1px dotted;
            position: relative;
            height: 100px;
            width: 400px;
            background-color: lightgreen;
            /* set the property of the parent */
            transform-origin: bottom left;
        }
         
        .box {
            position: absolute;
            border: 1px solid;
            background: url(
            ) no-repeat;
         
            background-size: cover;
            height: 100px;
            width: 400px;
            transform: rotate(15deg);
            /* inherits the property of the parent */
            transform-origin: inherit;
        }
    </style>
</head>
 
<body>
    <h1>CSS transform-origin Property</h1>
    <p>
      The CSS transform-origin Property is used to set
    the origin of the element's transformation
      </p>
    <div class="outer">
        <div class="box"></div>
    </div>
</body>
   
</html>

Output: 

Supported Browsers: The browser supported by transform-origin property are listed below:


Article Tags :