Confusion may arise from the question at hand. My goal is to utilize CSS to absolutely position a div with perspective in a way that resembles it being inside the laptop screen I created with an SVG.
I am under the impression that achieving this effect is possible by using a combination of perspective
, rotateX()
, rotateY()
, but I haven't quite nailed it yet.
Take a look at this Codepen for reference.
Below is the relevant CSS code snippet:
.container {
perspective: 500px;
}
.screenContents {
transform: rotateX(10deg) rotateY(-10deg);
/* 16:9 ratio to calculate size */
--scale: 36;
width: calc(16px * var(--scale));
height: calc(9px * var(--scale));
position: absolute;
top: 30px;
left: 300px;
}
Here's the HTML part:
<div class='container'>
<div class='screenContents'>Screen Contents</div>
</div>
<img src='device.svg' />
I've experimented with setting perspective-origin
, and using rotateZ()
among other things.
Dimensions and Information Regarding the SVG
Here are some screen dimensions I derived from the SVG:
area: 1529 sq pixels
(width and height calculated from each corner point)
width: ~500px
height: ~350px
Coordinates of each point on the SVG screen I used for calculations:
top left: 382,514
top right: 882,503
bottom left: 312,187
bottom right: 816,151
The SVG is a traced image of a Google Pixel Go laptop with a 13.3" screen size and 16:9 ratio.
I believe these dimensions must play a role in the final calculations.
Conclusion
I aim to position a div in a manner that simulates it being displayed on the laptop screen. I'm seeking guidance or a brilliant mind to devise a solution. Feel free to experiment with the Codepen.
Calling all CSS experts out there, any help would be greatly appreciated!