CSS 2D Transforms

Move, rotate, scale, and skew elements in 2D space

๐Ÿ”„ CSS 2D Transforms

CSS transforms allow you to move, rotate, scale, and skew elements without affecting the document flow. Perfect for creating interactive effects and animations.


/* Basic 2D transforms */
.transform-example {
    transform: rotate(45deg) scale(1.2) translate(20px, 10px);
    transition: transform 0.3s ease;
}

.transform-example:hover {
    transform: rotate(90deg) scale(1.5) translate(30px, 20px);
}
                                    

Output (hover to see effect):

Hover Me

2D Transform Functions

๐Ÿ“

Translate

Move elements along X and Y axes

transform: translate(50px, 100px);
transform: translateX(50px);
transform: translateY(100px);
๐Ÿ”„

Rotate

Rotate elements around their center

transform: rotate(45deg);
transform: rotate(-90deg);
transform: rotate(0.5turn);
๐Ÿ“

Scale

Resize elements proportionally

transform: scale(1.5);
transform: scale(2, 0.5);
transform: scaleX(1.5);
๐Ÿ“Š

Skew

Distort elements along axes

transform: skew(20deg, 10deg);
transform: skewX(20deg);
transform: skewY(10deg);

๐Ÿ”น Transform Examples

The CSS transform property enables you to move, rotate, scale, and skew HTML elements in 2D or 3D space, fundamentally altering their visual presentation without disrupting the document flow. Common functions include translate() to reposition, rotate() to spin, and scale() to resize. These transformations are applied directly to an elementโ€™s rendering, allowing for smooth animations and interactive effects. Understanding these functions is crucial for creating modern, dynamic user interfaces, hover effects, and complex animations without relying on additional graphics or JavaScript for basic visual manipulation. This property is hardware-accelerated for optimal performance.

/* Individual transforms */
.translate { transform: translate(30px, 20px); }
.rotate { transform: rotate(45deg); }
.scale { transform: scale(1.3); }
.skew { transform: skew(15deg, 5deg); }

/* Combined transforms */
.combined {
    transform: translate(20px, 10px) rotate(30deg) scale(1.2);
}

Output:

Translate
Rotate
Scale
Skew
Combined

๐Ÿ”น Transform Origin

The transform-origin property precisely controls the fixed point, or pivot, around which CSS transformations such as rotation or scaling occur. Its default value is 50% 50%, centering the transformation. You can define it using keywords (left, top), percentages, or length units. For example, setting transform-origin: left top makes an element rotate from its top-left corner. This is essential for creating realistic animations like a swinging door hinge or a radial menu, ensuring transformations behave predictably. Mastering transform-origin is key to achieving professional, polished motion design in CSS animations and transitions.

/* Transform origin examples */
.origin-center { 
    transform-origin: center;
    transform: rotate(45deg);
}

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

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

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

Output:

Center
Top Left
Bottom Right

๐Ÿ”น Practical Transform Examples

In real-world web development, 2D transforms are used to create interactive cards, animated buttons, and responsive image galleries that enhance user engagement. For instance, a product card can use scale(1.05) on hover for a subtle zoom effect, while a navigation menu might employ translateY() for a slide-in animation. These techniques improve the perceived performance and interactivity of a website. They are also foundational for building micro-interactions that provide immediate user feedback, contributing to a more intuitive and visually appealing experience without heavy JavaScript libraries, thus improving site speed and SEO through efficient code.

/* Card hover effect */
.card {
    transition: transform 0.3s ease;
}
.card:hover {
    transform: translateY(-10px) scale(1.05);
}

/* Loading spinner */
.spinner {
    animation: spin 1s linear infinite;
}
@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Button press effect */
.button:active {
    transform: scale(0.95);
}

Output:

Hover Card

๐Ÿง  Test Your Knowledge

Which transform function moves an element without rotating it?