Transforms
The transform property lets you move, resize, rotate and slant an element visually, without disturbing the layout around it. Transforms power hover effects, card flips, icons that turn and most smooth animations on the web.
The four basic transforms
translate(x, y)moves the element.translateX()andtranslateY()move along one axis. Percentages are relative to the element's own size, sotranslate(-50%, -50%)shifts it back by half its width and height.scale(n)resizes it:1is normal,1.5is 50% larger,0.5is half size.scale(2, 1)stretches only horizontally.rotate(angle)turns it clockwise for positive angles:45deg,0.25turn(90 degrees),-10deg(anticlockwise).skew(x, y),skewX()andskewY()slant it, like pushing the top of a box sideways.
transform-origin.Transforms don't affect layout
A transformed element still occupies its original space in the layout. Neighbours don't move out of the way when you scale a card up; it simply overlaps them. That's exactly why transforms are great for hover effects and animation: nothing else on the page has to be recalculated.
.product:hover {
transform: scale(1.05);
}
The hovered product grows by 5% and overlaps its neighbours slightly, while the grid around it stays perfectly still.
Combining transforms
List several functions separated by spaces. They are applied from right to left as seen from the element (or, equivalently, each one acts in the coordinate system left by the one before), so order changes the result:
.a { transform: translateX(100px) rotate(45deg); }
.b { transform: rotate(45deg) translateX(100px); }
.a moves 100px to the right and then appears rotated in place. .b is rotated first, so its "x" direction now points diagonally down-right, and the 100px move travels along that diagonal. Writing a second transform declaration replaces the first rather than adding to it.
Modern CSS also has separate translate, scale and rotate properties, for example rotate: 10deg;. They're handy because you can change one (say, on hover) without rewriting the others. They are applied in a fixed order: translate, then rotate, then scale, and then any transform.
transform-origin
By default an element scales and rotates around its centre (transform-origin: center). Change it to pivot from elsewhere: transform-origin: top left makes a box swing like a door hinged at its top-left corner, and transform-origin: bottom makes a bar grow upwards from its base, which is useful for animated bar charts.
A taste of 3D
Transforms can work in three dimensions with rotateX(), rotateY() and translateZ(). Give the parent a perspective (for example perspective: 800px) so nearer parts look bigger. A card flip rotates a card 180 degrees around the Y axis and uses backface-visibility: hidden to hide the reverse side of each face.
Try it
h1 {
display: inline-block;
transform: rotate(-3deg);
background: #fde68a;
padding: 0.25rem 0.75rem;
}
a {
display: inline-block;
transition: transform 0.2s ease-out;
}
a:hover {
transform: translateY(-4px) scale(1.1);
}
button {
padding: 0.6rem 1.2rem;
background: #1e3a8a;
color: white;
border: none;
border-radius: 6px;
transition: transform 0.3s ease;
}
button:hover {
transform: rotate(8deg) scale(1.15);
}
button:active {
transform: scale(0.95);
}
The heading sits on a yellow background, tilted slightly anticlockwise like a sticker. Hovering over the link lifts it up and enlarges it a little. Hovering over the button rotates and enlarges it smoothly, and pressing it makes it shrink as if pushed in. Links are inline by default and inline boxes can't be transformed, which is why the link needs display: inline-block.
Key points
translate(),scale(),rotate()andskew()change how an element looks, not where the layout puts it.- Combined transforms are order-sensitive; a new
transformdeclaration replaces the old one. transform-originsets the pivot point (centre by default).- Pair transforms with transitions for smooth, cheap animations.
Tip: use the ← and → keys to move between pages.
