On this page10 sections
Lecture 036: 2D & 3D Transforms
| Category | 7: Advanced Styling |
| Lecture | 036 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
transformmoves, rotates, scales and skews an element without disturbing anything around it. With perspective, the same tools work in 3D: cards that flip over, and boxes that turn in space.
Learning Objectives
- Use
translate,rotate,scaleandskew - Combine transforms and change the pivot with
transform-origin - Add 3D depth with
perspective - Build a flip card
1. The 2D Functions
Example: the dashed outline shows each box's original place:
<style>
.wrap { display: inline-block; width: 90px; height: 90px; margin: 18px; outline: 1px dashed #adb5bd; }
.b { width: 90px; height: 90px; background: #4c6ef5; color: white; display: grid; place-items: center; font: 12px monospace; }
</style>
<div class="wrap"><div class="b" style="transform: translate(20px, 10px);">translate</div></div>
<div class="wrap"><div class="b" style="transform: rotate(25deg);">rotate</div></div>
<div class="wrap"><div class="b" style="transform: scale(0.7);">scale</div></div>
<div class="wrap"><div class="b" style="transform: skew(15deg, 5deg);">skew</div></div>| Function | Does |
|---|---|
translate(x, y) | Moves |
rotate(angle) | Turns (deg or turn) |
scale(x, y) | Grows or shrinks (1 = normal size) |
skew(x, y) | Slants |
matrix(…) | All of the above in six numbers (rarely written by hand) |
Modern CSS also has the separate properties translate, rotate and scale:
.icon { rotate: 45deg; scale: 1.2; }2. Combining and the Origin
Several functions are applied right to left, and the order changes the result.
Example
<style>
.b { width: 70px; height: 70px; background: #12b886; margin: 30px; display: inline-block; }
</style>
<div class="b" style="transform: rotate(45deg) translateX(40px);"></div>
<div class="b" style="transform: translateX(40px) rotate(45deg);"></div>
<div class="b" style="transform: rotate(30deg); transform-origin: top left;"></div>transform-origin sets the pivot point: center by default, or top left, 50% 100% and so on.
3. Hover Effects
Example
<style>
.btn { padding: 12px 20px; border: 0; border-radius: 8px; background: #e64980; color: white; transition: transform 0.2s; }
.btn:hover { transform: scale(1.06) rotate(-2deg); }
.btn:active { transform: scale(0.97); }
</style>
<button class="btn">Hover and click</button>4. 3D: Perspective
perspective on the parent sets how far away the viewer is; smaller means more dramatic.
Example
<style>
.scene { perspective: 400px; display: inline-block; margin: 20px; }
.card3d { width: 120px; height: 80px; background: #7048e8; color: white; display: grid; place-items: center; }
</style>
<div class="scene"><div class="card3d" style="transform: rotateY(45deg);">rotateY</div></div>
<div class="scene"><div class="card3d" style="transform: rotateX(50deg);">rotateX</div></div>
<div class="scene"><div class="card3d" style="transform: translateZ(60px);">translateZ</div></div>5. A Flip Card
Example: hover over the card:
<style>
.flip { width: 200px; height: 130px; perspective: 800px; font-family: system-ui; }
.inner { position: relative; width: 100%; height: 100%; transition: transform 0.6s; transform-style: preserve-3d; }
.flip:hover .inner { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; display: grid; place-items: center; border-radius: 12px; backface-visibility: hidden; color: white; }
.front { background: #1971c2; }
.back { background: #2f9e44; transform: rotateY(180deg); }
</style>
<div class="flip">
<div class="inner">
<div class="face front">What is HTML?</div>
<div class="face back">The structure of a web page</div>
</div>
</div>transform-style: preserve-3dkeeps the children in 3D space.backface-visibility: hiddenhides the side facing away.- The back face starts already turned 180°, so it faces you after the flip.
Common Beginner Mistakes
1. Expecting transforms to move other elements. The space in the layout stays where it was.
2. Wrong order of functions. rotate then translate isn't the same as the reverse.
3. 3D without perspective. Without it, 3D rotations look flat.
4. Transforms on inline elements. They have no effect on display: inline. Use inline-block.
Practice Questions
- Move an element 30px right and 10px down.
- Make an element 50% bigger.
- What does
transform-originchange? - Do transforms move neighbouring elements?
- Where do you put
perspective? - Which property hides the back of a flipped card?
Practice Answers
transform: translate(30px, 10px);transform: scale(1.5);- The point the element rotates or scales around.
- No. The element's original space in the layout doesn't change.
- On the parent of the element being rotated in 3D.
backface-visibility: hidden;
Key Takeaways
translate,rotate,scaleandskewchange an element without affecting the layout.- Order matters;
transform-originsets the pivot. perspectiveon a parent +rotateX/YortranslateZgives 3D.preserve-3d+backface-visibility: hiddenmake flip cards.