What Are CSS3 Transform Functions and How Do They Work?
Updated
CSS3 transform functions change how an element is drawn — moving, rotating, scaling, or skewing it — without affecting the layout of surrounding elements. You apply them through the transform property, and they work in both 2D and 3D space. Use them when you want visual effects like hover animations, card flips, or subtle motion; avoid them when you actually need to change an element's position in the document flow, since transforms are purely visual.
The Core 2D Functions
Each function takes one or more arguments and returns a transformed version of the element.
| Function | What it does | Example |
|---|---|---|
translate(x, y) |
Moves the element along the X and Y axes | translate(20px, -10px) |
rotate(angle) |
Rotates around the Z axis (2D spin) | rotate(45deg) |
scale(x, y) |
Resizes by a multiplier | scale(1.5, 0.8) |
skew(x-angle, y-angle) |
Slants the element along an axis | skew(10deg, 5deg) |
A simple hover effect:
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: scale(1.05) rotate(2deg);
}
The element grows slightly and tilts on hover. Because transforms don't trigger layout recalculation, this stays smooth.
3D Functions and Perspective
3D transforms add depth. The key functions are rotateX(), rotateY(), rotateZ(), and translate3d(). But rotation alone looks flat unless you add perspective.
perspective defines how far the viewer is from the Z=0 plane. A smaller value creates a more dramatic 3D effect:
.scene {
perspective: 800px;
}
.box {
transform: rotateY(45deg);
}
Without perspective, rotateY(45deg) just squashes the element horizontally. With it, the element appears to turn in space, with one edge closer to the viewer.
You can also apply perspective per-element with perspective() inside the transform:
.box {
transform: perspective(600px) rotateX(30deg);
}
The visualizer at css-transform.moro.es lets you drag sliders for rotation, perspective, and scaling and see the result immediately — useful for finding values before committing them to code.
Transform Origin: Changing the Pivot
By default, transforms pivot around the element's center (50% 50%). transform-origin moves that pivot:
.box {
transform-origin: top left;
transform: rotate(30deg);
}
Now the element rotates around its top-left corner instead of its center. This is one of the most common sources of "why does it rotate like that?" confusion. If a rotation or scale looks wrong, check transform-origin first.
How Multiple Functions Combine
When you list several functions, they apply right to left — the last function in the list is applied first to the element's coordinate system.
transform: translateX(100px) rotate(45deg);
This rotates the element 45° first, then translates it 100px along the (now rotated) X axis. Swap the order and you get a different result:
transform: rotate(45deg) translateX(100px);
Here the element moves along the rotated axis, so it travels diagonally. Order matters, and it's the second most common gotcha after transform-origin.
Where to Experiment
The CSS Transform Functions Visualizer (css-transform.moro.es) is a playground for exactly this. Its interface covers Rotation (2D), Rotation (3D), Perspective, Transform Origin, and Scaling (Resizing), with front/back views for 3D. Adjust a value, watch the element respond, then copy the resulting transform into your stylesheet. This beats guessing numbers and refreshing a browser.
Practical Notes
- Transforms don't affect surrounding layout — the element's original space is preserved.
- They combine well with
transitionfor animation, but for complex sequences use@keyframes. - 3D transforms need
perspectiveon the parent (orperspective()in the transform) to look three-dimensional. - Always verify function order and
transform-originwhen a result surprises you.