🔄 CSS Transforms

2D and 3D Transformations

CSS Transforms

Modify the visual appearance and position of elements without affecting document flow. Transforms are GPU-accelerated for smooth animations.

💻 Transform Basics

/* Basic syntax */
.element {
    transform: function(value);
}

/* Multiple transforms */
.element {
    transform: rotate(45deg) scale(1.5) translate(100px, 50px);
}

/* Transform order matters! */
.element {
    transform: rotate(45deg) translate(100px, 0);
    /* Different from: */
    transform: translate(100px, 0) rotate(45deg);
}

📐 2D Transforms

translate() - Move

/* Move horizontally and vertically */
.move {
    transform: translate(50px, 100px);  /* X, Y */
}

/* Move only horizontally */
.move-x {
    transform: translateX(50px);
}

/* Move only vertically */
.move-y {
    transform: translateY(100px);
}

/* Negative values */
.move-back {
    transform: translate(-50px, -100px);
}

/* Percentage values (relative to element size) */
.center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* Examples */
.slide-right {
    transform: translateX(100px);
}

.slide-down {
    transform: translateY(50px);
}

rotate() - Rotate

/* Rotate in degrees */
.rotate {
    transform: rotate(45deg);
}

/* Negative rotation (counter-clockwise) */
.rotate-ccw {
    transform: rotate(-45deg);
}

/* Full rotation */
.spin {
    transform: rotate(360deg);
}

/* Turns (1turn = 360deg) */
.half-turn {
    transform: rotate(0.5turn);  /* 180deg */
}

/* Radians */
.radians {
    transform: rotate(3.14rad);  /* ~180deg */
}

/* Gradians */
.gradians {
    transform: rotate(100grad);  /* 90deg */
}

/* Examples */
.tilt-left {
    transform: rotate(-15deg);
}

.upside-down {
    transform: rotate(180deg);
}

scale() - Resize

/* Scale uniformly */
.scale {
    transform: scale(1.5);  /* 150% size */
}

/* Scale X and Y separately */
.scale-xy {
    transform: scale(2, 0.5);  /* 200% wide, 50% tall */
}

/* Scale X only */
.scale-x {
    transform: scaleX(1.5);
}

/* Scale Y only */
.scale-y {
    transform: scaleY(0.8);
}

/* Scale down */
.shrink {
    transform: scale(0.5);  /* 50% size */
}

/* Flip horizontally */
.flip-x {
    transform: scaleX(-1);
}

/* Flip vertically */
.flip-y {
    transform: scaleY(-1);
}

/* Examples */
.zoom-in {
    transform: scale(1.2);
}

.mirror {
    transform: scaleX(-1);
}

skew() - Slant

/* Skew on both axes */
.skew {
    transform: skew(10deg, 5deg);  /* X, Y */
}

/* Skew X only */
.skew-x {
    transform: skewX(15deg);
}

/* Skew Y only */
.skew-y {
    transform: skewY(10deg);
}

/* Negative skew */
.skew-negative {
    transform: skewX(-20deg);
}

/* Examples */
.italic-effect {
    transform: skewX(15deg);
}

.perspective-effect {
    transform: skewY(-5deg);
}

matrix() - Combined Transform

/* matrix(a, b, c, d, tx, ty) */
/* a, d: scale */
/* b, c: skew */
/* tx, ty: translate */

.matrix {
    transform: matrix(1, 0, 0, 1, 50, 100);
    /* Same as: translate(50px, 100px) */
}

/* Complex transformation */
.complex {
    transform: matrix(1.5, 0.5, -0.5, 1.5, 50, 50);
}

/* Usually not written manually */
/* Use individual functions instead */

🎨 Combining 2D Transforms

/* Multiple transforms */
.combo {
    transform: translate(50px, 100px) rotate(45deg) scale(1.5);
}

/* Order matters! */
.example1 {
    transform: rotate(45deg) translateX(100px);
    /* Translates along rotated axis */
}

.example2 {
    transform: translateX(100px) rotate(45deg);
    /* Translates then rotates */
}

/* Common patterns */
.card-hover {
    transform: translateY(-10px) scale(1.05);
}

.button-active {
    transform: scale(0.95);
}

.badge {
    transform: rotate(-15deg) translate(-5px, -5px);
}

