UGURUS offers elite coaching and mentorship for agency owners looking to grow. Start with the free Agency Accelerator today.
The transform-origin
property is used in conjunction with CSS transforms, letting you change the point of origin of a transform.
.element {
transform: rotate(360deg);
transform-origin: top left;
}
As indicated above, the transform-origin
property can take up to two space-separated keyword or length values for a 2D transform and up to three values for a 3D transform.
Using the code above on a 200px by 200px box, with the transform applied to a transitioned using a click event, would behave like this:
By default, the origin of a transform is “50% 50%”, which is exactly in the center of any given element. Changing the origin to “top left” (as in the demo above) causes the element to use the top left corner of the element as a rotation point.
Values can be lengths, percentages or the keywords top
, left
, right
, bottom
, and center
.
The first value is the horizontal position, the second value is the vertical, and the third value represents the position on the Z axis. The third value will only work if you are using 3D transforms, and it cannot be a percentage.
Browser support
IE | Edge | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|---|
9+ | All | 3.5+ | All | All | 11.5+ |
Android Chrome | Android Firefox | Android Browser | iOS Safari | Opera Mobile |
---|---|---|---|---|
All | All | 91+ | All | All |
Css- tricks.com is really usefull for web designers.. :)
what jquery library should i use for this code?
None, it’s pure CSS.
You’d be best of using https://github.com/madrobby/vapor.js, pretty good for implementing these transforms.
Unless you’re talking about fallback coding CSS tricks are for CSS tricks, rarely is JS used anywhere for the actual affect. If you refer to Codepen you can see there is minimal JS and no libraries are used. This would defeat the CSS workaround in the first place. As such though besides IE9(and lower) vendor prefixes should support all other browsers for these methods in most cases.
Welcome to CSS Tricks!
Hi. I’m wondering if this might be a way solve an issue I’m having.
I’ve used transform: scale(.5,.5); but my margins behave as though the item has not been scaled. When I resize the browser, the left margin cause the item to push away from the edge. When I inspect elements and turn off the transform, I can see that the item is reacting to the original size (pre-transform).
Is there a way to use transform-origin to correct this? I tried transform-origin: 25%50%; however the problem persists, just at different proportions.
I am referring to the bottom image on this page: stephendanison.com/mondrian
Did you ever find out the solution to this?
Because this is the EXACT problem that I am having….
I just had the same problem and solved it like this:
– you first have to flip the DIV to the left (rotate -90) on its bottom left corner.
– then you shift it to the right (left: Xpx) by the exact amount of pixels as the original height !
Result: the original top-left corner has moved to the original bottom left corner and thus scaling works as expected.
Here’s the html code:
test
and css:
.vertical {
height: 30px;
width: 60 px;
transform-origin: bottom left;
transform: rotate(-90deg);
left: 30px;
}
regards,
GermanChris
Hi!
Tell me, please, can i specify transform-origin for rotation only and disable this effect for other properties.
You’d simply use the tag attribute transform-origin: initial; as reset value. See Transform Origin Attributes
Hello ,
This is good, but can anybody help me for this complicated task…
I want to make a div which have bottom-right corner edge is longer then the Top-right corner edge(i.e.. Lower line is longer than the upper one). How can achieve this ?
Well I’ve been looking for this all freaking day. Thanks Chris.
Is there any special reason you are using
transform-origin: top left
instead of
transform-origin: left top
?I would have thought the first value refers to horizontal position whereas the second value refers to vertical position. Or is it the case that the order of values does not matter when using keywords such as left, right, center, top etc?