CSS Transforms
In this page:
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.
Note: Use transform: translate() instead of absolute margins to build smooth, hardware-accelerated animations.
Warning: Translated elements do not affect the normal page flow, so they can overlap with adjacent elements.
Example: Shifting Elements with translate
<style>
.box {
transform: translate(20px, 10px);
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
</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.
Note: Use positive degrees to rotate clockwise, and negative degrees to rotate counter-clockwise.
Warning: Rotating text containers can make the characters look blurry or jagged on some screens.
Example: Spinning Elements with rotate
<style>
.box {
transform: rotate(45deg);
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
</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%).
Note: Use scale transforms inside hover states to build elegant card zoom transitions on your pages.
Warning: Scaling up raster images can make them look blurry and pixelated; use scalable vector SVGs instead.
Example: Resizing Elements with scale
<style>
.box {
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.
Note: Pair skew transforms with hidden overflow containers to create sleek, angled layout divider shapes.
Warning: Skewing container cards will also skew all the text inside them, which can make it hard to read.
Example: Twisting Elements with skew
<style>
.box {
transform: skew(20deg, 0deg);
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
</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.
Note: Write your transform methods in a single, space-separated list; do not declare the transform property multiple times.
Warning: Writing multiple separate transform declarations will cause the later rules to completely overwrite the earlier ones.
Example: Combining Multiple Transforms
<style>
.box {
transform: translate(10px, 10px) rotate(15deg) scale(1.1);
background: steelblue;
color: white;
padding: 10px;
display: inline-block;
}
</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