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.
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.
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.
Try 2D transforms
Hover over each box to see different transforms in action:
Controlling the pivot: transform-origin
By default, transforms happen around the element's center. The transform-origin property lets you change this 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.
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
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: 1000pxon the parent containerbackface-visibility: hiddenon both facestransform-style: preserve-3don 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-origincontrols the pivot point for rotations and scaling.- You can combine multiple transforms:
transform: translate(20px, 10px) rotate(15deg) scale(1.1); - 3D transforms: Use
perspectiveon a parent,rotateX/Y/Zon children, andbackface-visibility: hiddenfor card flips. - Transforms don't affect document flow โ they're GPU-accelerated and smooth.
- Always use
transitionor CSS animations to make transforms feel natural.
Test your knowledge
translate(50px, 100px) do?transform-origin do?backface-visibility: hidden do?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 โ