Tutorials CSS Transforms 2D & 3D
Ch 43 / 60
Chapter 43 Intermediate 10 min read CSS

CSS Transforms — 2D & 3D

The CSS transform property applies geometric changes to an element — moving, rotating, scaling, skewing — without disturbing the document layout. Neighbouring elements don't reflow; the browser composites the transformed element on a separate layer, making transforms fast and ideal for animations. This chapter covers every 2D function (translate, rotate, scale, skew), the transform origin, and the full suite of 3D transforms including perspective, rotateX/Y/Z and translateZ.

What you'll learn
translate(), rotate(), scale(), skew()
Combining multiple transforms
transform-origin — the pivot point
Individual transform properties (new syntax)
3D: perspective, rotateX/Y/Z, translateZ
transform-style, backface-visibility, will-change

How transform Works

The transform property takes one or more transform functions separated by spaces. The key insight: transforms do not affect layout. The element's original space in the document is preserved — only its painted position changes.

✓ Transform — layout unaffected
Box 1
Box 2 ↑
Box 3

Box 2 moved up — Box 3 not pushed aside

✗ position: relative — same visual, same layout effect
Box 1
Box 2 ↑
Box 3

Same visual result — but Box 3's position can shift with position:relative on some layouts

GPU-accelerated
Transforms (especially translate, rotate, scale, opacity) are composited on the GPU — they are the cheapest CSS properties to animate. They do not trigger layout recalculation or paint, making them the preferred approach for smooth 60fps animations. Avoid animating width, height, top, left — they trigger expensive layout passes.

2D Transform Functions

translate() — Move

Moves an element without affecting layout. translateX() for horizontal, translateY() for vertical, or translate(x, y) for both. Percentage values are relative to the element's own dimensions — powerful for centering.

original
tx
translate(16px, -10px)
tx
translateX(20px)
ty
translateY(-16px)
-50%
translate(-50%,-50%)
centering trick

rotate() — Rotate

Rotates around the transform-origin (default: element centre). Accepts degrees (deg), turns (turn), or radians (rad). Positive = clockwise.

30°
rotate(30deg)
45°
rotate(45deg)
90°
rotate(90deg)
-30°
rotate(-30deg)
.25t
rotate(.25turn)
= 90deg

scale() — Resize

Scales the element without affecting surrounding layout. scale(2) doubles both axes; scale(1.5, .5) stretches x and squishes y.

0.5
scale(0.5)
1
scale(1) — original
1.2
scale(1.2)
1.5×
scale(1.5, 0.7)
scX
scaleX(1.6)

skew() — Slant

Slants an element along the X or Y axis. Less used, but helpful for decorative shapes and diagonal dividers.

skX
skewX(20deg)
skY
skewY(15deg)
both
skew(15deg,10deg)
diagonal
diagonal divider

Combining Transforms — Order Matters

Multiple transform functions are applied right to left. The order of functions changes the result:

combo
translate→rotate→scale
translate(8px,-8px) then rotate(15deg) then scale(1.1)
← applied in this visual order
(transforms are applied right-to-left in the matrix)
CSS — all 2D transform functions
/* ── translate ── */
.el { transform: translate(20px, -10px); }  /* x, y */
.el { transform: translateX(20px); }
.el { transform: translateY(-10px); }
.el { transform: translate(-50%, -50%); } /* centering trick */

/* ── rotate ── */
.el { transform: rotate(45deg); }     /* clockwise */
.el { transform: rotate(-45deg); }    /* counter-clockwise */
.el { transform: rotate(0.5turn); }   /* 180° */
.el { transform: rotate(1.5708rad); } /* π/2 = 90° */

/* ── scale ── */
.el { transform: scale(2); }         /* 2× both axes */
.el { transform: scale(1.5, 0.5); }  /* x, y different */
.el { transform: scaleX(1.5); }
.el { transform: scaleY(0.5); }
.el { transform: scale(-1); }       /* mirror / flip */

/* ── skew ── */
.el { transform: skewX(20deg); }
.el { transform: skewY(15deg); }
.el { transform: skew(20deg, 10deg); }

/* ── combining — order matters (right-to-left application) ── */
.el { transform: translate(10px, 0) rotate(45deg) scale(1.2); }
/* ↑ First scale, then rotate, then translate (right-to-left) */

/* Different order = different result */
.a { transform: translate(100px) rotate(45deg); }
.b { transform: rotate(45deg) translate(100px); }
/* .a moves right 100px then rotates around that point
   .b rotates first, then moves 100px in rotated direction */

