CSS 2D Transforms
The transform property moves, rotates, scales and skews an element visually, without affecting the layout around it. Neighbouring elements behave as if nothing changed. That makes transforms ideal for hover effects and animations, and they're also very fast, because the browser can hand them to the GPU.
Transform functions
| Function | Effect | Example |
|---|---|---|
translate(x, y) |
Move | translate(20px, -10px) |
translateX(x) / translateY(y) |
Move along one axis | translateY(50%) |
rotate(angle) |
Rotate clockwise | rotate(45deg), rotate(.25turn) |
scale(x, y) |
Resize (1 = normal) | scale(1.5), scale(2, .5) |
scaleX() / scaleY() |
Scale one axis; negative values mirror | scaleX(-1) |
skew(x, y) |
Slant | skew(20deg, 0) |
matrix(a, b, c, d, e, f) |
All of the above as a 2×3 matrix | rarely written by hand |
The dashed outline in each demo marks the original position:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 30px; padding: 30px; }
.slot { width: 80px; height: 80px; outline: 2px dashed #b0bec5; }
.box { width: 80px; height: 80px; background: #42a5f5; color: white; display: grid; place-items: center; font-size: 12px; opacity: .9; }
.t { transform: translate(20px, 15px); }
.r { transform: rotate(30deg); background: #ef5350; }
.s { transform: scale(1.3); background: #66bb6a; }
.k { transform: skew(20deg); background: #ffa726; }
.m { transform: scaleX(-1); background: #ab47bc; }
</style>
</head>
<body>
<div class="slot"><div class="box t">translate</div></div>
<div class="slot"><div class="box r">rotate</div></div>
<div class="slot"><div class="box s">scale</div></div>
<div class="slot"><div class="box k">skew</div></div>
<div class="slot"><div class="box m">mirror</div></div>
</body>
</html>Tip: Percentages in
translate()refer to the element's own size.translate(-50%, -50%)moves an element back by half its width and height, which is the basis of the classic centring trick.
Combining transforms
List several functions separated by spaces. They are applied from right to left, so the order changes the result. Rotating and then translating is not the same as translating and then rotating.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 60px; padding: 40px; }
.slot { width: 70px; height: 70px; outline: 2px dashed #b0bec5; }
.box { width: 70px; height: 70px; color: white; font-size: 11px; display: grid; place-items: center; text-align: center; }
.a { background: #26a69a; transform: rotate(45deg) translateX(60px); }
.b { background: #ec407a; transform: translateX(60px) rotate(45deg); }
</style>
</head>
<body>
<div class="slot"><div class="box a">rotate then translate</div></div>
<div class="slot"><div class="box b">translate then rotate</div></div>
</body>
</html>In the first box the translation happens along the already rotated axis, so the box moves diagonally.
transform-origin
Every transform pivots around the transform origin. By default that's the centre (50% 50%). Change it to rotate around a corner or an edge, as a door swings on its hinges.
| Value | Pivot point |
|---|---|
center (default) |
Middle |
top left |
Top-left corner |
bottom center |
Middle of the bottom edge |
20px 80% |
Any exact point |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 50px; padding: 50px 30px; }
.slot { width: 80px; height: 80px; outline: 2px dashed #b0bec5; position: relative; }
.box { width: 80px; height: 80px; transform: rotate(30deg); color: white; font-size: 12px; display: grid; place-items: center; }
.c { background: #5c6bc0; }
.tl { background: #ff7043; transform-origin: top left; }
.bc { background: #9ccc65; transform-origin: bottom center; }
</style>
</head>
<body>
<div class="slot"><div class="box c">center</div></div>
<div class="slot"><div class="box tl">top left</div></div>
<div class="slot"><div class="box bc">bottom</div></div>
</body>
</html>Interactive transforms
Transforms really shine together with :hover and transition. Hover over the cards:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 20px; padding: 30px; background: #fafafa; }
.card { width: 110px; height: 130px; border-radius: 12px; color: white; font-weight: bold; display: grid; place-items: center;
transition: transform .3s ease; box-shadow: 0 4px 10px rgba(0,0,0,.2); cursor: pointer; }
.grow { background: #29b6f6; }
.grow:hover { transform: scale(1.12); }
.tilt { background: #ab47bc; }
.tilt:hover { transform: rotate(-6deg) translateY(-8px); }
.flip { background: #ff7043; }
.flip:hover { transform: scaleX(-1); }
</style>
</head>
<body>
<div class="card grow">grow</div>
<div class="card tilt">tilt</div>
<div class="card flip">flip</div>
</body>
</html>Note: You can also write the individual properties
translate,rotateandscale(for examplerotate: 10deg;). They are applied in a fixed order (translate, then rotate, then scale), and you can change one of them without rewriting the wholetransformlist.
Test yourself
Does transform: translateX(100px) push neighbouring elements aside?
- Yes
- No, transforms don't affect layout
- Only for block elements
- Only with position: relative
Which property changes the point an element rotates around?
- rotate-center
- transform-origin
- transform-pivot
- origin
Which transform mirrors an element horizontally?
- rotate(180deg)
- scaleX(-1)
- skew(180deg)
- translateX(-100%)