🎯 Transform Origin

/* Default: center center (50% 50%) */
.default {
    transform-origin: center center;
    transform: rotate(45deg);
}

/* Keywords */
.top-left {
    transform-origin: top left;
    transform: rotate(45deg);
}

.bottom-right {
    transform-origin: bottom right;
    transform: rotate(45deg);
}

/* Percentages */
.custom {
    transform-origin: 25% 75%;
    transform: rotate(45deg);
}

/* Pixels */
.pixel {
    transform-origin: 100px 50px;
    transform: rotate(45deg);
}

/* Single value (applies to X, Y defaults to center) */
.x-only {
    transform-origin: left;
    /* Same as: left center */
}

/* Three values for 3D */
.three-d {
    transform-origin: 50% 50% 100px;
}

/* Examples */
.door {
    transform-origin: left center;
    transform: rotateY(90deg);
}

.card-flip {
    transform-origin: center center;
    transform: rotateY(180deg);
}

.corner-spin {
    transform-origin: top left;
    transform: rotate(45deg);
}

🌐 3D Transforms

3D Translation

/* Translate in 3D space */
.translate-3d {
    transform: translate3d(50px, 100px, 200px);  /* X, Y, Z */
}

/* Z-axis only */
.translate-z {
    transform: translateZ(100px);
}

/* Examples */
.forward {
    transform: translateZ(50px);
}

.backward {
    transform: translateZ(-50px);
}

3D Rotation

/* Rotate around X-axis (pitch) */
.rotate-x {
    transform: rotateX(45deg);
}

/* Rotate around Y-axis (yaw) */
.rotate-y {
    transform: rotateY(45deg);
}

/* Rotate around Z-axis (roll - same as 2D rotate) */
.rotate-z {
    transform: rotateZ(45deg);
}

/* Rotate around arbitrary axis */
.rotate-3d {
    transform: rotate3d(1, 1, 0, 45deg);  /* X, Y, Z, angle */
}

/* Examples */
.flip-card {
    transform: rotateY(180deg);
}

.tilt-forward {
    transform: rotateX(10deg);
}

3D Scale

/* Scale in 3D */
.scale-3d {
    transform: scale3d(1.5, 1.5, 2);  /* X, Y, Z */
}

/* Scale Z only */
.scale-z {
    transform: scaleZ(2);
}

🎭 3D Transform Properties

perspective

/* Add perspective to parent */
.container {
    perspective: 1000px;
}

.child {
    transform: rotateY(45deg);
}

/* Or on element itself */
.element {
    transform: perspective(1000px) rotateY(45deg);
}

/* Smaller value = more dramatic perspective */
.dramatic {
    perspective: 400px;
}

/* Larger value = subtle perspective */
.subtle {
    perspective: 2000px;
}

perspective-origin

/* Vanishing point location */
.container {
    perspective: 1000px;
    perspective-origin: center center;  /* Default */
}

.container-left {
    perspective: 1000px;
    perspective-origin: left center;
}

.container-custom {
    perspective: 1000px;
    perspective-origin: 75% 25%;
}

transform-style

/* Preserve 3D for children */
.parent {
    transform-style: preserve-3d;
}

.parent .child {
    transform: rotateY(45deg);
}

/* Flatten (default) */
.flat {
    transform-style: flat;
}

backface-visibility

/* Hide back face when rotated */
.card-face {
    backface-visibility: hidden;
}

/* Show back face (default) */
.visible {
    backface-visibility: visible;
}

/* Essential for card flip effects */
.card {
    transform-style: preserve-3d;
}

.card-front {
    backface-visibility: hidden;
}

.card-back {
    backface-visibility: hidden;
    transform: rotateY(180deg);
}

🎯 Practical Examples

/* Button hover effect */
.button {
    transition: transform 0.3s;
}

.button:hover {
    transform: translateY(-3px);
}

.button:active {
    transform: translateY(-1px);
}

/* Card hover */
.card {
    transition: transform 0.3s, box-shadow 0.3s;
}

.card:hover {
    transform: translateY(-10px) scale(1.02);
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}

/* Image zoom on hover */
.image-container {
    overflow: hidden;
}

.image-container img {
    transition: transform 0.5s;
}

