transform
PropertyThe CSS transform
property allows you to visually manipulate an element by rotating, scaling, skewing, or translating it. This property lets you apply 2D or 3D transformations to an element, altering its appearance and position without affecting the layout of other elements on the page.
Transformations are useful for animations, hover effects, and enhancing visual design.
element {
transform: transformation-function(value);
}
Where transformation-function
is one of several functions that defines how the element should be transformed.
translate()
: Moves the element.scale()
: Scales the element.rotate()
: Rotates the element.skew()
: Skews (distorts) the element.matrix()
: Combines multiple transformations into one.You can combine multiple transformations by separating them with spaces.
translate()
The translate()
function moves an element horizontally and/or vertically on the 2D plane.
transform: translate(x, y);
x
: The horizontal distance to move the element.y
: The vertical distance to move the element..box {
width: 100px;
height: 100px;
background-color: lightblue;
transform: translate(50px, 100px);
}
<div class="box">Translate</div>
.box
element is moved 50px to the right and 100px down from its original position without changing the document’s layout.Before transformation:
+----------------------------------------+
| |
| [Original box here] |
| |
+----------------------------------------+
After transform: translate(50px, 100px)
:
+----------------------------------------+
| |
| |
| |
| |
| [Moved box here] |
| |
+----------------------------------------+
scale()
The scale()
function resizes an element by increasing or decreasing its dimensions. It allows you to stretch or shrink the width and height of an element.
transform: scale(sx, sy);
sx
: Scaling factor in the horizontal direction (width).sy
: Scaling factor in the vertical direction (height).If you only specify one value (scale(sx)
), it applies uniformly to both directions.
.box {
width: 100px;
height: 100px;
background-color: lightgreen;
transform: scale(1.5, 0.5);
}
<div class="box">Scale</div>
The .box
becomes wider and shorter compared to its original size.
rotate()
The rotate()
function rotates an element around a fixed point (usually the center) by a specified angle.
transform: rotate(angle);
angle
: The degree to rotate the element. Positive values rotate the element clockwise, and negative values rotate it counterclockwise..box {
width: 100px;
height: 100px;
background-color: lightcoral;
transform: rotate(45deg);
}
<div class="box">Rotate</div>
.box
is rotated 45 degrees clockwise from its original position.The element will appear tilted by 45 degrees.
skew()
The skew()
function distorts an element along its horizontal and/or vertical axes.
transform: skew(x-angle, y-angle);
x-angle
: Angle to skew the element along the horizontal (X-axis).y-angle
: Angle to skew the element along the vertical (Y-axis).If you only specify one value (skew(x-angle)
), it applies the skew along the X-axis.
.box {
width: 100px;
height: 100px;
background-color: lightyellow;
transform: skew(20deg, 10deg);
}
<div class="box">Skew</div>
The .box
will be visually distorted as if “pulled” in both directions.
matrix()
The matrix()
function allows for more complex transformations by combining translation, rotation, scaling, and skewing into a single transformation. It takes six parameters that represent a 2D transformation matrix.
transform: matrix(a, b, c, d, e, f);
Where:
a
, b
, c
, d
represent scaling and rotation.e
, f
represent translation..box {
width: 100px;
height: 100px;
background-color: lightpink;
transform: matrix(1, 0.5, -0.5, 1, 50, 50);
}
<div class="box">Matrix</div>
You can apply multiple transformations to a single element by chaining transformation functions together.
.box {
width: 100px;
height: 100px;
background-color: lightblue;
transform: translate(50px, 50px) scale(1.5) rotate(30deg);
}
<div class="box">Combined Transforms</div>
.box
is first translated 50px right and down, then scaled to 1.5 times its size, and finally rotated by 30 degrees. Each transformation is applied in the order it is listed.The transform-origin
property defines the pivot point or center of transformation for an element. By default, transformations (such as rotation or scaling) are applied relative to the center of the element (50% 50%
).
transform-origin: x-axis y-axis;
x-axis
: Horizontal position (e.g., 50%
, left
, right
).y-axis
: Vertical position (e.g., 50%
, top
, bottom
)..box {
width: 100px;
height: 100px;
background-color: lightcoral;
transform-origin: top left;
transform: rotate(45deg);
}
<div class="box">Transform Origin</div>
.box
is rotated by 45 degrees, but the rotation happens from the top-left corner instead of the center.CSS transform
also supports 3D transformations, which add depth by manipulating elements on the Z-axis.
rotateX(angle)
: Rotates an element around the X-axis (horizontal).rotateY(angle)
: Rotates an element around the Y-axis (vertical).rotateZ(angle)
: Same as rotate()
, rotates around the Z-axis (in-plane)..box {
width: 100px;
height: 100px;
background-color: lightseagreen;
transform: rotateX(45deg) rotateY(45deg);
transform-style: preserve-3d;
}
<div class="box">3D Rotation</div>
.box
rotates 45 degrees along both the X-axis and Y-axis, creating a 3D effect.When applying 3D transformations, the perspective
property determines how the element’s depth is perceived.
.container {
perspective: 500px;
}
.box {
width: 100px;
height: 100px;
background-color: lightgreen;
transform: rotateY(45deg);
}
<div class="container">
<div class="box">Perspective</div>
</div>
.box
is rotated in 3D space, and the perspective
of the container creates a depth effect, where the element appears to recede as it rotates.You can combine
transform
with transition
to animate the transformations over time.
.box {
width: 100px;
height: 100px;
background-color: lightblue;
transition: transform 0.5s ease;
}
.box:hover {
transform: rotate(360deg) scale(1.2);
}
<div class="box">Hover me!</div>
.box
, it will rotate 360 degrees and scale up by 1.2 times over 0.5 seconds.The CSS transform
property is an incredibly versatile tool for altering the appearance and position of elements in both 2D and 3D space. Whether you’re shifting, resizing, rotating, or skewing elements, transform
opens up many creative possibilities for animations and dynamic effects in web design.
next -> Css Box Layout