Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS transform-origin Property

Posted in CSS Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 22 - Views
⏳ 4 mins
💬 1 Comment
CSS transform-origin Property

Photo Credit to CodeToFun

🙋 Introduction

The transform-origin property in CSS is used to change the position of the origin of a transformation. By default, transformations like rotate, scale, and skew are performed around the center of an element.

The transform-origin property allows you to adjust this point to a different location within the element.

💡 Syntax

The syntax for the transform-origin property is straightforward. You can specify one, two, or three values to set the origin point along the x-axis, y-axis, and z-axis.

Syntax
Copied
Copy To Clipboard
element {
  transform-origin: x-axis y-axis z-axis;
}
  • x-axis: Defines where the transformation's x-origin is located.
  • y-axis: Defines where the transformation's y-origin is located.
  • z-axis (Optional): Defines where the transformation's z-origin is located.

🎛️ Default Value

The default value of the transform-origin property is 50% 50% 0, which means the transformation is performed around the center of the element along the x and y axes, with no offset along the z-axis.

🏠 Property Values

ValueDescription
lengthA length value (e.g., 10px, 2em) that defines the origin point.
percentageA percentage value (e.g., 50%, 0%) relative to the element's size.
keywordKeywords such as top, bottom, left, right, and center.
z-axis (optional)A length or percentage defining the z-axis origin.

📄 Example

In this example, we'll change the transform origin of a square to the top left corner and rotate it by 45 degrees.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS transform-origin Example</title>
  <style>
    .box {
      width: 100px;
      height: 100px;
      background-color: lightblue;
      transform-origin: top left;
      transform: rotate(45deg);
    }
  </style>
</head>
<body>
  <h1>Box with Custom Transform Origin</h1>
  <div class="box"></div>
</body>
</html>

🖥️ Browser Compatibility

The transform-origin property is supported in all modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. Ensure that your website is tested across different browsers to confirm compatibility.

🎉 Conclusion

The transform-origin property is an essential tool for web developers looking to create complex and visually engaging transformations.

By adjusting the origin point of transformations, you can achieve a wide range of effects and animations that enhance the user experience. Experiment with different origin points and see how this property can elevate your web designs.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy