CSS transforms: translate, scale, rotate, skew and 3D
Move, resize, turn and flip elements without disturbing the layout, with transform origin and a touch of 3D.
In this lesson you will
- Use translate, scale, rotate and skew
- Combine transforms in the right order
- Set the pivot with transform-origin
- Build a simple 3D flip card
About 15 minutes
Moving things without breaking the layout
You want a card to lift slightly on hover, an icon to spin, a badge to pop in larger, a panel to slide in from the side. If you changed margin or top to do this, the surrounding elements would shuffle around and the browser would redo the layout on every frame. Transforms solve this. They move, scale, rotate or skew an element visually, as if it were a sticker on a sheet of glass, while leaving its original space in the layout untouched.
translate: move
.a { transform: translateX(20px); } /* right */
.b { transform: translateY(-10px); } /* up */
.c { transform: translate(20px, -10px); }/* both */
.d { transform: translateX(-50%); } /* half of its own width, left */
Percentages in translate refer to the element’s own size, not the parent’s. That is exactly what makes the centring trick possible: top: 50%; left: 50%; transform: translate(-50%, -50%) moves the element’s top-left corner to the centre, then pulls it back by half of its own width and height.
scale: grow and shrink
.thumb:hover { transform: scale(1.05); } /* 5% bigger */
.pop { transform: scale(0.9); } /* 10% smaller */
.flip { transform: scaleX(-1); } /* mirror horizontally */
A value of 1 is the normal size. A small zoom of around 1.03 to 1.08 on hover is enough to feel responsive without looking cartoonish.
rotate: turn
.icon:hover { transform: rotate(90deg); }
.tilt { transform: rotate(-3deg); }
Angles use deg for degrees. Positive values turn clockwise. A slight tilt of a few degrees can make a card or a sticker look hand-placed and playful.
Press Run, then hover or tap the box. Add the hover rule: scale it to 1.1 and rotate it 6 degrees, with a smooth transition.
<style>
body { padding:30px; font-family:Arial; }
.box { width:120px; height:120px; background:linear-gradient(135deg,#6C2BE0,#4A6EDD); border-radius:20px; color:#fff; display:grid; place-items:center; font-weight:700; /* transition */ }
.box:hover { /* transform */ }
</style>
<div class="box">Hover</div>
Add transition: transform .3s ease; to .box and transform: scale(1.1) rotate(6deg); to the hover rule.
<style>
body { padding:30px; font-family:Arial; }
.box { width:120px; height:120px; background:linear-gradient(135deg,#6C2BE0,#4A6EDD); border-radius:20px; color:#fff; display:grid; place-items:center; font-weight:700; transition: transform .3s ease; }
.box:hover { transform: scale(1.1) rotate(6deg); }
</style>
<div class="box">Hover</div>
skew
.slant { transform: skewX(-12deg); }
Skew leans an element sideways. It is used for dynamic banners and diagonal section edges. Remember that it also slants the text inside, so counter it by skewing the inner text the opposite way.
Combining transforms
You can chain several functions in one declaration, separated by spaces. Order matters, because each step happens relative to the result of the previous one.
.a { transform: translateX(100px) rotate(45deg); } /* move, then turn */
.b { transform: rotate(45deg) translateX(100px); } /* turn, then move along the turned axis */
In the second example the element rotates first, so its own X axis now points diagonally, and the move travels along that diagonal. If a transform does something unexpected, check the order.
transform-origin: the pivot point
.door { transform-origin: left center; }
.hinge { transform-origin: top right; }
.door:hover { transform: rotateY(-70deg); }
By default, transforms happen around the centre. Changing the origin moves the pivot, so a card can swing from a corner like a door on a hinge, or a menu can grow from its top edge.
A touch of 3D
.scene { perspective: 800px; }
.card { transition: transform .6s; transform-style: preserve-3d; }
.scene:hover .card { transform: rotateY(180deg); }
perspective on a parent gives the child’s 3D rotations depth, with nearer edges looking larger. rotateX and rotateY tilt around the horizontal and vertical axes. Combined with backface-visibility: hidden on the two faces, this is how flip cards are built. Use 3D sparingly: a single flourish is delightful, while a page full of it is exhausting.
Make the flip card: the parent .scene needs perspective, the inner .card a transition and transform-style: preserve-3d, and on hover rotate the card 180 degrees around Y.
<style>
body { padding:24px; font-family:Arial; }
.scene { width:140px; height:140px; /* perspective */ }
.card { position:relative; width:100%; height:100%; /* transition and 3d */ }
.face { position:absolute; inset:0; display:grid; place-items:center; border-radius:16px; color:#fff; font-weight:700; backface-visibility:hidden; }
.front { background:#5A28C8; } .back { background:#E8409F; transform:rotateY(180deg); }
/* hover */
</style>
<div class="scene"><div class="card"><div class="face front">Front</div><div class="face back">Back</div></div></div>
.scene{perspective:600px} .card{transition:transform .6s; transform-style:preserve-3d} .scene:hover .card{transform:rotateY(180deg)}
<style>
body { padding:24px; font-family:Arial; }
.scene { width:140px; height:140px; perspective:600px; }
.card { position:relative; width:100%; height:100%; transition:transform .6s; transform-style:preserve-3d; }
.face { position:absolute; inset:0; display:grid; place-items:center; border-radius:16px; color:#fff; font-weight:700; backface-visibility:hidden; }
.front { background:#5A28C8; } .back { background:#E8409F; transform:rotateY(180deg); }
.scene:hover .card { transform:rotateY(180deg); }
</style>
<div class="scene"><div class="card"><div class="face front">Front</div><div class="face back">Back</div></div></div>
The individual transform properties
Modern browsers also let you write each transform as its own property: translate, rotate and scale. They are easier to animate separately and do not depend on order.
.icon { translate: 10px 0; rotate: 15deg; scale: 1.2; }
.icon:hover { rotate: 45deg; }
Transforms and layout
- A transformed element keeps its original space. Neighbours do not move to make room.
- A transform creates a new stacking context and can make fixed-position children behave differently.
- Text can look slightly blurry after fractional transforms on some screens. Keep distances to whole pixels when you can.
- Hover scaling can push parts of an element outside a container with
overflow: hidden, clipping the edges.
Animating top, left or margin to move something. It works, but it is slower and janky on phones. Use transform: translate() instead.
Wrap transform hover effects in @media (hover: hover) so touch screens do not get stuck in a transformed state after a tap.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Why are transforms preferred over changing margin to move things?
- They are shorter
- They do not trigger layout recalculation
- They work only on images
- They add colour
Transforms are cheap and smooth.
A value of scale(1) means…
- Hidden
- Normal size
- Double size
- Half size
1 is the original size.
Why does transform order matter?
- It does not
- Each step is relative to the result of the previous one
- Browsers randomise it
- Only the last works
Rotating before moving changes the movement direction.
What does transform-origin change?
- The colour
- The pivot point of the transformation
- The z-index
- The font
It sets where the transformation happens from.
What does perspective on a parent do?
- Adds depth to child 3D transforms
- Blurs the page
- Changes the font
- Hides children
It makes 3D rotations look three-dimensional.
Summary
- Transforms change appearance, not layout.
- Order of transform functions matters.
- transform-origin sets the pivot.
- Use transforms rather than margin or top for motion.
Questions
Does a transform affect SEO or layout?
No. It is purely visual.
Why is my transformed text blurry?
Fractional pixel positions can blur text. Use whole numbers and avoid unnecessary scaling.
Can I animate several transforms at once?
Yes. Transition the transform property, or use keyframes, as the next lesson shows.