transform-origin — The Pivot Point

transform-origin sets the point around which transforms are applied. Default is the element centre (50% 50%). Changing it dramatically affects how rotations and scales look:

rot
top left
rot
top center
rot
center (default)
rot
bottom left
rot
bottom right
sc
scale top left
CSS — transform-origin
/* Keywords */
.el { transform-origin: center; }         /* 50% 50% — default */
.el { transform-origin: top left; }      /* 0 0 */
.el { transform-origin: bottom right; }  /* 100% 100% */
.el { transform-origin: top center; }    /* 50% 0 */

/* Percentages */
.el { transform-origin: 0% 0%; }       /* top left */
.el { transform-origin: 25% 75%; }

/* Length values */
.el { transform-origin: 20px 30px; }

/* Real-world patterns */
/* Swing from top (like a door hinge) */
.door { transform-origin: left center; }
.door:hover { transform: rotateY(-30deg); }

/* Scale from bottom (like a tooltip rising) */
.tooltip { transform-origin: center bottom; }
.tooltip { transform: scale(0); }
.parent:hover .tooltip { transform: scale(1); }

/* Rotate underline from left */
.link::after {
  transform-origin: left;
  transform:        scaleX(0);
}
.link:hover::after { transform: scaleX(1); }

/* 3D — add Z axis */
.card { transform-origin: center center -50px; }

Individual Transform Properties (Modern Syntax)

CSS now supports setting transform functions as individual properties — translate, rotate, scale. This is especially useful in animations where you want to transition only one function without overriding the others:

Problem with transform shorthand
.card {
  transform: translateY(0) scale(1);
}
.card:hover {
  /* Must repeat all functions */
  transform: translateY(-8px) scale(1.05);
}
/* Animation can't target just
   translateY independently */
Individual properties ✓
.card {
  translate: 0 0;
  scale: 1;
}
.card:hover {
  translate: 0 -8px; /* only this */
}
/* Animate scale independently
   in keyframes, etc. */
CSS — individual transform properties
/* Individual transform properties (Chrome 104+, Firefox 72+, Safari 14.1+) */

.el {
  translate:  20px -10px;    /* x y (or just x, or x y z) */
  rotate:     45deg;           /* or: x y z angle for 3D axis */
  scale:      1.2;              /* or: x y z */
}

/* Allows independent transitions */
.card {
  translate: none;
  scale: 1;
  transition: translate .3s ease, scale .2s ease;
}
.card:hover {
  translate: 0 -8px;
  scale: 1.03;
}

/* Order of application: translate → rotate → scale
   (always this order regardless of declaration order) */

/* Use with @keyframes */
@keyframes float {
  0%, 100%  { translate: 0 0; }
  50%       { translate: 0 -15px; }
}
.floating { animation: float 3s ease-in-out infinite; }

3D Transforms

3D transforms add depth by introducing a Z axis (toward/away from the viewer). Three components work together: perspective on the parent (sets the depth), transform-style: preserve-3d (keeps children in 3D space), and the 3D transform functions on children.

The 3 axes — rotateX, rotateY, rotateZ
rotateX(45°)
rotateX
tilts top/bottom
rotateY(45°)
rotateY
turns left/right
rotateZ(45°)
rotateZ
= 2D rotate()
rX+rY
rotateX + rotateY
combined
tz 30
translateZ
toward viewer
Pure CSS 3D cube — 6 faces, preserve-3d
Front
Back
Right
Left
Top
Bot
.cube-wrap { perspective: 500px; }
.cube {
  transform-style: preserve-3d;
  transform: rotateX(-20deg) rotateY(30deg);
}
.face-front  { transform: translateZ(40px); }
.face-back   { transform: rotateY(180deg) translateZ(40px); }
.face-right  { transform: rotateY(90deg) translateZ(40px); }
.face-left   { transform: rotateY(-90deg) translateZ(40px); }
.face-top    { transform: rotateX(90deg) translateZ(40px); }
.face-bottom { transform: rotateX(-90deg) translateZ(40px); }
CSS — 3D transform functions and properties
/* ── Step 1: perspective on PARENT ── */
.scene {
  perspective: 800px;   /* distance to the "screen" — lower = more dramatic */
  /* or use perspective() function on child:
     transform: perspective(800px) rotateY(45deg); */
}

