CSS3 2D Transforms
CSS 2D transforms move, rotate, scale and skew an element on a flat plane without changing the space it takes up in the layout. You use them for hover effects, icons that spin, cards that lift, centring tricks and smooth animations. Because the browser can usually apply transforms on the graphics card, animating them is much smoother than animating top, left or width.
The transform functions
| Function | What it does | Example |
|---|---|---|
translate(x, y) | Moves the element | translate(20px, -10px) |
rotate(angle) | Turns it clockwise for positive angles | rotate(45deg) |
scale(x, y) | Grows or shrinks it; one value scales both axes | scale(1.1) |
skew(x, y) | Slants it along the axes | skewX(-12deg) |
matrix(a, b, c, d, e, f) | Combines all of the above in one | Rarely written by hand |
Modern browsers also support the separate properties translate, rotate and scale, which let you change one part without rewriting the whole transform value.
Order changes the result
When you list several functions, they are applied in sequence, and each one works in the coordinate system left by the one before. rotate(90deg) translateX(100px) moves the element downward, because after rotating, the x-axis points down. translateX(100px) rotate(90deg) moves it right and then rotates it in place. If a transform lands somewhere unexpected, the order is the first thing to check.
Choosing the pivot with transform-origin
Rotation and scaling happen around the element's centre by default. transform-origin moves that pivot. A door-like swing uses transform-origin: left center; a dropdown that grows from its top uses transform-origin: top.
.card {
transition: transform 200ms ease, box-shadow 200ms ease;
}
.card:hover {
transform: translateY(-4px) scale(1.02);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}
.chevron {
transition: rotate 150ms ease;
}
.accordion[open] .chevron {
rotate: 180deg;
}
/* Centre a box inside a positioned parent */
.overlay-label {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
In the centring trick, the percentages inside translate refer to the element's own size, which is why it shifts back by exactly half its width and height.
Pitfalls to avoid
Transforms do not reflow the page. A scaled-up element overlaps its neighbours instead of pushing them aside, and a translated element still leaves its original gap. That is useful for animation, but surprising if you expected layout to adjust.
A second trap is that a transform on an element creates a new containing block for its position: fixed children and a new stacking context. A modal placed inside a transformed wrapper will be positioned relative to that wrapper instead of the viewport, and its z-index may stop working as expected. Keep fixed overlays outside transformed parents.
Lastly, setting transform on hover replaces any transform already applied. If an element is centred with translate(-50%, -50%) and the hover rule writes transform: scale(1.1), the centring disappears. Repeat the translate in the hover rule, or use the separate scale property.
Accessibility note
Large or continuous motion can make some people feel unwell. Wrap bigger animations in @media (prefers-reduced-motion: no-preference) so users who ask for less motion get a calmer page.
Takeaways
Use transforms for visual movement, not layout. Mind the order of functions, set transform-origin when the pivot matters, animate them with transitions, and keep fixed-position overlays out of transformed containers.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.