--- title: transform-origin slug: Web/CSS/transform-origin tags: - CSS translation_of: Web/CSS/transform-origin ---
CSS 內的transform-origin
屬性可以設定元素變化的原點。
變化原點指的是應用變化的點。舉例來說, rotate()
函數的原點為旋轉中心。 (This property is applied by first translating the element by the negated value of the property, then applying the element's transform, then translating by the property value.)
/* 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: unset;
The transform-origin
p屬性可以使用多次,每一次都代表著一個偏移量。若未設定偏移量,則重置為其對應的 初始值。
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.
left
, center
, right
, top
or bottom
.left
, center
, and right
.top
, center
, and bottom
.left
, right
, top
, bottom
, or center
keyword describing the corresponding offset.left
, right
, or center
keyword describing how far from the left edge of the box the origin of the transform is set.top
, bottom
, or center
keyword describing how far from the top edge of the box the origin of the transform is set.The keywords are convenience shorthands and match the following {{cssxref("<percentage>")}} values:
Keyword | Value |
---|---|
left |
0% |
center |
50% |
right |
100% |
top |
0% |
bottom |
100% |
Code | Sample |
---|---|
|
<div class="box1"> </div> .box1 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: none; -webkit-transform: none; } {{EmbedLiveSample('transform_none', '', 120, '', '', 'no-button') }}
|
|
<div class="box2"> </div> .box2 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: rotate(30deg); -webkit-transform: rotate(30deg); } {{EmbedLiveSample('transform_rotate_only', '', 120, '', '', 'no-button') }}
|
|
<div class="box3"> </div> .box3 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform-origin: 0 0; -webkit-transform-origin: 0 0; transform: rotate(30deg); -webkit-transform: rotate(30deg); } {{EmbedLiveSample('transform_rotate', '', 120, '', '', 'no-button') }}
|
|
<div class="box4"> </div> .box4 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform-origin: 100% 100%; -webkit-transform-origin: 100% 100%; transform: rotate(30deg); -webkit-transform: rotate(30deg); } {{EmbedLiveSample('transform_rotate_with_percentage', '', 120, '', '', 'no-button') }}
|
|
<div class="box5"> </div> .box5 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform-origin: -1em -3em; -webkit-transform-origin: -1em -3em; transform: rotate(30deg); -webkit-transform: rotate(30deg); } {{EmbedLiveSample('transform_rotate_with_em', '', 120, '', '', 'no-button') }}
|
|
<div class="box6"> </div> .box6 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: scale( {{EmbedLiveSample('transform_scale_only', '', 120, '', '', 'no-button') }}
|
|
<div class="box7"> </div> .box7 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: scale( {{EmbedLiveSample('transform_scale_without_origin', '', 120, '', '', 'no-button') }}
|
|
<div class="box8"> </div> .box8 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: scale( {{EmbedLiveSample('transform_scale', '', 120, '', '', 'no-button') }}
|
|
<div class="box9"> </div> .box9 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: skewX(50deg); -webkit-transform: skewX(50deg); transform-origin: 100% -30%; -webkit-transform-origin: 100% -30%; } {{EmbedLiveSample('transform_skew_x', '', 120, '', '', 'no-button') }}
|
|
<div class="box10"> </div> .box10 { margin: 0.5em; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; transform: skewY(50deg); -webkit-transform: skewY(50deg); transform-origin: 100% -30%; -webkit-transform-origin: 100% -30%; } {{EmbedLiveSample('transform_skew_y', '', 120, '', '', 'no-button') }}
|
Specification | Status | Comment |
---|---|---|
{{ SpecName('CSS3 Transforms', '#transform-origin-property', 'transform-origin') }} | {{ Spec2('CSS3 Transforms') }} |
{{Cssinfo}}
{{Compat("css.properties.transform-origin")}}