The powerful CSS transform feature lets you apply different modifications to HTML components. Among other effects, it can be used to move, scale, skew, and rotate an element. Here’s a brief rundown of a few typical transformations:
Table of Contents
ToggleBasic Transformations
Translate
An element is moved from its existing location.
transform: translate(50px, 100px); /* Moves the element 50px right and 100px down */
Scale
Increases or decreases the element’s scale.
transform: scale(1.5); /* Scales the element to 150% of its original size */
transform: scale(1.5, 0.5); /* Scales the element to 150% width and 50% height */
Rotate
Rotates the component by a predetermined amount.
transform: rotate(45deg); /* Rotates the element 45 degrees clockwise */
Skew
Modifies the element’s X or Y-axis skew.
transform: skew(20deg, 10deg); /* Skews the element 20 degrees along the X-axis and 10 degrees along the Y-axis */
Combining Transformations
Multiple changes can be combined by chaining each of them and separating them with spaces.
transform: translate(50px, 100px) scale(1.5) rotate(45deg);
Example
The following is an illustration of how to utilize the change property within a CSS transform rule:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
width: 100px;
height: 100px;
background-color: lightblue;
transform: rotate(30deg) translate(20px, 50px) scale(1.2);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
3D Transformations
You can use 3D transformations alongside 2D transformations in css transform:
transform: rotateX(45deg) rotateY(30deg);
CSS Transform Origin
With transform-origin, the transformational centre of gravity can be adjusted.
transform-origin: 50% 50%; /* Default: center of the element */
transform-origin: top left; /* Top left corner of the element */
Transition and Animation
You can utilize transition to improve the smoothness of transformations:
With CSS transitions, you may gradually alter an element’s value of properties during a predetermined amount of time when a state change—such as a click or hover—occurs. They increase the fluidity of state transitions.
Key Properties:
Transition-property
Indicates which CSS property (such as transform or opacity) should be animated.
Transition-duration
Specifies the amount of time (e.g., 0.5s meaning half a second) the transition takes.
Transition-timing-function
Describes the speed curve of the transition (e.g., ease-in
, linear
).
Transition-delay
Delays the start of the transition (e.g., 0.2s
for a 0.2-second delay).
Example:
.box {
transition: transform 0.5s ease-in-out;
}
.box:hover {
transform: rotate(45deg) scale(1.2);
}
In this example, the. box gently rotates about 45 degrees over a period of 0.5 seconds when it is hovered over.
Animation
With CSS animations, you can program intricate keyframe-based animation sequences to play frequently and automatically without requiring user input.
Key Properties:
- @keyframes: Specifies the styles at different intervals (keyframes) during the animation, defining the animation sequence.
- Animation-name: Your @keyframes animation to be applied’s name.
- Animation-duration: Indicates the amount of time needed for one cycle of the animation to finish.
- Animation-timing-function: Explains the progression of the animation across the keyframes (e.g., steps(5), ease-in-out).
- Animation-iteration-count: Indicates the number of times an animation should play (unlimited for endless recurrence, for example).
- Animation-delay: Postpones the animation’s beginning.
- Animation-direction (normal, reverse, alternate): Indicates whether the films should play forward, backward, or alternately between the two.
Example:
@keyframes slideAndFade {
0% { transform: translateX(-100px); opacity: 0; }
50% { transform: translateX(0); opacity: 1; }
100% { transform: translateX(100px); opacity: 0; }
}
.box {
animation: slideAndFade 3s ease-in-out infinite;
}
In this instance, the. box will repeat endlessly while sliding from right to left and fading out and in over a period of three seconds.
Summary
You can apply a variety of visual effects to items using the CSS transform property, such as skewing (skew), rotating (rotate), scaling (scale), and moving (translate). For more dynamic effects, these modifications can be mixed and animated. Furthermore, 3D transformations like rotateX and rotateY are supported. The transition property can smoothly animate changes, while the transform-origin property determines the point around which these transformations occur.