What are CSS transforms?

CSS transforms let you modify the shape, position, and rotation of elements without affecting the document flow. Unlike moving elements with position or margin, transforms happen entirely in the browser's rendering layer โ€” making them smooth and performant.

There are two types: 2D transforms (moving, rotating, scaling in a flat plane) and 3D transforms (adding depth and perspective). Both are GPU-accelerated, so they run smoothly even on mobile devices.

Key benefit:
Transforms don't trigger layout recalculations. They're fast, smooth, and perfect for animations and interactive effects.

2D Transforms

The transform property accepts several functions. Here are the most common 2D ones:

translate(x, y) โ€” Move an element

translate() moves an element horizontally and/or vertically. translate(50px, 100px) moves an element 50px to the right and 100px down. You can also use translateX() and translateY() for one direction at a time.

rotate(deg) โ€” Rotate an element

rotate(45deg) spins an element 45 degrees clockwise. Negative values rotate counter-clockwise. The rotation happens around the element's center by default.

scale(x, y) โ€” Resize an element

scale(1.5, 0.8) stretches an element to 150% width and 80% height. Use scaleX() or scaleY() for one direction. scale(1.2) scales both equally.

skew(x, y) โ€” Slant an element

skew(20deg, 10deg) slants the element 20 degrees on the X-axis and 10 degrees on the Y-axis. Use skewX() or skewY() for one direction.

2d-transforms.css Common 2D transform functions

You can combine multiple transforms in one line. The order matters: translate(30px, 20px) rotate(15deg) scale(1.1) first moves, then rotates, then scales.

Controlling the pivot: transform-origin

By default, transforms happen around the element's center. The transform-origin property lets you change this pivot point.

transform-origin.css Setting the pivot point

Common values: top left, top center, top right, center left, center center (default), center right, bottom left, bottom center, bottom right. You can also use pixels or percentages.

3D Transforms

CSS also supports transforms in 3D space. You need two things:

1. perspective โ€” Enable 3D space

Apply perspective to a parent element to create a 3D viewing space. Think of it as the "distance from the viewer's eye." Lower values make the 3D effect stronger. perspective: 1000px is a good default.

2. rotateX, rotateY, rotateZ โ€” Rotate in 3D

rotateX(45deg) rotates around the horizontal axis (flips forward/back). rotateY(45deg) rotates around the vertical axis (flips left/right). rotateZ(45deg) is the same as regular rotate().

3. backface-visibility โ€” Hide the back

backface-visibility: hidden hides the back of an element when it's rotated away from you. Perfect for card flips where you don't want to see the back showing through.

3d-transforms.css 3D transforms with perspective

Try 3D card flip

Hover over the card to flip it. This uses perspective, rotateY(180deg), and backface-visibility: hidden:

Alice Chen

Full Stack Developer

Hover to see contact

Get in Touch

alice@email.com

github.com/alice

linkedin.com/in/alice

โœ๏ธ Try It Yourself โ€” 3D Card Flip
Preview
Challenge: Build a 3D Profile Card

Task: Create a card that flips on hover with:

  • Front side: A profile photo (you can use a colored gradient), name, and job title
  • Back side: Contact information (email, phone, social links)
  • Smooth 3D flip animation using rotateY(180deg)
  • perspective: 1000px on the parent container
  • backface-visibility: hidden on both faces
  • transform-style: preserve-3d on the card

Use the Editor 2 above as a starting point, then customize with your own colors and content.

Hint: Create a container with perspective: 1000px. Inside, add a card div with transform-style: preserve-3d. Add two child divs for the front and back faces. Set the back face to transform: rotateY(180deg) and both to backface-visibility: hidden. On container hover, apply transform: rotateY(180deg) to the card.

Key takeaways

  • 2D transforms: translate(), rotate(), scale(), skew() move, spin, resize, and slant elements.
  • transform-origin controls the pivot point for rotations and scaling.
  • You can combine multiple transforms: transform: translate(20px, 10px) rotate(15deg) scale(1.1);
  • 3D transforms: Use perspective on a parent, rotateX/Y/Z on children, and backface-visibility: hidden for card flips.
  • Transforms don't affect document flow โ€” they're GPU-accelerated and smooth.
  • Always use transition or CSS animations to make transforms feel natural.

Test your knowledge

Question 1 of 5
What does translate(50px, 100px) do?
Question 2 of 5
How do you rotate an element 45 degrees?
Question 3 of 5
What does transform-origin do?
Question 4 of 5
What property enables 3D space in CSS?
Question 5 of 5
What does backface-visibility: hidden do?
0
/5
Great work! You understand CSS transforms.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered CSS transforms.
Imagine the interactive experiences you'll build.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’