CSS transform-origin lets you control the pivot point around which an element’s transform (like rotate, scale, or skew) is applied. By default, that point is the center of the element (50% 50%), but you can move it to any corner, edge, or even a specific pixel coordinate. This article explains the syntax, how it interacts with transforms, and how to choose the right origin for common effects.
What Is transform-origin?
Every CSS transform on an element happens relative to a fixed point called the transform origin. Think of it as the pin in a map: if you rotate a rectangular card, the card spins around that pin. The default pin is the exact center of the element’s border box. Changing the origin changes where that pin sits, which dramatically changes the visual result.
For example, rotating an element 45° with the default origin spins it around its center. If you set transform-origin: top left, the same rotation swings the element like a door hinge on its top-left corner. The transform property itself (rotate, scale, etc.) stays the same—only the pivot changes.
Syntax and Values
The property accepts one, two, or three values (the third is for 3D transforms). All values are relative to the element’s border box, which includes padding but not the border or margin.
- One value: The other axis defaults to
center. Sotransform-origin: topis the same astop center. - Two values: First is X (horizontal), second is Y (vertical). Order matters.
- Three values: The third is Z for 3D transforms (e.g.,
rotateZortranslateZ). It must be a length (like20px), not a percentage.
Each value can be a keyword, a length, or a percentage:
- Keywords:
left,center,rightfor X;top,center,bottomfor Y. These correspond to 0%, 50%, and 100% respectively. - Lengths: Fixed distances from the top-left corner of the border box. Negative values are allowed—they place the origin outside the element.
- Percentages: Relative to the element’s own width (for X) and height (for Y). A value of
100% 100%is the bottom-right corner.
How It Interacts with Transforms
The origin is applied before the transform. That means the element is first repositioned so that the origin point coincides with the transform’s pivot, then the transform is applied, then the element is moved back. This is why changing the origin can shift the element’s final position even if the transform values are identical.
Consider scale(2) with origin at the center: the element doubles in size from its center, so it expands equally in all directions. With transform-origin: top left, the element doubles in size but the top-left corner stays fixed—the element grows right and down only. This is a common technique for zooming into a specific part of an image or UI panel.
For rotate(), the origin determines the arc. A clock hand uses transform-origin: bottom center so it pivots from the base. A loading spinner often uses center so it spins around its middle.
How to Choose the Right Origin
Your choice depends on the effect you want. Here are common scenarios:
- Rotate a card from a corner: Use
transform-origin: top leftortop rightto simulate a page turning. - Grow from a specific edge: For an accordion that expands downward, use
transform-origin: top center. - Scale an image from its center: Keep the default
center(or50% 50%) so the image stays centered. - Pivot a needle or hand: Use
transform-origin: bottom center(or0% 100%). - Zoom into a point: Set the origin to the point you want to zoom into (e.g.,
transform-origin: 20% 30%).
For interactive elements, you might change the origin dynamically via JavaScript based on the user’s click position. That allows a magnifier effect that follows the cursor.
Common Mistakes and Pitfalls
One frequent mistake is forgetting that transform-origin only affects the element it is applied to, not its children. If you want a child to pivot around a point outside itself, you must set the origin on the child, not the parent.
Another issue is using transform-origin with translate(). The origin does not affect translation—translation moves the element regardless of the origin. This often confuses beginners who expect the origin to act as a center for all transforms.
Also, note that transform-origin is ignored for elements that are not transformed. If you set it but no transform is applied, nothing happens visually. And if you use multiple transforms in one declaration (e.g., transform: rotate(45deg) scale(1.5)), the origin applies to the whole chain—you cannot have different origins for each function.
Browser Support and Performance
All modern browsers support transform-origin, including Chrome, Firefox, Safari, Edge, and Opera. Internet Explorer 9 and later support it with the -ms- prefix. For 3D transforms, you may need the -webkit- prefix in older Safari versions, but current versions do not.
Performance-wise, changing the origin itself is cheap. However, animating a transform that uses a non-default origin can be slightly more expensive than animating with the default because the browser must recalculate the element’s position. In practice, the difference is negligible on modern hardware. For smooth animations, prefer using transform and opacity over properties like top or left, and keep the origin constant during the animation.
Frequently Asked Questions
What is the default value of transform-origin?
The default is 50% 50%, which is the center of the element. For SVG elements, the default is 0 0 (top-left), but in CSS, HTML elements default to center.
Can I use transform-origin with SVG elements?
Yes, but the behavior differs slightly. In SVG, the default origin is 0 0 (the top-left of the SVG canvas), not the element’s center. You can set it explicitly (e.g., transform-origin: center) to get the same behavior as HTML. Note that in SVG, the origin is relative to the user coordinate system, not the element’s border box.
Does transform-origin affect translate()?
No. Translation moves the element along the X and Y axes regardless of the origin. The origin only affects rotations, scaling, and skewing.
How do I set transform-origin to a specific pixel?
Use length values: transform-origin: 20px 30px. This places the origin 20px from the left and 30px from the top of the border box. Negative values are allowed, which place the origin outside the element.
Can I animate transform-origin itself?
Yes, transform-origin is animatable in most browsers. You can transition it from one value to another, but the interpolation may not be smooth in all cases because it involves percentages and lengths. It’s often better to keep the origin fixed and animate the transform instead.
Why is my transform-origin not working?
Check that the element has a transform applied. Also, ensure you are not overriding it with a shorthand that resets it. For example, the transform property does not reset transform-origin, but some CSS resets or libraries might. Also, verify the element is not an inline element—inline elements do not respect transforms unless you set display: inline-block or block.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Hard Drive Very Slow Suddenly? Fixes That Work
- Windows Insider: Check New Windows 10 Features
- Restore Pitted Chrome Sink Drain Surrounds
- Error Performing Query Error Facebook: Fixes
- WiFi Not Working on HTC 10? Solved
- Fix Windows 100% CPU Usage: 7 Real Fixes