The transform-origin property specifies the point within an element around which transformations are applied. This can significantly affect the visual outcome of transformations like rotation, scaling, and skewing.
Syntax
CSS
transform-origin: x-offset y-offset;
- x-offset: The horizontal position of the origin point, relative to the element’s top-left corner.
- y-offset: The vertical position of the origin point, relative to the element’s top-left corner.
Default Value
The default value is 50% 50%, which means the origin point is at the center of the element.
Examples
- Center: transform-origin: 50% 50%;
- Top left: transform-origin: 0 0;
- Bottom right: transform-origin: 100% 100%;
- Specific coordinates: transform-origin: 20px 50px;
Using transform-origin with Transformations
When combined with transformation functions like rotate, scale, and skew, transform-origin can create various effects:
Rotating from the center:
CSS
.element {
transform: rotate(45deg);
}
Rotating from the top left:
CSS
.element {
transform-origin: 0 0;
transform: rotate(45deg);
}
Scaling from the bottom right:
CSS
.element {
transform-origin: 100% 100%;
transform: scale(1.5);
}
Responsive Considerations
- Use relative units: Use em or rem for x-offset and y-offset values to create responsive transformations.
- Combine with media queries: Adjust transform-origin based on screen size to achieve desired effects.
The transform-origin property is essential for controlling the behavior of transformations in CSS. By understanding its syntax and usage, you can create more precise and visually appealing effects in your responsive web designs.