CSS Reference ยท Transforms, Transitions and Animations
The transform-origin property
Reference for the CSS transform-origin property: choose the point that rotation, scaling and skewing pivot around, using keywords, lengths or percentages.
The transform-origin property sets the fixed point around which an element's transformations happen. When you rotate or scale an element, this is the one point that stays where it was. By default it is the center of the element.
Moving the origin changes the result of rotations, scaling and skewing, but not of plain translation. A door swings from its hinge, not its middle, and transform-origin is how you set the hinge.
.door { transform-origin: left center; transform: rotate(-25deg); }
Try it
The red dot marks each origin. It is the only point of the blue box that does not move.
Syntax
transform-origin: <x> <y> <z>?; /* x: left | center | right | <length-percentage> */ /* y: top | center | bottom | <length-percentage> */ /* z: <length> */
- Initial value:
50% 50% 0 - Applies to: transformable elements
- Inherited: no
- Animatable: yes
- Computed value: an absolute length (or percentage) for each position
Values
| Value | Description |
|---|---|
left, center, right |
Horizontal keywords, equal to 0%, 50% and 100%. |
top, center, bottom |
Vertical keywords, equal to 0%, 50% and 100%. |
<length> |
Offset from the top-left corner of the reference box. Can be negative or larger than the box. |
<percentage> |
Relative to the reference box width (x) or height (y). |
| Third value | A length along the z axis, used with 3D transforms. Percentages are not allowed here. |
With one value, the other defaults to center. Keywords may be written in either order when both are keywords (top left = left top).
Usage notes
- Reference box. For HTML elements this is the border box. For SVG elements, the reference depends on
transform-box, and without it SVG shapes use the SVG viewport origin, a common surprise. - Translation is unaffected.
translate()ignores the origin; rotation, scale and skew use it. - Animation. Changing the origin during an animation makes the element jump or sweep, so set it once and animate
transformonly. - 3D. The origin's third value shifts the pivot along the depth axis. It is separate from
perspective-origin, which sets the vanishing point. - Hit area. The pointer follows the transformed shape.
Browser support
Supported in every browser.
Related
transform,transform-boxrotate,scale- Lesson: 2D Transforms