On this page10 sections
  1. Learning Objectives
  2. 1. The 2D Functions
  3. 2. Combining and the Origin
  4. 3. Hover Effects
  5. 4. 3D: Perspective
  6. 5. A Flip Card
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 036: 2D & 3D Transforms

Category7: Advanced Styling
Lecture036 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

transform moves, 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, scale and skew
  • 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:

html
<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>
FunctionDoes
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:

css
.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

html
<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

html
<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

html
<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:

html
<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-3d keeps the children in 3D space.
  • backface-visibility: hidden hides 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

  1. Move an element 30px right and 10px down.
  2. Make an element 50% bigger.
  3. What does transform-origin change?
  4. Do transforms move neighbouring elements?
  5. Where do you put perspective?
  6. Which property hides the back of a flipped card?

Practice Answers

  1. transform: translate(30px, 10px);
  2. transform: scale(1.5);
  3. The point the element rotates or scales around.
  4. No. The element's original space in the layout doesn't change.
  5. On the parent of the element being rotated in 3D.
  6. backface-visibility: hidden;

Key Takeaways

  • translate, rotate, scale and skew change an element without affecting the layout.
  • Order matters; transform-origin sets the pivot.
  • perspective on a parent + rotateX/Y or translateZ gives 3D.
  • preserve-3d + backface-visibility: hidden make flip cards.