/* ── Step 2: 3D transform functions on CHILD ── */
.child {
  transform: rotateX(45deg);     /* tilt: top away, bottom toward */
  transform: rotateY(45deg);     /* turn: right side away */
  transform: rotateZ(45deg);     /* same as rotate(45deg) */
  transform: rotate3d(1,1,0,45deg); /* x y z axis + angle */
  transform: translateZ(50px);   /* toward viewer */
  transform: translateZ(-50px);  /* away from viewer */
  transform: scaleZ(2);          /* depth scale (for 3d objects) */
  transform: translate3d(x,y,z);
  transform: scale3d(1.2,1.2,1.2);
}

/* ── preserve-3d — keep children in 3D space ── */
.scene-3d {
  transform-style: preserve-3d;  /* children share 3D space */
}
.flat     { transform-style: flat; }      /* children flattened — default */

/* ── backface-visibility — hide back of element ── */
.card        { backface-visibility: hidden; }   /* hides when facing away */
.card-back   { transform: rotateY(180deg); }

/* ── Flip card pattern ── */
.flip-card              { perspective: 1000px; }
.flip-card-inner        { transform-style: preserve-3d; transition: transform .6s; }
.flip-card:hover .flip-card-inner { transform: rotateY(180deg); }
.flip-card-front,
.flip-card-back         { backface-visibility: hidden; }
.flip-card-back         { transform: rotateY(180deg); }

Performance — will-change & Compositing

The browser creates a new compositing layer when an element is transformed, keeping it separate from the rest of the page for GPU rendering. will-change hints to the browser to promote an element to its own layer before the animation starts:

✓ GPU-composited properties (cheap)
  • transform (translate, rotate, scale)
  • opacity
  • filter
  • clip-path

No layout recalc · No repaint · Just compositing

✗ Avoid animating (expensive)
  • width, height — triggers layout
  • top, left — triggers layout
  • margin, padding
  • background-color — triggers repaint

Use transform + opacity instead

CSS — performance patterns
/* ✓ Animate with transform instead of position */
.move {
  transform:  translateX(0);   /* ✓ GPU, no layout */
  transition: transform .3s ease;
}
.move:hover { transform: translateX(20px); }

/* ✗ Don't animate left/top (triggers layout) */
.bad      { left: 0; transition: left .3s; }
.bad:hover{ left: 20px; }  /* causes layout recalculation! */

/* will-change — hint for expected transforms */
.animated-card {
  will-change: transform;      /* promotes to own layer */
}
.modal {
  will-change: transform, opacity;
}
/* Remove will-change after animation ends to release memory */
.idle { will-change: auto; }

/* ⚠ Don't over-use will-change — each layer uses GPU memory
   Only apply to elements that WILL animate */

/* Centering with transform (GPU-friendly) */
.centred {
  position:  absolute;
  top:       50%;
  left:      50%;
  transform: translate(-50%, -50%);
}

Complete Transform Reference

Function Syntax What it does
translate() translate(x, y) Move along X and/or Y axis
translateX/Y/Z() translateX(20px) Move along a single axis
rotate() rotate(45deg) Rotate around Z axis (2D)
rotateX/Y/Z() rotateX(45deg) Rotate around a 3D axis
scale() scale(1.5) or scale(x, y) Resize uniformly or per axis
skew() skewX(20deg) Slant along X or Y axis
matrix() matrix(a,b,c,d,tx,ty) 6-value 2D affine transform (all functions combine to this)
perspective() perspective(500px) Sets depth on the element itself (vs on parent)
translate3d() translate3d(x,y,z) 3D translate — forced GPU layer
scale3d() scale3d(x,y,z) 3D scale
rotate3d() rotate3d(x,y,z,angle) Rotate around custom axis vector
Free Resource
CSS3 Cheat Sheet — All Properties & Transform Functions
Complete reference covering all CSS 2D and 3D transform functions with examples.
Download PDF

Try It Yourself

All 2D and 3D transforms with hover animations — hover over each card to see the transform:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 43

Questions & Answers
When you apply a transform, the element's original space in the document flow is preserved — the browser treats it as if the element hasn't moved at all from a layout perspective. The transform only changes where the element is painted, not where it exists in the layout grid.

This means: surrounding elements don't reflow when you translate an element. The box model (margins, padding, width, height) stays identical. Other elements don't "see" the visual change.

Advantages:
1. Performance: no layout recalculation triggered — the browser can composite the transformed layer on the GPU without touching the rest of the DOM
2. Smooth animations: because no layout pass is needed, transform animations can run at 60fps without janking
3. Predictable: you know the transform won't accidentally push or pull other elements

