CSS Transforms
In this page:
selector {
transform: function(value); /* translate(), rotate(), scale(), skew() */
}
Shifting Elements with translate
The translate() method moves an element from its normal position along the horizontal X-axis and vertical Y-axis, serving as a lightweight and high-performance way to offset layouts compared to using margins.
- Use transform: translate() instead of absolute margins to build smooth, hardware-accelerated animations.
- Accepted values:
- translate(x, y) — moves the element
- rotate(angle) — angle in deg, turn, or rad
- scale(x, y) — number multipliers
- skew(x, y) — angle distortion
- matrix() — all 2D transforms in one function
- none — no transform
Example: Shifting Elements with translate
<style>
.box {
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
.box:hover {
transform: translate(20px, 10px);
}
</style>
<div class="box">Shifted right and down</div>
Spinning Elements with rotate
The rotate() method spins an element clockwise or counter-clockwise around its center point by a specified number of degrees (deg) or turns.
- Use positive degrees to rotate clockwise, and negative degrees to rotate counter-clockwise.
- Accepted values:
- rotate(<angle>) — 2D rotation, clockwise when positive
- rotate(45deg) — degrees
- rotate(0.25turn) — turns, also rad and grad
- rotateX() | rotateY() | rotateZ() — axis rotations
- transform-origin — sets the pivot point
Example: Spinning Elements with rotate
<style>
.box {
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
.box:hover {
transform: rotate(45deg);
}
</style>
<div class="box">Rotated 45 degrees clockwise</div>
Resizing Elements with scale
The scale() method increases or decreases the display size of an element. It accepts multiplier values (like 1.5 to scale up to 150%, or 0.5 to scale down to 50%).
- Use scale transforms inside hover states to build elegant card zoom transitions on your pages.
- Accepted values:
- scale(<number>) — uniform scale, 1 is unchanged
- scale(x, y) — separate horizontal and vertical factors
- scaleX(<number>) | scaleY(<number>) — one axis only
- scale(-1) — a negative number mirrors the element
Example: Resizing Elements with scale
<style>
.box:hover {
transform: scale(1.5);
}
</style>
<div class="box">Scaled up to 150%</div>
Twisting Elements with skew
The skew() method tilts an element along the X-axis or Y-axis by a specified angle. This creates a slanted, parallelogram-like effect, which is useful for creating dynamic background shapes and designs.
- Pair skew transforms with hidden overflow containers to create sleek, angled layout divider shapes.
- Accepted values:
- skew(<angle>) — skews along the X axis
- skew(x, y) — X and Y angles
- skewX(<angle>) — horizontal skew only
- skewY(<angle>) — vertical skew only
Example: Twisting Elements with skew
<style>
.box {
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
.box:hover {
transform: skew(20deg, 0deg);
}
</style>
<div class="box">Slanted along the X-axis</div>
Combining Multiple Transforms
You can apply multiple transform methods to a single element by writing them in a space-separated list inside the transform property, allowing you to combine translation, rotation, and scaling smoothly.
- Write your transform methods in a single, space-separated list; do not declare the transform property multiple times.
- Accepted values:
- transform: A B C — space-separated list, applied right to left visually
- none — no transform (default)
- translate() rotate() scale() — the most common combination
- matrix() — a single combined 2D matrix
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Combining Multiple Transforms
<style>
.box {
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
.box:hover {
transform: translate(10px, 10px) rotate(15deg) scale(1.1);
}
</style>
<div class="box">Translated, rotated, and scaled together</div>
- Writing multiple separate transform properties on an element, which causes later rules to overwrite earlier ones.
- Attempting to rotate or scale elements without realizing they do not affect surrounding page layouts.
- Applying extreme skew angles that tilt text containers past readability limits.
- The transform property applies structural shifts, rotations, scaling, and skews to elements without affecting surrounding layouts.
- Use translate() to offset elements, rotate() to spin them, scale() to resize them, and skew() to tilt them.
- Combine multiple transform methods inside a single, space-separated list to animate elements smoothly.
Standard 2D CSS transforms are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties