I have a box in the top left corner of a document. I want to rotate it using a transform so that one of its edges is pointing toward the lower right corner of the viewport. Based on my reading of the relevant specs, this seems like it ought to work.
html, body {
margin: 0;
padding: 0;
}
#oriented {
position: absolute;
width: 100px;
height: 100px;
top: 0;
left: 0;
outline: solid black 2px;
transform-origin: top left;
transform: rotate(atan2(100vh, 100vw));
}
<div id="oriented"></div>
However, it doesn’t work as expected. Firefox doesn’t rotate the box at all. Chrom-ish browsers put it at a fixed 45° angle, regardless of viewport. Is there any way to get this working? Is this a bug in the browsers, or am I misunderstanding the specs?
2
Answers
Dividing two lengths to get a number is something defined in the Spec but has no support yet so your code may work in the future but not now.
You can try
calc(50vw/100vw)
which actually invalid but in the future it should give you0.5
Same logic with your code since
atan2(100vh, 100vw)
is equivalent toatan(100vh/100vw)
.it seems that chrome is doing the effort to convert your code into something valid but it’s clearly not the expected result.
Although it may not make much of a difference here, but the values assigned to
atan2(100vh, 100vw)
seemed reversed in that the horizontal axisx
is the first value and the vertical axisy
is the second value whereinx = 100vh
andy = 100vw
vh
is normally associated withy
andvw
withx
. Also, if you use anabsolute
positioned element, it’s easier to position it within an element withposition: relative
. One more thing is thattransform-origin
is in relation to the position of the element being transformed so it’s been changed tocenter center
.I don’t know exactly what you’re objective is so I made an interactive example so you can adjust
x
andy
on the fly.