This is why transforms (especially translate, rotate, scale) are the gold standard for CSS animations — use transform: translateX(20px) instead of left: 20px.
Transform functions are applied as matrix multiplications, and matrix multiplication is not commutative — the order of operations changes the result.

The functions are applied right to left (mathematically), which visually means the last function listed applies first. However, it's easier to think of it this way:

transform: translate(100px, 0) rotate(45deg)
— The element is first rotated 45°, then translated 100px in the rotated direction (which is diagonal, not right)

transform: rotate(45deg) translate(100px, 0)
— The element is first translated 100px to the right, then rotated 45° around the origin

Both end up at different positions with different orientations. The practical rule: write transforms in the order you intend them to happen visually — translate first if you want to move then rotate; rotate first if you want the translation to follow the rotation.
The transform shorthand takes a space-separated list of functions. Overriding any part of it requires rewriting the entire list — this creates issues with animations and transitions where you want to independently control different transform functions.

Individual transform properties (translate, rotate, scale) set each function separately:

translate: 0 -8px; scale: 1.05;

This means you can transition only translate on hover, then independently animate scale with a keyframe animation — they don't cancel each other out.

Another difference: individual properties always apply in a fixed order — translate → rotate → scale — regardless of declaration order in CSS. The shorthand applies in the order you write them.
A CSS flip card needs three key properties working together:

1. perspective on the wrapper: sets the depth perception. Values like 600px–1000px work well. Smaller = more dramatic 3D.
.flip-wrap { perspective: 800px; }

2. transform-style: preserve-3d on the inner container: keeps the front and back faces in shared 3D space. Without this, both faces flatten to the same plane.
.flip-inner { transform-style: preserve-3d; transition: transform .6s; }

3. backface-visibility: hidden on both faces: hides each face when it's rotated away from the viewer (facing backwards), so you only ever see one face at a time.
.flip-front, .flip-back { backface-visibility: hidden; }
.flip-back { transform: rotateY(180deg); }

The actual flip is triggered by: .flip-wrap:hover .flip-inner { transform: rotateY(180deg); }
will-change hints to the browser that an element is about to be animated, so it can create a new compositing layer in advance — avoiding the stuttery first frame that sometimes appears when an animation starts.

will-change: transform; — promotes to GPU layer for transform animations
will-change: transform, opacity; — both

When to use:
— Elements that animate frequently (looping spinners, parallax elements)
— Complex animations where you notice a jank on first run
— Apply it before the animation, ideally on the element's base state

Risks:
— Every will-change element uses GPU memory (VRAM) for its compositing layer
— Applying to many elements can exhaust GPU memory, degrading performance on low-end devices
— Over-using it negates the benefit — the GPU has to manage too many layers

Best practice: Apply only to elements that genuinely animate, remove it when animation is idle with will-change: auto (can do this via JavaScript after the animation ends).
Multiple Choice Questions
Question 1 of 5
An element has transform: translateX(50px). What happens to the space it originally occupied?
Question 2 of 5
What does transform: translate(-50%, -50%) do when applied to an absolutely positioned element with top: 50%; left: 50%?
Question 3 of 5
Which property must be set on the parent to make 3D transforms on children have visible depth?
Question 4 of 5
Why is animating transform: translateX() better for performance than animating left:?
Question 5 of 5
What does transform-origin: top left change about rotate(45deg)?
Chapter Summary
  • transform applies geometric changes without affecting layout. The element's original space is preserved. Transforms are GPU-composited — ideal for animations.
  • translate(x, y): moves without affecting layout. Percentage values are relative to the element's own size — translate(-50%, -50%) is the classic centering technique.
  • rotate(angle): rotates around the transform-origin. Accepts deg, turn, rad. Positive = clockwise.
  • scale(n): resizes without affecting layout. scale(-1) flips/mirrors. scale(x, y) stretches per axis.
  • skew(): slants. Useful for diagonal decorative elements.
  • Order matters: functions are applied right-to-left. translate → rotate ≠ rotate → translate.
  • transform-origin: sets the pivot point. Default: center. Use top left for door hinges, bottom for popping tooltips, left for underline expand effects.
  • Individual properties: translate, rotate, scale as separate properties — allows independent transitions. Always applied: translate → rotate → scale.
  • 3D requires: perspective on parent + transform-style: preserve-3d on container + backface-visibility: hidden on faces.
  • will-change: transform: hints GPU layer promotion. Use sparingly — only on elements that animate frequently. Release with will-change: auto when idle.
  • Performance: animate transform and opacity. Never animate width, height, top, left — they trigger expensive layout passes.
~10 min read
Intermediate level
Chapter 43 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel