Possibly more math than CSS, but I'm trying to determine a method for adjusting the positioning of a div after a CSS skewY transform has been applied to it.
In the snippet below, the div with the blue border has a 3.5deg skewY applied, and I'd like to know if there is a mathematical way to know how much top
to apply to the blue div so that the top right corner is always perfectly aligned to the top right corner of the div with the red border, regardless of the width of the two divs.
I have played around with numbers using %
and vw
, but I'm looking for a solid math-based solution.
.parent {
border: 1px solid red;
position: relative;
margin-top: 100px;
height: 200px;
}
.child {
border: 1px solid blue;
position: absolute;
width: 100%;
height: 100%;
transform: skewY(-3.5deg);
}
<div class="parent">
<div class="child">
content
</div>
</div>
No need math, simply adjust
transform-origin
:But if you want to play with math the exact formula is :
green is the
top
, purple ishalf the width
and yellow isthe angle
of skewIn this case we have
-3.5deg
so thetan(-3.5deg) = -0.061
sotop = -0.061 * 50% of width
BUT since the reference of the div istop left
when applying top property we need to consider a minus sign because we want to adjust thetop right
corner and not thetop left
one