Sub topic 6
Moving, Scaling and Rotating with transform
This chapter assumes you know basic CSS selectors, the cascade, and how to write a transition on a hover state. It covers the transform property as an animation target: what each function does, how the order of functions changes the result, and the mistakes that break animations.
Why transform is the main animation target
The transform property changes how an element is drawn without changing its place in the layout. Neighboring elements do not move. The element's box in the normal flow stays where it was, and only the painted result is shifted, scaled, rotated or skewed.
This makes transform a good fit for motion. Animating margin-left forces the browser to recompute layout on every frame. Animating transform: translateX() usually does not. The performance chapter covers the cost in detail. Here the focus is how to write transforms correctly.
The core transform functions
You give transform one or more functions. Each function does one job.
| Function | Effect | Example |
|---|---|---|
| `translate(x, y)` | Moves the element | `translate(20px, -10px)` |
| `translateX(x)`, `translateY(y)` | Moves along one axis | `translateY(-2px)` |
| `scale(n)` or `scale(x, y)` | Resizes by a factor | `scale(1.1)` |
| `rotate(angle)` | Rotates around the origin point | `rotate(45deg)` |
| `skew(x, y)` | Slants the element | `skewX(10deg)` |
A few details matter in practice:
- Percentages in
translaterefer to the element's own size, not the parent's.translateX(100%)moves an element by exactly its own width. This is useful for slide-in panels whose width you do not know. scale(1)means the original size.scale(0)makes the element invisible, andscale(-1)flips it.- Angles accept
deg,rad,gradandturn.rotate(1turn)is a full circle. - Positive
rotatevalues turn clockwise. PositivetranslateYvalues move down.
A first transform transition
The hover button from the introduction uses translateY(-2px) to lift slightly. Here is a card that tilts and grows when you point at it.
.card {
transform: rotate(0deg) scale(1);
transition: transform 250ms ease-out;
}
.card:hover,
.card:focus-within {
transform: rotate(2deg) scale(1.03);
}Both states list the same functions in the same order. This matters, and the next sections explain why.
Function order changes the result
When you list several functions, the browser applies them to the element's coordinate system one after another. Each function changes the coordinate system for the functions after it. As a result, order affects the final position.
Compare these two rules:
.a {
transform: translateX(100px) rotate(45deg);
}
.b {
transform: rotate(45deg) translateX(100px);
}In .a, the element moves 100px to the right and then rotates in place. In .b, the element first rotates, which turns its x-axis toward the lower right. The 100px move then follows that rotated axis, so the element ends up diagonally down and to the right.
A common rule of thumb is to put translate first when you want to position the element in screen directions, and to put rotate first when you want it to travel along its own rotated axis.
transform-origin sets the pivot
rotate and scale work around a point called the transform origin. The default is 50% 50%, the center of the element. You can change it with transform-origin.
.door {
transform-origin: left center;
transform: rotateY(0deg);
transition: transform 400ms ease-in-out;
}
.door.open {
transform: perspective(600px) rotateY(-70deg);
}With the origin on the left edge, the element swings like a door on a hinge. With the default center origin, the same rotation would spin it around its middle.
Origin does not affect translate. Moving by 20px is the same from any pivot. It does affect scale: with transform-origin: top left, a scaled element grows toward the bottom right instead of in all directions.
Interpolation: why matching function lists matter
During a transition, the browser calculates in-between values. For transforms, it interpolates function by function when both lists have the same functions in the same order.
.good {
transform: translateX(0) rotate(0deg);
transition: transform 300ms;
}
.good:hover {
transform: translateX(50px) rotate(90deg);
}Here the browser blends translateX with translateX and rotate with rotate. The motion is smooth and predictable.
If the lists do not match, the browser falls back to converting both to a single matrix and interpolating the matrix numbers. That still runs, but the path can look odd. A rotation of 360 degrees can collapse into no movement at all, because a rotation by 360deg and by 0deg produce the same matrix.
.matrix-fallback {
transform: none;
transition: transform 600ms;
}
.matrix-fallback:hover {
transform: rotate(360deg);
}none is a special case that the browser handles well by filling in an identity version of each function, so this particular example does spin. Mismatched lists such as translateX(10px) to rotate(90deg) scale(2) are where surprises appear. To stay safe, write the same list in both states and use neutral values such as 0, 0deg and 1 in the resting state.
The individual transform properties
Modern browsers also support translate, rotate and scale as separate properties.
.icon {
translate: 0 0;
rotate: 0deg;
scale: 1;
transition: translate 200ms, rotate 400ms, scale 200ms;
}
.icon:hover {
translate: 0 -4px;
rotate: 15deg;
scale: 1.2;
}This has two benefits:
- Each property can have its own duration, delay and easing, as shown above.
- One effect does not overwrite another. With a single
transform, a hover rule that setstransform: scale(1.2)replaces atransform: rotate(15deg)set elsewhere. With separate properties, they combine.
The browser applies the individual properties first, in the order translate, rotate, scale, and then applies transform after them. Check browser support for your audience before relying on them, and keep the transform form as the fallback when you need older browsers.
Transforms and layout side effects
Transforms are visual only, and that has consequences you should know:
- The layout box does not change. A scaled-up element can overlap its neighbors, and scrollable overflow may grow if the transformed element extends beyond its container.
- Hit testing follows the transformed shape. The clickable area moves with the element. A button that shifts away from the pointer on hover can lose the hover state, flicker, and cause a loop. Avoid large moves on the same element that listens for the hover. A common fix is to apply the hover to a wrapper and the transform to the child.
- A transformed element creates a new stacking context and becomes the containing block for
position: fixeddescendants. A fixed-position child inside a transformed parent will position relative to that parent, not the viewport. - Text can look blurry at fractional pixel positions or during scaling, especially in some browsers. Rounding pixel values often helps.
Transforms in keyframe animations
Everything above applies in @keyframes. Because the same function list rule holds, keep the lists parallel in every keyframe.
@keyframes wobble {
0%, 100% {
transform: translateX(0) rotate(0deg);
}
25% {
transform: translateX(-6px) rotate(-3deg);
}
75% {
transform: translateX(6px) rotate(3deg);
}
}
.alert {
animation: wobble 500ms ease-in-out 2;
}Each keyframe lists translateX then rotate, so the browser interpolates each function separately between keyframes.
Common mistakes
- Animating
toporleftfor movement thattranslatecould do. - Listing different functions in the start and end states, which produces unexpected paths.
- Overwriting a base
transformin a state rule and losing part of the effect. - Forgetting
transform-origin, so a scale grows from the center when you wanted it to grow from an edge. - Using inline elements. Transforms do not apply to non-replaced inline boxes such as a plain
span. Setdisplay: inline-blockor use a block or flex item.
Exercise
A .panel has transform: translateX(0) rotate(0deg) at rest. Its hover rule is transform: rotate(10deg). On hover the panel rotates, but the transition looks different from what you expected, and a teammate says the lists do not match. What should the hover rule be so the browser interpolates each function separately, and how would you make the panel pivot from its top-left corner?
Show answer
Write the hover rule as transform: translateX(0) rotate(10deg); so both states list translateX then rotate in the same order. Add transform-origin: top left; to .panel so the rotation pivots from the top-left corner instead of the center.
Exercise
Why does translateX(100%) work well for a slide-in panel of unknown width, and what does the percentage refer to?
Show answer
In translate, a percentage refers to the element's own size, so translateX(100%) moves the element by exactly its own width. This works without knowing the width in pixels.