CSS Transforms
Transform elements in 2D and 3D space without affecting document flow. Create rotations, scaling, translations, and complex 3D effects for immersive user experiences.
What does transform do in CSS?
transform visually moves, rotates, scales or skews an element without affecting the layout around it. Common functions are translate(), rotate(), scale() and skew(), and you can chain them: transform: translateY(-4px) scale(1.05);. Because the surrounding layout isn't recalculated, transforms are the smoothest property to animate.
.card:hover { transform: translateY(-4px) scale(1.02); }
.icon { transform: rotate(45deg); }
.centered {
position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%);
}
/* Individual properties */
.box { translate: 10px 0; rotate: 15deg; scale: 1.1; }Interactive CSS Transforms Demo
Code Editor
<div class="row">
<div class="box move">translate</div>
<div class="box turn">rotate</div>
<div class="box grow">scale</div>
<div class="box skew">skew</div>
</div>
<p>Hover the row: transforms don't move the other boxes.</p>Preview
Transform Fundamentals
CSS transforms properties define move, rotate, scale, and skew elements without affecting the document flow. They're hardware-accelerated for smooth animations and are essential for modern web interactions.
Transform Function Syntax
transform: function(value) function(value);
transform: translate(50px, 100px) rotate(45deg) scale(1.2);
transform: translateX(100px) rotateY(45deg);Order matters: Functions apply from right to left
Combine functions: Space-separated list
Hardware accelerated: GPU-optimized
No layout impact: Doesn't affect other elements
2D Transform Functions
2D transforms operate in the X and Y plane, perfect for common interface animations and layout adjustments.
Translation
translateX(100px) /* Move horizontally */
translateY(50px) /* Move vertically */
translate(100px, 50px) /* Both directions */Moves element without affecting layout flow
Scaling
scale(1.5) /* Uniform scaling */
scaleX(2) /* Horizontal only */
scaleY(0.5) /* Vertical only */Resizes element from transform origin
Rotation
rotate(45deg) /* Clockwise rotation */
rotate(-90deg) /* Counter-clockwise */
rotate(0.5turn) /* Half turn */Rotates around transform origin point
Skewing
skewX(20deg) /* Horizontal slant */
skewY(15deg) /* Vertical slant */
skew(20deg, 15deg) /* Both axes */Creates parallelogram-like distortion
3D Transforms & Perspective
3D transforms add depth to your designs, enabling card flips, cubes, and immersive visual effects that respond to user interactions.
Setting Up 3D Context
Parent Container
.container {
perspective: 1000px;
perspective-origin: center;
}3D Element
.element {
transform-style: preserve-3d;
backface-visibility: hidden;
}3D Rotation
rotateX(45deg)
rotateY(45deg)
rotateZ(45deg)Rotate around X, Y, or Z axis in 3D space
3D Translation
translateZ(100px)
translate3d(x, y, z)Move elements along the Z-axis (depth)
Perspective
perspective(800px)
perspective: 1000px;Controls the depth and viewing distance
Transform Origin & Performance
Transform Origin
transform-origin: center; /* default */
transform-origin: top left;
transform-origin: 50% 50%;
transform-origin: 10px 20px;Controls the pivot point for transformations
Performance Tips
- • Use transform instead of changing position
- • Prefer translate3d() for GPU acceleration
- • Add will-change: transform for animations
- • Use perspective wisely (impacts performance)
- • Remove transforms when not needed
CSS Transforms — step by step
translate, scale, rotate
Move elements with translate(x, y), resize with scale(x, y), and spin with rotate(deg) — none of these affect surrounding layout.
🎯 Quick Check
Where is the default transform-origin for rotate()?
CSS Transforms: summary
Key points
- Transforms apply right to left, so
rotate()thentranslate()gives a different result from the reverse order. transform-originsets the pivot point (defaultcenter).- Percentages in
translate()refer to the element's own size, which is whytranslate(-50%, -50%)centres it. - A transformed element creates a stacking context and becomes the containing block for
fixedchildren.
Frequently asked questions
›Does transform affect layout?
No. The element keeps its original space in the layout and is only drawn somewhere else, so neighbouring elements don't move.
›What is the difference between translate and position?
translate moves the element visually and is cheap to animate. Changing top or left on a positioned element triggers layout calculations on every frame.