# Transforms (2D & 3D)

> Move, scale, spin, and skew elements without breaking your page layout.

Subject: CSS | Lesson 32 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/transforms
Previous: [Keyframe Animations](https://codewithanimation.in/css/keyframe-animations.md) | Next: [Scroll-Driven & Reduced Motion](https://codewithanimation.in/css/scroll-driven-and-reduced-motion.md)

## What are Transforms?

The `transform` property lets you visually manipulate an element in 2D or 3D space. You can move it (`translate`), resize it (`scale`), spin it (`rotate`), or distort it (`skew`).

Crucially, transforms happen in a separate compositing layer. This means you can wildly move an element around the screen, and the surrounding text and layout won't flinch at all.

## Why are they important?

Transforms are the absolute backbone of modern web animation. If you try to animate an element moving across the screen by changing its `margin-left` or `top` properties, the browser has to recalculate the entire page layout on every single frame, causing horrible lag.

Because transforms are hardware-accelerated by the GPU, they guarantee a buttery-smooth 60fps experience.

## How do you use them?

You declare the `transform` property and chain one or more functions together. For example: `transform: translate(50px, 100px) rotate(45deg);`.

By default, all transforms originate from the exact dead-center of the element. You can change this anchor point using the `transform-origin` property (like setting it to `top left`).

## Common mistake: The stacking context trap

Applying any `transform` to an element (even `translate(0)`) automatically turns that element into a new containing block and stacking context. This means it will suddenly trap `z-index` and `position: absolute` children that used to escape it.

## Tip: Unlock 3D space

You can rotate elements on the X and Y axes (`rotateX`, `rotateY`) to create 3D flips. But to make it look like true 3D instead of a flat squish, you must apply the `perspective` property to the element's parent container!

## Try it: Try it

Use the sliders to manipulate the card in 2D and 3D space. Notice how the code updates to chain the transform functions together.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/transforms

## Check yourself

### Question 1

Why is it a best practice to animate transform instead of margin or top/left?

- A. Transforms can be animated by the GPU without recalculating page layout.
- B. Transforms are automatically responsive to mobile devices.
- C. Browsers block animations on the margin property for security reasons.

Correct answer: A

GPU acceleration is the key. Layout properties (like margin, padding, width) force the CPU to recalculate the document flow, while transforms just paint the existing pixels in a new location.

- Why not B: Transforms use absolute units or percentages of the element itself, they don't inherently make things responsive.
- Why not C: You can animate margins, it just results in terrible, jittery performance.

### Question 2

Which property changes the anchor point that an element rotates or scales around?

- A. transform-anchor
- B. transform-origin
- C. perspective-origin

Correct answer: B

By default, `transform-origin` is `50% 50%` (the center). Changing it to `0% 0%` will make the element swing from its top-left corner like a hinge.

- Why not A: That property does not exist in CSS.
- Why not C: This controls the vanishing point for 3D space, not the physical anchor of the element.

### Question 3

If you apply transform: scale(2) to a 100px box, what happens to the surrounding text?

- A. The text moves out of the way to make room for the 200px box.
- B. The box visually grows to 200px and completely overlaps the surrounding text.
- C. The box is restricted and cannot grow past its original 100px layout boundary.

Correct answer: B

Because transforms don't trigger layout shifts, the browser still thinks the box is 100px wide. The visual scaling happens 'above' the page, causing it to overlap anything nearby.

- Why not A: Transforms do not affect the document flow.
- Why not C: Transforms operate in a separate visual layer and happily ignore layout boundaries.

## Remember this

- Always use transform (never margins) for moving or scaling animations.
- Chain multiple functions on one property: transform: scale(1.5) rotate(10deg).
- Use transform-origin to change the anchor point from the center to an edge.
- Remember that applying a transform creates a new stacking context.
