Master 2D and 3D transformations to create dynamic visual effects
CSS Transforms allow you to modify the coordinate space of elements, enabling rotation, scaling, skewing, and translating without affecting document flow. They're essential for creating modern, interactive web experiences.
/* Move element horizontally and vertically */
.element {
transform: translate(50px, 100px); /* X, Y */
}
/* Individual axes */
.element {
transform: translateX(50px); /* Move right */
transform: translateY(100px); /* Move down */
}
/* Percentage values (relative to element size) */
.element {
transform: translate(50%, 50%);
}
/* Common use: Perfect centering */
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Rotate in degrees */
.element {
transform: rotate(45deg); /* Clockwise */
transform: rotate(-45deg); /* Counter-clockwise */
}
/* Full rotation */
.element {
transform: rotate(360deg);
}
/* Common use: Icon spin */
.icon:hover {
transform: rotate(180deg);
}
/* Scale uniformly */
.element {
transform: scale(1.5); /* 150% size */
transform: scale(0.5); /* 50% size */
}
/* Scale on individual axes */
.element {
transform: scale(2, 1); /* X, Y - width doubled */
transform: scaleX(2); /* Width only */
transform: scaleY(0.5); /* Height only */
}
/* Common use: Hover zoom */
.card:hover {
transform: scale(1.1);
}
/* Skew (distort) element */
.element {
transform: skew(20deg, 10deg); /* X-axis, Y-axis */
transform: skewX(20deg); /* Horizontal skew */
transform: skewY(10deg); /* Vertical skew */
}
/* Parallelogram effect */
.parallelogram {
transform: skewX(-20deg);
}
Translate
Rotate
Scale
Skew
You can apply multiple transform functions by separating them with spaces.
/* Multiple transforms (space-separated) */
.element {
transform: rotate(45deg) scale(1.5) translate(50px, 50px);
}
/* Order matters! These produce different results: */
.element-1 {
transform: rotate(45deg) translate(100px, 0);
}
.element-2 {
transform: translate(100px, 0) rotate(45deg);
}
/* Common combinations */
.card:hover {
transform: scale(1.1) rotate(3deg);
}
.tilt-zoom {
transform: rotate(-5deg) scale(1.2);
}
The order of transform functions matters! Transforms are applied from right to left, and each transform affects the coordinate system for the next one.
rotate → translate
translate → rotate
Notice the different final positions!
The transform-origin property sets the point around which transformations occur.
/* Default is center */
.element {
transform-origin: center; /* Same as 50% 50% */
}
/* Keywords */
.element {
transform-origin: top left;
transform-origin: bottom right;
transform-origin: center top;
}
/* Precise values */
.element {
transform-origin: 0 0; /* Top left corner */
transform-origin: 100% 100%; /* Bottom right */
transform-origin: 50px 50px; /* Specific point */
}
/* Practical example: Rotate from corner */
.door {
transform-origin: left center;
transform: rotateY(90deg); /* Opens like a door */
}
Origin: Center
Origin: Top Left
Origin: Bottom Right
Red dots show the pivot point
Create depth and perspective with 3D transformations.
/* Enable 3D space on parent */
.container {
perspective: 1000px; /* Distance to 3D space */
perspective-origin: center;
}
/* Preserve 3D for children */
.parent {
transform-style: preserve-3d;
}
/* 3D Rotation */
.element {
transform: rotateX(45deg); /* Rotate around X-axis */
transform: rotateY(45deg); /* Rotate around Y-axis */
transform: rotateZ(45deg); /* Same as rotate() */
}
/* 3D Translation */
.element {
transform: translateZ(100px); /* Move toward viewer */
transform: translate3d(x, y, z); /* All three axes */
}
/* 3D Scale */
.element {
transform: scaleZ(2);
transform: scale3d(1.5, 1.5, 2);
}
/* Card flip effect */
.card {
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card:hover {
transform: rotateY(180deg);
}
/* Control back side visibility */
.element {
backface-visibility: visible; /* Default */
backface-visibility: hidden; /* Hide back face */
}
/* Card flip with hidden back */
.card-front,
.card-back {
backface-visibility: hidden;
}
.card-back {
transform: rotateY(180deg);
}
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-10px) scale(1.05);
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
}
.flip-container {
perspective: 1000px;
}
.flip-card {
position: relative;
width: 300px;
height: 400px;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip-container:hover .flip-card {
transform: rotateY(180deg);
}
.flip-card-front,
.flip-card-back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.flip-card-back {
transform: rotateY(180deg);
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.loader {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
}
.btn-3d {
position: relative;
background: #667eea;
padding: 15px 30px;
transform: translateY(0);
transition: transform 0.1s;
}
.btn-3d::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #5568d3;
transform: translateY(6px);
z-index: -1;
}
.btn-3d:active {
transform: translateY(6px);
}
.btn-3d:active::before {
transform: translateY(0);
}
.image-container {
overflow: hidden;
}
.image-container img {
transition: transform 0.5s ease;
}
.image-container:hover img {
transform: scale(1.2) rotate(5deg);
}
Rotate + Scale
Translate Up
Skew + Scale
/* ❌ Poor performance */
.element {
position: relative;
transition: top 0.3s, left 0.3s;
}
.element:hover {
top: -10px;
left: 10px;
}
/* ✅ Better performance */
.element {
transition: transform 0.3s;
}
.element:hover {
transform: translate(10px, -10px);
}
/* For complex animations */
.complex-animation {
will-change: transform;
}
/* 1. Perfect Centering */
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* 2. Modal/Dialog Animation */
.modal {
transform: scale(0);
transition: transform 0.3s;
}
.modal.active {
transform: scale(1);
}
/* 3. Navigation Menu Slide */
.menu {
transform: translateX(-100%);
transition: transform 0.3s;
}
.menu.open {
transform: translateX(0);
}
/* 4. Image Zoom on Hover */
.image-wrapper {
overflow: hidden;
}
.image-wrapper img {
transition: transform 0.5s;
}
.image-wrapper:hover img {
transform: scale(1.2);
}
/* 5. Rotating Icon */
.icon {
transition: transform 0.3s;
}
.icon:hover {
transform: rotate(180deg);
}
Q1: Which transform function moves an element?
Q2: What does transform-origin control?
Q3: Which property creates 3D space for child elements?
Q4: What's the correct syntax for multiple transforms?
Q5: Which value makes an element twice its size?
translate(), rotate(), scale(), skew()rotateX/Y/Z(), translateZ(), perspectivetransform-style: preserve-3d for 3D effectsbackface-visibility controls visibility of rotated elementsTransforms are GPU-accelerated. Always prefer transform over position properties for animations to ensure smooth, 60fps performance.
The order of transform functions affects the final result. Transforms apply from right to left, with each affecting the coordinate system.
Control where transformations pivot from using transform-origin property. Change the pivot point to create effects like door swings.
Use perspective and preserve-3d to create stunning 3D transformations. Perfect for card flips and immersive interfaces.