CSS Transform

Transform Functions

Quick Reference

Function Values Effect
translate(x, y) 50px, -30px, 50% Move element
translateX(x) 50px Horizontal move
translateY(y) -30px Vertical move
scale(n) 1.5, 2, 0.5 Scale (1 = original)
scaleX(n) 1.5 Horizontal scale
scaleY(n) 0.8 Vertical scale
rotate(deg) 45deg, 90deg, -180deg Rotate (positive = clockwise)
skew(x, y) 10deg, -5deg Skew distortion
skewX(deg) 10deg Horizontal skew
skewY(deg) 10deg Vertical skew
transform-origin center, top left, 50% 50% Transform origin point

translate (Move)

Example

HTML
<style>
.box {
  width: 100px;
  height: 100px;
  background: #3498db;
  transition: transform 0.3s;
}
.box:hover {
  transform: translate(50px, 20px);
}
</style>
<div class="box">Hover to move</div>
▶ Try it Yourself

scale (Resize)

Example

HTML
<style>
.box {
  width: 100px;
  height: 100px;
  background: #e74c3c;
  transition: transform 0.3s;
}
.box:hover {
  transform: scale(1.5);
}
</style>
<div class="box">Hover to enlarge</div>
▶ Try it Yourself

rotate (Rotate)

Example

HTML
<style>
.box {
  width: 100px;
  height: 100px;
  background: #2ecc71;
  transition: transform 0.3s;
}
.box:hover {
  transform: rotate(45deg);
}
</style>
<div class="box">Hover to rotate</div>
▶ Try it Yourself

transform-origin

Changes the rotation/scale reference point:

Example

HTML
<style>
.box {
  width: 100px;
  height: 100px;
  background: #9b59b6;
  transform-origin: top left;
  transition: transform 0.3s;
}
.box:hover {
  transform: rotate(45deg);
}
</style>
<div class="box">Rotate from top-left corner</div>
▶ Try it Yourself

Combining Transforms

Multiple transforms execute right to left (reverse order):

CSS
.box {
  transform: translateX(50px) rotate(45deg) scale(1.2);
  /* First scale → Then rotate → Finally translate */
}

❓ FAQ

Q What's the difference between transform and changing top/left?
A transform only triggers the composite stage — doesn't cause layout or paint reflow, better performance. Changing top/left triggers reflow, causing page jank. For animations, prefer transform.
Q What does transform: translate(-50%, -50%) with position: absolute do?
A Classic centering solution. `top: 50%; left: 50%; transform: translate(-50%, -50%);` first positions element's top-left corner at parent center, then translate moves back by half its own dimensions for perfect centering.
Q Do 3D transforms need special setup?
A When using `translateZ()`, `rotateX()`, `rotateY()` etc., add `perspective` to parent (like `perspective: 1000px`), otherwise 3D effects may not be visible. 3D transforms trigger GPU acceleration, better performance than 2D.

📖 Summary

📝 Exercises

  1. Create a box that moves right 50px and scales 1.5x on hover.
  2. Create an image that rotates 180° around Y-axis on hover (needs perspective).
  3. Create a "flip card" — hover flips card to show back content (using rotateY and backface-visibility).
  4. Use transform-origin to change rotation center, create a "pendulum" effect.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