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.
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.
Box 2 moved up — Box 3 not pushed aside
Same visual result — but Box 3's position can shift with position:relative on some layouts
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.
rotate() — Rotate
Rotates around the transform-origin (default:
element centre). Accepts degrees (deg), turns (turn), or radians (rad). Positive =
clockwise.
scale() — Resize
Scales the element without affecting surrounding layout. scale(2) doubles both axes; scale(1.5, .5) stretches x and squishes y.
skew() — Slant
Slants an element along the X or Y axis. Less used, but helpful for decorative shapes and diagonal dividers.
Combining Transforms — Order Matters
Multiple transform functions are applied right to left. The order of functions changes the result:
← applied in this visual order
(transforms are applied right-to-left in the matrix)
/* ── 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:
/* 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:
/* 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.
tilts top/bottom
turns left/right
= 2D rotate()
combined
toward viewer
.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); }
/* ── 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:
transform(translate, rotate, scale)opacityfilterclip-path
No layout recalc · No repaint · Just compositing
width,height— triggers layouttop,left— triggers layoutmargin,paddingbackground-color— triggers repaint
Use transform + opacity instead
/* ✓ 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 |
Try It Yourself
All 2D and 3D transforms with hover animations — hover over each card to see the transform:
Quick Check — Chapter 43
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.
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.
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.
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
animationswill-change: transform, opacity; — bothWhen 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).
transform: translateX(50px).
What happens to the space it originally occupied?transform: translate(-50%, -50%) do
when applied to an absolutely positioned element with top: 50%; left: 50%?transform: translateX()
better for performance than animating left:?transform-origin: top left change
about rotate(45deg)?- 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. Usetop leftfor door hinges,bottomfor popping tooltips,leftfor underline expand effects. - Individual properties:
translate,rotate,scaleas separate properties — allows independent transitions. Always applied: translate → rotate → scale. - 3D requires:
perspectiveon parent +transform-style: preserve-3don container +backface-visibility: hiddenon faces. - will-change: transform: hints GPU layer promotion. Use sparingly — only on elements
that animate frequently. Release with
will-change: autowhen idle. - Performance: animate transform and opacity. Never animate width, height, top, left — they trigger expensive layout passes.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.