Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › transform

CSS Reference

transform

Moves, rotates, scales, and skews elements without affecting layout. The foundation of CSS animation.

Last updated: October 2026

Definition: The transform property visually transforms an element without changing its actual position in the layout. The space the element occupies stays the same, but its appearance moves. It supports 2D and 3D transformations and is GPU-accelerated for smooth animation.

🎬 Live Examples

Six transform functions, each in its own box.

translateX
moved right
translateY
moved up
rotate
rotated 15°
scale
15% larger
skewX
slanted
combined
3 functions

💡 Transforms don't affect the layout. Notice the boxes stay in their original grid cells.

📝

Basic Syntax

.element {
    transform: translateX(20px);
}

.element {
    transform: rotate(45deg);
}

/* Chain multiple transforms (order matters) */
.element {
    transform: translateX(20px) rotate(45deg) scale(1.5);
}

🎯 All transform Functions

Function Purpose Example
translate(x, y)Moves the elementtranslate(10px, 20px)
translateX(x)Moves horizontallytranslateX(20px)
translateY(y)Moves verticallytranslateY(-10px)
rotate(deg)Rotates clockwiserotate(45deg)
scale(x, y)Scales sizescale(1.2), scale(1, 2)
scaleX(x) / scaleY(y)Scale one axisscaleX(1.5)
skew(x, y)Slants the elementskewX(-15deg)
matrix(...)Low-level 2D matrixRarely used manually
noneRemove all transformsReset
1

The Classic Centering Trick

.modal {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

The -50%, -50% values are relative to the element's own size. This moves it back by half its width and height, centering it perfectly. Still one of the most-used transform patterns.

2

Hover Lift Effect

.card {
    transition: transform 0.2s ease;
}

.card:hover {
    transform: translateY(-4px);
}

Because transform doesn't affect layout, the neighboring cards don't move. This creates a smooth, GPU-accelerated lift on hover.

3

transform-origin

By default, transforms originate from the center of the element. Change the anchor with transform-origin.

/* Rotate around the top-left corner */
.rotate-tl {
    transform-origin: top left;
    transform: rotate(15deg);
}

/* Scale from the bottom */
.grow-up {
    transform-origin: bottom;
    transform: scale(1.5);
}

/* Pop from center (default) */
.pop {
    transform-origin: center;
    transform: scale(1.2);
}

Accepts keywords (top, center, bottom, left, right), percentages, or pixel values.

4

3D Transforms

/* Perspective on the parent */
.scene {
    perspective: 1000px;
}

/* 3D rotation */
.card-3d {
    transform: rotateY(45deg);
}

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

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

3D transforms require perspective on the parent for depth. Great for flip cards, tilt effects, and rotating cubes.

5

Transform Order Matters

Transforms apply left to right. Changing the order changes the result.

/* Move first, then rotate */
.a {
    transform: translateX(50px) rotate(45deg);
}

/* Rotate first, then move (moves in rotated direction) */
.b {
    transform: rotate(45deg) translateX(50px);
}

This is a common source of confusion. If your transforms look wrong, try reordering.

🎯 Common transform Patterns

Effect Transform
Centered modaltranslate(-50%, -50%)
Hover lifttranslateY(-4px)
Slight zoom on hoverscale(1.05)
Full-size image on hoverscale(1.1)
Menu slide intranslateX(-100%)
Notification popscale(0) → scale(1)
Icon rotate on clickrotate(180deg)

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS transform property?

The transform property moves, rotates, scales, or skews an element without affecting the layout of surrounding elements. It is the foundation of modern CSS animations, hover effects, and 3D interfaces.

What is the difference between transform and position?

position moves elements within the layout flow. transform moves elements visually without affecting layout — the space the element occupies stays the same. This makes transform better for animations because it doesn't cause reflows.

What does translate(-50%, -50%) do?

translate(-50%, -50%) moves the element back by half its own width and height. Combined with top: 50%; left: 50%, it perfectly centers an absolutely positioned element. This is a classic centering trick.

Does transform create a stacking context?

Yes. Any element with a transform value other than 'none' creates a new stacking context. This means child z-index values are scoped to that element and cannot be compared with elements outside it.

How do I combine multiple transforms?

Chain them in a single transform declaration, separated by spaces. Example: transform: translateX(20px) rotate(45deg) scale(1.5). Order matters — transforms apply left to right.

🎯 What's Next?

← All CSS Reference