π― CSS Tutorial: transform-origin
π Introduction
The transform-origin property in CSS controls the **point around which a transform is applied**. When you rotate, scale, or skew an element using transform, the transformation occurs around a default center point β but transform-origin lets you change that pivot. This is extremely useful for animations, rotations, scaling effects, and UI micro-interactions. β¨
π― What Does transform-origin Control?
- Sets the pivot point for transformations
- Accepts keywords, lengths, or percentages
- Can move the origin outside the element
- Works with rotate, scale, skew, translate
β¨ Syntax
Syntax
selector {
transform-origin: <x-axis> <y-axis> <z-axis optional>;
}Note
The **default** transform-origin is:
50% 50% 0 β center of the element.
50% 50% 0 β center of the element.
π§© Accepted Values
1. Keywords
Keyword Examples
transform-origin: left top;
transform-origin: center center;
transform-origin: right bottom;- left, center, right β x-axis
- top, center, bottom β y-axis
2. Percentages
Based on elementβs dimensions.
Percentage Example
transform-origin: 0% 50%; /* left center */
transform-origin: 100% 100%; /* bottom right */3. Length Values (px, rem)
Length Example
transform-origin: 20px 40px;4. 3D Transform Origin (optional z-axis)
3D Origin
transform-origin: 50% 50% 50px;π₯ Practical Examples
1. Rotate From the Top
Rotate From Top
.rotate-top {
transform-origin: top;
transform: rotate(45deg);
}2. Scale From the Bottom
Scale From Bottom
.scale-bottom {
transform-origin: bottom;
transform: scaleY(1.5);
}3. Door Opening Animation (hinge on left)
Door Animation
.door {
transform-origin: left center;
transform: rotateY(-70deg);
}4. Tooltip Animation (grow from center-top)
Tooltip Origin
.tooltip {
transform-origin: center top;
transform: scale(0);
transition: transform 0.2s ease;
}
.tooltip.show {
transform: scale(1);
}5. Spinner Rotation Around Custom Point
Custom Spinner
.spinner {
transform-origin: 50% 20%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}π transform-origin Cheat Sheet
| Value | Pivot Point |
|---|---|
| center | Element center (default) |
| left top | Top-left corner |
| right bottom | Bottom-right corner |
| 0% 100% | Bottom-left edge |
| 100% 0% | Top-right edge |
π§ Tips & Best Practices
- Always adjust transform-origin before animating transforms
- Use percentages for responsive transformations
- Use length values when you need pixel-precision pivot points
- Combine with transition or animation for smooth effects
- 3D transforms respond to z-axis origin adjustments
>>βA perfect animation starts with the perfect pivot β transform-origin gives you that control.β β¨
π Conclusion
The transform-origin property is a key part of creating polished animations and transformations. By controlling the pivot point, you can rotate, scale, skew, and animate elements with precision and visual intention. Master it β and your interactive UI effects will instantly level up. π