Sub topic 6
Moving, scaling and rotating with transform
This chapter assumes you can write a transition (property, duration, easing) and trigger it with :hover or a class change. It covers transform, the property you will animate most often.
What transform does
transform changes how an element is drawn without changing the layout. Neighbouring elements do not move, and the element keeps the space it had before. This is why it is cheap to animate, as a later chapter on performance explains.
The common functions are:
| Function | Effect | Example |
|---|---|---|
| `translate(x, y)` | Moves the element | `translate(20px, -4px)` |
| `scale(n)` | Resizes around the origin | `scale(1.1)` |
| `rotate(angle)` | Turns the element clockwise | `rotate(45deg)` |
| `skew(x, y)` | Slants the element | `skewX(10deg)` |
Percentages in translate() refer to the element's own size. translateX(-50%) moves an element left by half its own width, whatever that width is.
A transition on transform
.tile {
display: inline-block;
transform: translateY(0) rotate(0deg);
transition: transform 250ms ease-out;
}
.tile:hover,
.tile:focus-visible {
transform: translateY(-4px) rotate(3deg);
}transform only applies to elements that generate a transformable box. Plain <span> elements are inline and ignore it, so the example sets display: inline-block. Block, flex items, grid items and images work without this.
Order and origin
Transform functions are applied right to left, relative to the element's own coordinate system. Each function changes that system for the ones before it. So translate(100px) rotate(45deg) and rotate(45deg) translate(100px) end in different places. In the first, the element moves 100px right and then spins in place. In the second, it spins first, so its x-axis points diagonally, and the 100px move follows that diagonal.
flowchart LRThe diagram below shows the same two orders.
Rotation and scaling happen around transform-origin, which defaults to 50% 50%, the centre of the element. Change it to pivot elsewhere:
.flap {
transform-origin: top left;
transition: transform 300ms ease-in-out;
}
.flap.open {
transform: rotate(-90deg);
}Individual transform properties
Modern browsers also support translate, scale and rotate as separate properties. They combine with transform and can be transitioned independently:
.icon {
rotate: 0deg;
scale: 1;
transition: rotate 300ms ease, scale 150ms ease;
}
.icon:hover {
rotate: 90deg;
scale: 1.2;
}This avoids rewriting a whole transform list just to change one part.
Common errors
- Replacing the whole list. A rule that sets
transform: scale(1.1)on hover discards atranslateX(-50%)set on the base rule. Repeat every function you still need, or use the individual properties. - Mismatched function lists. The browser interpolates function by function only when both states list the same functions in the same order. If they differ, it falls back to interpolating the combined matrix, which can look wrong, especially with rotation.
- Expecting layout to follow. A scaled element still occupies its original space and can overlap neighbours.
- Transforming inline elements. The element needs a transformable box, as shown above.
- Wrong units.
rotate(45)is invalid. Angles needdeg,turnorrad.translate(20)is invalid too, because lengths need units unless they are zero.
Exercise
A card has transform: translateX(-50%) in its base rule. On hover you want it to also grow by 5% while staying at the same horizontal offset. Write the hover declaration.
Show answer
transform: translateX(-50%) scale(1.05); Writing only scale(1.05) would drop the translation and shift the card. The function lists match in order (translateX first), so the transition interpolates cleanly. Alternatively, keep the translation in transform and use scale: 1.05 on hover.