.image-container:hover img {
    transform: scale(1.2);
}

/* Rotating loader */
@keyframes spin {
    to { transform: rotate(360deg); }
}

.loader {
    animation: spin 1s linear infinite;
}

/* Flip card */
.flip-card {
    width: 300px;
    height: 400px;
    perspective: 1000px;
}

.flip-card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transition: transform 0.6s;
    transform-style: preserve-3d;
}

.flip-card:hover .flip-card-inner {
    transform: rotateY(180deg);
}

.flip-card-front,
.flip-card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
}

.flip-card-back {
    transform: rotateY(180deg);
}

/* 3D cube */
.cube-container {
    width: 200px;
    height: 200px;
    perspective: 1000px;
}

.cube {
    width: 100%;
    height: 100%;
    position: relative;
    transform-style: preserve-3d;
    transform: rotateX(-20deg) rotateY(30deg);
    transition: transform 1s;
}

.cube-face {
    position: absolute;
    width: 200px;
    height: 200px;
    border: 2px solid #333;
    opacity: 0.9;
}

.cube-front { transform: rotateY(0deg) translateZ(100px); }
.cube-back { transform: rotateY(180deg) translateZ(100px); }
.cube-right { transform: rotateY(90deg) translateZ(100px); }
.cube-left { transform: rotateY(-90deg) translateZ(100px); }
.cube-top { transform: rotateX(90deg) translateZ(100px); }
.cube-bottom { transform: rotateX(-90deg) translateZ(100px); }

/* Parallax effect */
.parallax-container {
    perspective: 1px;
    height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
}

.parallax-layer {
    position: absolute;
    width: 100%;
    transform: translateZ(-1px) scale(2);
}

/* Skew hover effect */
.skew-card {
    transition: transform 0.3s;
}

.skew-card:hover {
    transform: skewY(-2deg) scale(1.05);
}

/* Swing animation */
@keyframes swing {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(15deg); }
    75% { transform: rotate(-15deg); }
}

.swing {
    transform-origin: top center;
    animation: swing 2s ease-in-out infinite;
}

/* Bounce entrance */
@keyframes bounce-in {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.bounce-in {
    animation: bounce-in 0.6s;
}

/* Perspective cards */
.card-3d {
    transition: transform 0.3s;
}

.card-3d:hover {
    transform: perspective(1000px) rotateX(10deg) rotateY(10deg);
}

/* Door open effect */
.door {
    width: 200px;
    height: 300px;
    position: relative;
    transform-style: preserve-3d;
}

.door-panel {
    width: 100%;
    height: 100%;
    transform-origin: left center;
    transition: transform 1s;
}

.door:hover .door-panel {
    transform: rotateY(-120deg);
}

/* Text effects */
.text-3d {
    transform: perspective(500px) rotateY(15deg);
    text-shadow: 
        1px 1px 0 #ccc,
        2px 2px 0 #c9c9c9,
        3px 3px 0 #bbb,
        4px 4px 0 #b9b9b9;
}

/* Floating animation */
@keyframes float {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-20px);
    }
}

.float {
    animation: float 3s ease-in-out infinite;
}

/* Pulse effect */
@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
}

.pulse {
    animation: pulse 2s ease-in-out infinite;
}

💡 Performance Tips

/* 1. Use transform instead of position */
/* Bad (causes layout recalculation) */
.element {
    position: relative;
    left: 100px;
    top: 50px;
}

/* Good (GPU accelerated) */
.element {
    transform: translate(100px, 50px);
}

/* 2. Use will-change for complex transforms */
.complex {
    will-change: transform;
}

.complex:hover {
    transform: rotate(45deg) scale(1.5) translateZ(100px);
}

/* 3. Combine transforms in single property */
/* Bad */
.element {
    transform: rotate(45deg);
    transform: scale(1.5);  /* Overwrites rotate */
}

/* Good */
.element {
    transform: rotate(45deg) scale(1.5);
}

/* 4. Use translate3d to force GPU acceleration */
.smooth {
    transform: translate3d(100px, 50px, 0);
}

/* 5. Avoid transform on many elements simultaneously */
/* Stagger animations instead */

/* 6. Use transform for animations */
/* Better than animating width, height, top, left */

🎯 Key Takeaways