CSS: CSS Transform

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: Gray🏷 Category: CSS Tutorial

Properties in this section: transform-origin transform perspective backface-visibility

1. Transform Functions

(1) 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

2. translate (Move)

▶ Example

▶ Try it Yourself

3. scale (Resize)

▶ Example

▶ Try it Yourself

4. rotate (Rotate)

▶ Example

▶ Try it Yourself

5. transform-origin

Changes the rotation/scale reference point:

▶ Example

▶ Try it Yourself

6. Combining Transforms

Multiple transforms execute right to left (reverse order):

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

Properties in this section: transform-origin transform perspective backface-visibility

❓ FAQ

Q How do I rotate an element in CSS? How do I rotate by 90 degrees?
A transform: rotate(90deg); — the unit is deg, positive values rotate clockwise, negative counterclockwise, so rotate(-90deg) turns left. A full circle is 360deg; 0.25turn and radians also work. Rotation happens around the element's center by default; change that with transform-origin.
Q How does CSS scale work? Is scale(2) bigger or smaller?
A scale(2) doubles the size, scale(0.5) halves it, 1 is original. scale(1.5, 0.8) controls horizontal and vertical independently; one value scales both axes equally. Scaling doesn't change layout space — the element looks bigger but its original slot stays the same, which is completely different from changing width/height.
Q How does translate work — where does translate(10px, 20px) move the element?
A The two values are horizontal and vertical offsets: positive moves right/down, negative moves left/up, so translate(10px, 20px) shifts 10px right and 20px down. Percentages are relative to the element's own size — translate(50%, 0) moves half its own width. Translation never pushes other elements around, the key difference from margin.
Q What's the difference between transform and changing top/left?
A transform only triggers the composite stage — no layout or paint work, better performance. Changing top/left triggers reflow, and with many elements the page janks. Use transform for animations and interaction; leave top/left for one-time positioning.
Q How does transform: translate(-50%, -50%) with position: absolute center an element?
A The classic three-liner: top: 50%; left: 50%; transform: translate(-50%, -50%);. First pin the element's top-left corner at the parent's center, then translate back by half its own width and height. Percentage translate is based on the element's own size, so it centers even when you don't know the element's dimensions.
Q What is transform-origin and how do I change the rotation center?
A transform-origin sets the reference point for transforms, defaulting to the element's center (center center). transform-origin: top left rotates around the top-left corner — required for fan openings, clock hands and similar effects. Pixels and percentages work too: transform-origin: 0 0 equals the top-left corner.
Q Why does only the last of my two transforms take effect?
A transform is a single property — the later declaration overwrites the earlier one. Chain multiple transforms in one declaration separated by spaces: transform: translate(10px) rotate(45deg);. Order matters: translate-then-rotate differs from rotate-then-translate because rotate turns the coordinate system along with the element.
Q How do I zoom an image slightly on hover?
A Two rules do it: img { transition: transform 0.3s; } img:hover { transform: scale(1.05); }. The key companion is overflow: hidden on the container so the enlarged image doesn't spill out. Keep the factor between 1.03-1.1 with a 0.2-0.4s transition and it feels natural instead of abrupt.
Q What setup do 3D transforms need? What does perspective do?
A When using translateZ(), rotateX(), rotateY() and friends, the parent needs perspective: 1000px — without it there's no visible depth, just a flattened 2D result. Smaller values exaggerate the perspective. 3D transforms run on the GPU, so they animate even smoother than most 2D properties.

📖 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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