The CSS transform property moves, rotates, resizes and slants elements without changing the page layout around them. It powers hover effects, card flips, image zooms and smooth animations. Because transforms are handled by the graphics hardware, they are also the fastest way to animate. This lesson covers 2D transforms, the transform origin, combining transforms, and 3D effects with perspective.
In this lesson you will learn
translate,rotate,scaleandskew- Combining several transforms and why order matters
- Changing the pivot point with
transform-origin - 3D rotation with
perspective - A flip card with
backface-visibility
You should know the box model and transitions and animations.
2D Transforms
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2D transforms</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.row { display: flex; gap: 22px; flex-wrap: wrap; }
.box { width: 70px; height: 70px; background: #42a5f5; color: #fff;
display: grid; place-items: center; border-radius: 6px; }
.move { transform: translate(10px, 10px); }
.turn { transform: rotate(20deg); }
.grow { transform: scale(1.2); }
.lean { transform: skewX(-15deg); }
.combo { transform: translateY(8px) rotate(-10deg) scale(0.9); }
.corner { transform: rotate(25deg); transform-origin: top left; background: #ab47bc; }
.hover { transition: transform 0.3s; background: #26a69a; }
.hover:hover { transform: translateY(-6px) scale(1.08); }
</style>
</head>
<body>
<div class="row">
<div class="box move">translate</div>
<div class="box turn">rotate</div>
<div class="box grow">scale</div>
<div class="box lean">skew</div>
<div class="box combo">combo</div>
<div class="box corner">origin</div>
<div class="box hover">hover me</div>
</div>
</body>
</html>
Result
| Function | Effect | Example |
|---|---|---|
translate(x, y) | Moves the element | translate(10px, -50%) |
rotate(angle) | Turns it clockwise (negative = anticlockwise) | rotate(45deg) |
scale(x, y) | Resizes it; 1 is normal size | scale(1.5) |
skew(x, y) | Slants it | skewX(-15deg) |
- A transformed element still takes up its original space in the layout. Its neighbours do not move, which is why transforms never cause the page to jump.
- Several functions can be listed in one
transform; they apply from right to left around the element.rotate(45deg) translateX(50px)andtranslateX(50px) rotate(45deg)give different results. transform-originsets the pivot point. It defaults to the centre;top leftmakes the purple box swing from its corner like a door.- Pairing
transformwith atransitioncreates smooth hover effects, like the teal “hover me” box that lifts and grows. - Modern CSS also has separate
translate,rotateandscaleproperties, such asrotate: 20deg, which are easier to change one at a time.
3D Transforms and a Flip Card
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D flip card</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 14px; }
.scene { width: 160px; height: 100px; perspective: 600px; }
.card {
position: relative; width: 100%; height: 100%;
transform-style: preserve-3d;
transition: transform 0.6s;
}
.scene:hover .card { transform: rotateY(180deg); }
.face {
position: absolute; inset: 0;
display: grid; place-items: center;
border-radius: 8px; color: #fff;
backface-visibility: hidden; /* hide the side facing away */
}
.front { background: #ef6c00; }
.back { background: #2e7d32; transform: rotateY(180deg); }
.tilt { margin-top: 18px; width: 160px; padding: 10px; background: #5c6bc0; color: #fff;
transform: perspective(400px) rotateX(25deg); }
</style>
</head>
<body>
<div class="scene">
<div class="card">
<div class="face front">Question: 7 × 8?</div>
<div class="face back">Answer: 56</div>
</div>
</div>
<p>Hover the card to flip it.</p>
<div class="tilt">rotateX(25deg) with perspective</div>
</body>
</html>
Result
rotateXtilts an element forwards or backwards,rotateYturns it left or right, androtateZis the same as ordinaryrotate.perspectivesets how far the viewer is from the screen. A smaller value, such as 300px, gives a strong 3D effect; a larger one, such as 1000px, a gentle one. Put it on the parent (as on.scene) or inside the transform itself (perspective(400px) rotateX(25deg)).transform-style: preserve-3dlets the card’s two faces keep their own 3D positions instead of being flattened.- The back face starts rotated by 180°.
backface-visibility: hiddenhides whichever face points away, so on hover you see the answer replace the question.
A Classic Use: Centring
Before Flexbox and Grid, absolute centring relied on transforms, and you will still see it often:
.centre {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%); /* move back by half its own size */
}
Percentages in translate refer to the element’s own size, not its parent’s, which is exactly what makes this work.
Common Mistakes
- Writing two
transformdeclarations, such as one for rotate and one for scale. The second replaces the first; list both in one value. - Forgetting units:
rotate(45)is invalid; use45deg. - Putting
perspectiveon the rotating element instead of its parent, which gives a flat-looking result. - Using transforms on inline elements such as plain
spans, which ignore them. Make theminline-block.
Practice Exercises
Exercise 1. Make an image grow by 10% when hovered, smoothly over 0.3 seconds.
Show answer
img { transition: transform 0.3s; }
img:hover { transform: scale(1.1); }
Exercise 2. Rotate a “NEW” label 30° anticlockwise around its bottom-left corner.
Show answer
.new { display: inline-block; transform: rotate(-30deg); transform-origin: bottom left; }
Frequently Asked Questions
Why are transforms better for animation than changing top or left?
Changing top, left, width or margin forces the browser to recalculate the layout every frame. Transforms and opacity can be handled by the graphics processor without new layout, so animations stay smooth even on slower phones.
Should transforms respect reduced-motion settings?
Yes. Some people get dizzy from large movement. Wrap big animated transforms in @media (prefers-reduced-motion: no-preference) so they run only for users who have not asked for less motion.
Summary
transformmoves, rotates, scales and skews without affecting layout.- Combine functions in one value; order changes the result.
transform-originsets the pivot;perspectiveadds 3D depth.- Transforms are the smoothest property to animate.
Next: CSS Container Queries.
