CSS: CSS Transform
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 Yourself3. scale (Resize)
▶ Example
▶ Try it Yourself4. rotate (Rotate)
▶ Example
▶ Try it Yourself5. transform-origin
Changes the rotation/scale reference point:
▶ Example
▶ Try it Yourself6. 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 */
}
❓ 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
transformis the foundation of CSS animation — translate, scale, rotate, skew can combine into countless effects- Transforms don't affect document flow (element "looks" different but occupies same space)
- 3D transforms need
perspectivefor depth effects - When combining transforms, put translate last (code order first)
📝 Exercises
- Create a box that moves right 50px and scales 1.5x on hover.
- Create an image that rotates 180° around Y-axis on hover (needs
perspective). - Create a "flip card" — hover flips card to show back content (using
rotateYandbackface-visibility). - Use
transform-originto change rotation center, create a "pendulum" effect.
🧩 Related Examples
3D Press ButtonA solid box-shadow acts as the button's side; on :active the button translates down onto its own shadow for…
3D Flip Cardperspective sets the 3D depth, rotateY(180deg) flips the card, and backface-visibility: hidden keeps both fa…
Border Draw ButtonHover a transparent button and its border appears to be drawn: two pseudo-element lines growing with scaleX/…