HTML & CSSMedium
What is transform?
transform visually moves, rotates, scales or skews an element without affecting the layout around it, using functions like translate(), rotate(), scale() and skew(), in 2D or 3D. Because it does not trigger layout, it is the preferred property for smooth animations.
Transform functions
- Move:
translate(20px, 10px),translateX(50%)(percentages refer to the element's own size). - Rotate:
rotate(45deg),rotateY(180deg)(3D). - Scale:
scale(1.1),scaleX(-1)(mirror). - Skew:
skew(10deg). matrix()/matrix3d()for any combination.- Order matters:
translate(100px) rotate(45deg)differs fromrotate(45deg) translate(100px).
Individual transform properties
Modern CSS also has standalone translate, rotate and scale properties: scale: 1.1; rotate: 5deg;. They can be animated and overridden independently, without rewriting the whole transform list.
Related properties
transform-origin: the pivot point (centerby default,top left,50% 100%).perspectiveandtransform-style: preserve-3dfor 3D scenes;backface-visibility: hiddenfor card flips.
Side effects to know
- A transformed element creates a stacking context and becomes the containing block for
position: fixeddescendants. - Its original space in the layout is kept; neighbors do not move.
- It can make text slightly blurry when scaled; avoid scaling text permanently.
Classic uses
Centering (translate: -50% -50%), hover lift effects, icon rotation on open, card flips, and slide-in drawers.
Code example
html
1<style>2 body { font-family: system-ui; display: flex; gap: 24px; flex-wrap: wrap; align-items: center; padding: 20px; }3 .box { width: 80px; height: 80px; display: grid; place-items: center; color: white; background: #2b4bff; border-radius: 8px; transition: transform .3s, rotate .3s, scale .3s; }4 .t:hover { transform: translateY(-10px); }5 .r:hover { rotate: 45deg; }6 .s:hover { scale: 1.2; }7 .k:hover { transform: skewX(-15deg); }8 .origin { transform-origin: top left; }9 .origin:hover { rotate: 20deg; }10 .scene { perspective: 600px; }11 .flip { width: 120px; height: 80px; position: relative; transform-style: preserve-3d; transition: transform .6s; }12 .scene:hover .flip { transform: rotateY(180deg); }13 .face { position: absolute; inset: 0; display: grid; place-items: center; border-radius: 8px; backface-visibility: hidden; border: 2px solid #0b1020; }14 .back { transform: rotateY(180deg); background: #ffd84d; }15 .front { background: white; }16</style>17<div class="box t">translate</div>18<div class="box r">rotate</div>19<div class="box s">scale</div>20<div class="box k">skew</div>21<div class="box origin">origin</div>22<div class="scene"><div class="flip"><div class="face front">Hover me</div><div class="face back">3D flip!</div></div></div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.