An Introduction to CSS Transform Origin and How It Can Be Useful
Abhishek Garg`
Technical Lead specializing in Angular, CSS, JavaScript, React, and HTML5
CSS transform property allows you to modify an element's shape, size, position, and orientation. And, the transform-origin property determines the origin point of transformation. In this article, we'll dive into what the transform-origin property is, how it works, and explore some examples of its usage.
What is CSS Transform Origin?
The transform-origin property sets the origin point of an element's transformation. By default, the origin point is located at the center of an element. However, you can change this point to any desired location by specifying values for the transform-origin property.
The transform-origin property takes two values, which represent the x and y-coordinates of the origin point, respectively. The x-coordinate value can be set as a percentage or a length, while the y-coordinate value can also be set as a percentage or a length.
How Does CSS Transform Origin Work?
The transform-origin property works by rotating, scaling, or skewing an element around a specified point. The default origin point is located at the center of an element. However, you can change the origin point to any desired location by specifying values for the transform-origin property.
For example, consider a square element that has a default transform origin point at the center. If you rotate the element by 45 degrees, the transformation occurs around the center, and the square becomes a diamond shape. However, if you change the origin point to the top-left corner, the transformation will occur around that point, and the square will become a parallelogram.
Examples of CSS Transform Origin
Here are some examples that demonstrate the usage of the transform-origin property:
Rotation around a custom origin point.
<div class="box1">
No origin specified
</div>
<div class="box2">
Origin specified
</div>
<style>
body {
display: flex;
place-items: center;
place-content: center;
width: 100%;
height: 100vh;
}
div {
display: flex;
text-align: center;
place-items: center;
gap: 2rem;
}
.box1 {
width: 100px;
height: 100px;
background-color: red;
transform: rotate(45deg);
}
.box2 {
width: 100px;
height: 100px;
background-color: olive;
transform: rotate(45deg);
transform-origin: top left;
}
</style>
In this example, we have a red square element with a width and height of 100px. The transform property rotates the element by 45 degrees, and the transform-origin property sets the origin point to the top-left corner.
领英推è
Scaling around a custom origin point
<div class="box"></div>
<style>
.box {
? width: 100px;
? height: 100px;
? background-color: blue;
? transform: scale(2);
? transform-origin: bottom right;
}
</style>
In this example, we have a blue square element with a width and height of 100px. The transform property scales the element by a factor of 2, and the transform-origin property sets the origin point to the bottom-right corner.
Skewing around a custom origin point
<div class="box"></div>
<style>
.box {
? width: 100px;
? height: 100px;
? background-color: green;
? transform: skew(20deg);
? transform-origin: center;
}
</style>
In this example, we have a green square element with a width and height of 100px. The transform property skews the element by 20 degrees, and the transform-origin property sets the origin point to the center.
Conclusion
CSS is a powerful tool for modifying the visual appearance of elements on a web page. The transform-origin property plays a crucial role in controlling the origin point of transformations such as rotation, scaling, and skewing. By setting the origin point, you can control the way an element is transformed and create visually appealing designs.
In this article, we've explored what the transform-origin property is, how it works, and demonstrated its usage through various examples. Whether you're a seasoned web developer or just starting out, understanding the transform-origin property is an important aspect of creating visually appealing and responsive web pages.
Learn more on css transform: https://www.dhirubhai.net/pulse/css-transform-unlocking-power-transformation-abhishek-garg-/