02. CSS Transforms (2D Translate, Scale, Rotate, 3D & Perspective)
🟡 Intermediate
1. Definition
CSS Transforms modify element visual rendering geometry without affecting document layout flow: translate(x, y), scale(ratio), rotate(deg), skew(), transform-origin, 3D perspective, rotateY(), and backface-visibility.
2. Why It Is Used
Transforms run hardware-accelerated on GPU composite layers, achieving 60fps animations without causing expensive CPU main-thread reflows.
3. How It Works
Transforms apply a 2D or 3D affine matrix calculation to the element's visual box layer, shifting or rotating it relative to transform-origin (default center).
4. Syntax
.flip-card {
transform: rotateY(180deg);
transform-origin: center;
}
5. Transform Function Types Reference
| Function | Operation | 2D / 3D Context |
|---|---|---|
translate(x, y) | Shifts position along X and Y axes | 2D & 3D (translate3d) |
scale(x, y) | Resizes element dimensions proportionally | 2D & 3D (scale3d) |
rotate(angle) | Rotates element clockwise or counter-clockwise | 2D & 3D (rotateY, rotateX) |
perspective(px) | Sets 3D depth distance for children | 3D Container level |
6. Beginner Example
.icon-hover:hover {
transform: scale(1.1);
}
7. Intermediate Example
.card-tilt:hover {
transform: translateY(-5px) rotate(2deg);
}
8. Advanced 3D Card Flip Example
.flip-container {
perspective: 1000px;
}
.flip-inner {
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip-container:hover .flip-inner {
transform: rotateY(180deg);
}
.card-back {
transform: rotateY(180deg);
backface-visibility: hidden;
}
9. Line-by-Line Code Explanation
perspective: 1000px;: Defines 3D focal depth distance for child elements undergoing 3D rotation.backface-visibility: hidden;: Hides the reverse side of the card when flipped away from the viewer.
10. Expected Terminal / Browser Output
A 3D card component that flips around its Y-axis cleanly when hovered.
11. Common Mistakes & Pitfalls
- Forgetting
perspectiveon 3D container: Withoutperspective, 3D rotations appear flat and 2D.
12. Important Rules & Limitations
- Transforms do not affect neighboring element layout positions—only the transformed element's visual rendering box changes.
13. Multilingual Explanation: English
CSS Transforms alter element geometry (translate, scale, rotate) on the GPU without triggering document layout reflows.
14. Multilingual Explanation: Hindi (Roman Script)
Transforms scale, rotate, aur translate functions se elements ko GPU par fast animate karte hain.
15. Multilingual Explanation: Marathi (Roman Script)
Transforms mule elements rotate, scale kiva translate karta येतात GPU acceleration sobat.
16. Practice Questions
- What is the function of
backface-visibility: hiddenin 3D card flips?
17. Practical Coding Challenge
Write a CSS rule for .card-hover:hover that combines translateY(-8px) and scale(1.02) with a smooth transition.