The transform-origin
CSS property sets the origin for an element's transformations.
The transform-origin
CSS property sets the origin for an element's transformations.
The transform origin is the point around which a transformation is applied. For example, the transform origin of the rotate()
function is the center of rotation.
In effect, this property wraps a pair of translations around the element's other transformations. The first translation moves the transform origin to the true origin at . Then the other transformations are applied, and because the transform origin is at , those transformations act about the transform origin. Finally, the opposite translation is applied, moving the transform origin back to its original location. Consequently, this definition
transform-origin: -100% 50%; transform: rotate(45deg);
results in the same transformation as
transform-origin: 0 0; transform: translate(-100%, 50%) rotate(45deg) translate(100%, -50%);
Reading from right to left, translate(100%, -50%)
is the translation to bring the transform origin to the true origin, rotate(45deg)
is the original transformation, and translate(-100%, 50%)
is the translation to restore the transform origin to its original location.
By default, the origin of a transform is center
.
/* One-value syntax */ transform-origin: 2px; transform-origin: bottom; /* x-offset | y-offset */ transform-origin: 3cm 2px; /* x-offset-keyword | y-offset */ transform-origin: left 2px; /* x-offset-keyword | y-offset-keyword */ transform-origin: right top; /* y-offset-keyword | x-offset-keyword */ transform-origin: top right; /* x-offset | y-offset | z-offset */ transform-origin: 2px 30% 10px; /* x-offset-keyword | y-offset | z-offset */ transform-origin: left 5px -3px; /* x-offset-keyword | y-offset-keyword | z-offset */ transform-origin: right bottom 2cm; /* y-offset-keyword | x-offset-keyword | z-offset */ transform-origin: bottom right 2cm; /* Global values */ transform-origin: inherit; transform-origin: initial; transform-origin: revert; transform-origin: revert-layer; transform-origin: unset;
The transform-origin
property may be specified using one, two, or three values, where each value represents an offset. Offsets that are not explicitly defined are reset to their corresponding initial values.
If a single <length>
or <percentage>
value is defined, it represents the horizontal offset.
If two or more values are defined and either no value is a keyword, or the only used keyword is center
, then the first value represents the horizontal offset and the second represents the vertical offset.
<length>
, a <percentage>
, or one of the keywords left
, center
, right
, top
, and bottom
.<length>
, a <percentage>
, or one of the keywords left
, center
, and right
.<length>
, a <percentage>
, or one of the keywords top
, center
, and bottom
.<length>
. It always represents the Z offset.Is a <length>
or a <percentage>
describing how far from the left edge of the box the origin of the transform is set.
Is one of the left
, right
, top
, bottom
, or center
keyword describing the corresponding offset.
Is a <length>
or a <percentage>
describing how far from the top edge of the box the origin of the transform is set.
Is one of the left
, right
, or center
keyword describing how far from the left edge of the box the origin of the transform is set.
Is one of the top
, bottom
, or center
keyword describing how far from the top edge of the box the origin of the transform is set.
Is a <length>
(and never a <percentage>
which would make the statement invalid) describing how far from the user eye the z=0 origin is set.
The keywords are convenience shorthands and match the following <percentage>
values:
Keyword | Value |
---|---|
left | 0% |
center | 50% |
right | 100% |
top | 0% |
bottom | 100% |
Initial value | 50% 50% 0 |
---|---|
Applies to | transformable elements |
Inherited | no |
Percentages | refer to the size of bounding box |
Computed value | for <length> the absolute value, otherwise a percentage |
Animation type | simple list of length, percentage, or calc |
Note: The initial value of transform-origin
is 0 0
for all SVG elements except for root <svg>
elements and <svg>
elements that are a direct child of a foreignObject, and whose transform-origin
is 50% 50%
, like other CSS elements. See the SVG transform-origin attribute for more information.
transform-origin =
[ left | center | right | top | bottom | <length-percentage> ] |
[ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] <length>? |
[ [ center | left | right ] && [ center | top | bottom ] ] <length>?
<length-percentage> =
<length> |
<percentage>
This example shows the effect of choosing different transform-origin
values for a variety of transformation functions.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
transform-origin |
361 | 1212 | 49163.5 | 109 | 231512.1–1510.5–15 | 92 | ≤374.4 | 3618 | 49164 | 241512.1–1511–14 | 91 | 3.01.0 |
support_in_svg |
19 | 17 | 43Keywords and percentages refer to the canvas instead of the object itself. See bug 1209061. |
No | 15 | 6Only supported for transformations applied using the CSStransform property (e.g. .className { transform: rotate(45deg); transform-origin: center; } ). It has no effect on transformations applied using the transform SVG attribute (e.g. <rect style="transform-origin: center;" transform="rotate(45)" /> ). |
4.4 | 25 | 43Keywords and percentages refer to the canvas instead of the object itself. See bug 1209061. |
14 | 6Only supported for transformations applied using the CSStransform property (e.g. .className { transform: rotate(45deg); transform-origin: center; } ). It has no effect on transformations applied using the transform SVG attribute (e.g. <rect style="transform-origin: center;" transform="rotate(45)" /> ). |
1.5 |
three_value_syntax |
12 | 12 | 10 | 9 | 15 | 5 | ≤37 | 18 | 10 | 14 | 3.2 | 1.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin