CSS transform: translate, rotate, scale and transform-origin
Contents
The transform property moves, rotates, scales or skews an element visually, without changing the layout around it. The element’s original space stays reserved, and nothing else on the page moves.
That’s what makes transforms ideal for animations and hover effects: they’re cheap for the browser to draw, and they never cause the page to reflow.
Demo:
Hover (or tap) each box to see its transform.
The transform functions
translate(): move
Moves the element along the X and Y axes. Percentages are relative to the element’s own size, which is what makes translate(-50%, -50%) the classic trick for centering an absolutely positioned element (see position absolute vs relative).
.a { transform: translateX(20px); }
.b { transform: translateY(-10px); }
.c { transform: translate(20px, -10px); }
/* Center inside a positioned parent */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
rotate(): turn
Rotates clockwise for positive values. Units: deg, turn, rad.
.a { transform: rotate(45deg); }
.b { transform: rotate(-0.25turn); } /* 90° counterclockwise */
scale(): resize
1 is the normal size, 2 double, 0.5 half. A negative value mirrors the element: scaleX(-1) flips it horizontally.
.a { transform: scale(1.1); } /* 10% bigger */
.b { transform: scale(2, 0.5); } /* wider and flatter */
.c { transform: scaleX(-1); } /* mirror */
skew(): slant
Tilts the element along an axis. Useful for slanted section backgrounds and “parallelogram” buttons.
.ribbon { transform: skewX(-12deg); }
Combining transforms
Order matters
Read the functions from left to right: each one moves, turns or resizes the element’s coordinate system for the functions after it. So rotating first and then moving goes along the rotated axis, while moving first and then rotating doesn’t.
/* Rotates, then moves 100px along the rotated axis: ends up down-right */
.a { transform: rotate(45deg) translateX(100px); }
/* Moves 100px right, then rotates in place: ends up to the right */
.b { transform: translateX(100px) rotate(45deg); }
Individual properties: translate, rotate, scale
Modern CSS has separate translate, rotate and scale properties. You can change one without repeating the others, and animate them independently. They’re applied in a fixed order: translate, then rotate, then scale.
.card {
translate: 0 0;
rotate: -2deg;
transition: translate 0.2s, scale 0.2s;
}
.card:hover {
translate: 0 -6px; /* the rotation stays */
scale: 1.03;
}
transform-origin
The point the element rotates and scales around. The default is the center (50% 50%). Change it to rotate a door from its hinge, or grow an underline from the left, like the animated underline example.
.door {
transform-origin: left center;
transform: rotateY(-30deg);
}
.underline::after {
transform-origin: left;
transform: scaleX(0);
}
3D transforms
Perspective and rotateY
Add perspective to the parent to give 3D transforms depth: a smaller value means a stronger effect. rotateX and rotateY flip elements around the horizontal and vertical axes. For card flips, hide the back face.
.scene {
perspective: 800px;
}
.card {
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card:hover {
transform: rotateY(180deg);
}
.card__front,
.card__back {
backface-visibility: hidden;
}
.card__back {
transform: rotateY(180deg);
}
Good to know
- Transforms don’t work on inline elements like a plain
<span>or<a>. Give themdisplay: inline-block. - Animate transform and opacity. They can run on the GPU without recalculating layout, so they stay smooth where animating
top,leftorwidthstutters. That’s how the hamburger menu morphs into an X. - A transform creates a containing block.
position: fixedchildren of a transformed element are positioned relative to it, not to the viewport. - It creates a stacking context, which can change how
z-indexbehaves for the element’s children.
FAQ
Does transform affect layout?
No. The element is drawn moved, rotated or scaled, but its original box keeps its place, and other elements don’t move.
How do I apply multiple transforms?
List them in one property, separated by spaces: transform: translateX(10px) rotate(5deg) scale(1.1);. A second transform declaration replaces the first one; it doesn’t add to it.
Why isn’t my transform working?
The element is probably inline (use inline-block), a later rule overwrites the transform, or two functions cancel each other out in that order.
What’s the difference between translate and position relative?
Both move an element without affecting others. translate is better for animation (no layout work) and its percentages refer to the element’s own size; position: relative with top/left refers to the parent.