How to Use CSS Transforms: Rotate, Scale, Translate, Skew
CSS transforms change an element's shape, size, position, and rotation without affecting layout โ the element's original space is preserved. This makes transforms ideal for animations and hover effects because they don't trigger layout recalculation, which means smooth, GPU-accelerated performance.
- Transform elements with CSS: rotate, scale, translate, and skew.
- Covers basic 2d transforms.
- Covers combining transforms.
- Covers transform origin.
- Covers 3d transforms.
Basic 2D Transforms
translate(x, y) โ moves the element. translateX(50px) shifts right 50px. translate(-50%, -50%) centers an element relative to its position.
rotate(angle) โ rotates clockwise. rotate(45deg) turns 45 degrees. Negative values rotate counterclockwise.
scale(x, y) โ resizes. scale(1.5) enlarges 150%. scale(0.5) shrinks to 50%. scale(-1) mirrors/flips.
skew(x, y) โ tilts. skewX(10deg) tilts horizontally.
Combining Transforms
Chain transforms in one declaration:
gap instead of margin hacks for flexbox spacing. It keeps layout code cleaner and is supported in all modern browsers.transform: translateY(-10px) rotate(5deg) scale(1.05);
Order matters โ transforms are applied right to left. translate then rotate gives different results than rotate then translate. For hover effects, the most common combination is translate + scale for a 'lift and grow' effect.
Transform Origin
transform-origin sets the pivot point for rotations and scaling. Default is center. Top-left: transform-origin: 0 0. Bottom-right: transform-origin: 100% 100%. Custom: transform-origin: 20px 80%. This is critical for door-swing animations, card flips, and fan-out effects. The CSS Transform Playground lets you adjust the origin visually.
width, height, top, or left. These trigger layout recalculations on every frame and can drop performance below 60fps.3D Transforms
perspective() enables 3D space. rotateX() and rotateY() rotate in 3D. translateZ() moves toward/away from the viewer:
.card-container { perspective: 1000px; }
.card:hover { transform: rotateY(180deg); }
Set perspective on the parent, then rotate/translate children in 3D. Higher perspective values create subtler 3D effects; lower values create dramatic distortion.
Frequently Asked Questions
What is CSS transform?
What is transform-origin?
Are CSS transforms GPU-accelerated?
Use the CSS Transform Playground โ free, no signup required.
โก Open CSS Transform Playground