Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS perspective-origin Property

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

Photo Credit to CodeToFun

🙋 Introduction

The perspective-origin property in CSS is used to define the origin point of the perspective view for a 3D transformed element.

This property controls the position from which the viewer perceives the depth and perspective of the element, affecting how 3D transformations such as rotations and translations appear.

💡 Syntax

The syntax for the perspective-origin property allows you to specify the position in 2D space. It can take either two values for the X and Y coordinates or a single value for the X coordinate with the Y coordinate defaulting to 50%.

Syntax
Copied
Copy To Clipboard
element {
  perspective-origin: x-axis y-axis;
}
  • x-axis: Specifies the horizontal position of the perspective origin. This can be a length (e.g., 50px) or a percentage (e.g., 50%).
  • y-axis: Specifies the vertical position of the perspective origin. This can also be a length or a percentage.

🎛️ Default Value

The default value of the perspective-origin property is 50% 50%. This means that the perspective origin is positioned at the center of the element.

🏠 Property Values

ValueDescription
lengthA specific distance from the left edge (for the X-axis) or the top edge (for the Y-axis). For example, 20px, 10em.
percentageA percentage relative to the dimensions of the element. For example, 50% positions the origin at the center.
autoThe value auto is not typically used for perspective-origin.

📄 Example

In this example, we’ll set the perspective origin to the top-right corner of a 3D transformed element.

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 perspective-origin Example</title>
  <style>
    .container {
      perspective: 500px; /* Required for perspective-origin to work */
    }
    .box {
      width: 200px;
      height: 200px;
      background-color: lightcoral;
      transform: rotateY(45deg);
      perspective-origin: 100% 0%;
    }
  </style>
</head>
<body>
  <h1>Perspective-Origin Example</h1>
  <div class="container">
    <div class="box"></div>
  </div>
</body>
</html>

In this example, the perspective origin is set to the top-right corner of the .box element. The perspective property on the container ensures the 3D effect is visible.

🖥️ Browser Compatibility

The perspective-origin property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. However, be sure to test across different devices and browsers to confirm consistent behavior.

🎉 Conclusion

The perspective-origin property is essential for controlling the depth and perspective of 3D transformed elements.

By adjusting the perspective origin, you can influence how 3D transformations are perceived, allowing for more dynamic and visually interesting effects. Experiment with different values to see how the perspective changes and enhance the depth of 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