transform-originプロパティは、要素に2D変形、または、3D変形を適用するときの変形の原点を指定する際に使用します。
値は1つ、または、スペースで区切って2つ指定します。値を1つだけ指定すると、2番目の値はcenterとみなされます。
値を2つ指定すると、両方がキーワード値である場合を除いて、最初の値は水平方向、2番目の値は垂直方向の値となります。
値をパーセンテージや長さで指定する場合、2D変形では変形前の要素の左上位置(0,0)から右下方向への距離(ローカル座標システム)で指定します。
また、3D変形では変形前の要素の左上位置(0,0,0)から右下方向への距離(ローカル座標システム)で指定します。
値には負のパーセンテージや負の長さを指定することもできます。
■値
- パーセンテージ
- 変形を適用する要素の左上位置(0%,0%)からのパーセンテージ、および、3Dの場合にはZ軸方向の長さを指定(初期値は50% 50% 0)
- 長さ
- 変形を適用する要素の左上位置(0,0)からの長さを指定、3D変形では左上位置(0,0,0)からの長さを指定
- キーワード
- 水平方向はleft・center・right、垂直方向はtop・center・bottom、および、3Dの場合にはZ軸方向の長さを指定
■初期値・適用対象・値の継承
- 初期値
- 2D変形では50% 50%、3D変形では50% 50% 0
- 適用対象
- ブロックレベル要素、インライン要素
- 値の継承
- しない
■使用例
CSSソースは外部ファイル(sample.css)に記述
p.sample1, p.sample2 {background-color:limegreen;}
p.sample1 img, p.sample2 img{
-moz-transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
}
p.sample2 img {
transform-origin:right bottom;
}
HTMLソース
<html>
<head>
<link rel=”stylesheet” href=”sample.css” type=”text/css”>
</head>
<body>
<p class=”sample1″>
rotate(-45deg)を指定、transform-originプロパティは未指定<br>
<img src=”https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/kaeru.gif”>
</p>
<p class=”sample2″>
rotate(-45deg)を指定、transform-origin:right bottomを指定<br>
<img src=”https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/kaeru.gif”>
</p>
</body>
</html>
ブラウザ上の表示
rotate(-45deg)を指定、transform-originプロパティは未指定
rotate(-45deg)を指定、transform-origin:right bottomを指定
■ベンダープレフィックスを付けた場合の使用例
CSSソースは外部ファイル(sample.css)に記述
p.prefix_sample1, p.prefix_sample2 {background-color:limegreen;}
p.prefix_sample1 img, p.prefix_sample2 img{
-moz-transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
}
p.prefix_sample2 img {
-moz-transform-origin:200px 150px;
-webkit-transform-origin:200px 150px;
-o-transform-origin:200px 150px;
-ms-transform-origin:200px 150px;
}
HTMLソース
<html>
<head>
<link rel=”stylesheet” href=”sample.css”
type=”text/css”>
</head>
<body>
<p class=”prefix_sample1″>
rotate(-45deg)を指定、transform-originプロパティは未指定<br>
<img src=”https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/kaeru.gif”>
</p>
<p class=”prefix_sample2″>
rotate(-45deg)を指定、transform-origin:right bottomを指定<br>
<img src=”https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/kaeru.gif”>
</p>
</body>
</html>
ブラウザ上の表示
rotate(-45deg)を指定、transform-originプロパティは未指定
rotate(-45deg)を指定、transform-origin:right bottomを指定