CSS Transforms – Translate, Rotate, Scale, and Skew

Ridoy Hasan - Sep 10 - - Dev Community

Lecture 12: CSS Transforms – Translate, Rotate, Scale, and Skew

Welcome to the twelfth lecture of the "Basic to Brilliance" course. Today, we’ll be exploring CSS Transforms, a powerful feature that lets you manipulate the position, size, and orientation of elements. We'll cover the basics of transform functions like translate(), rotate(), scale(), and skew().


1. Transform Property Overview

The transform property allows you to apply various transformations to elements, such as translating, rotating, scaling, and skewing. These transformations are applied relative to the element's original position.

  • Syntax:
  transform: transform-function(value);
Enter fullscreen mode Exit fullscreen mode

2. Translate

The translate() function moves an element from its original position along the X and Y axes. You can specify values in pixels (px), percentages (%), or other units.

  • Example: Translating an element 50px right and 30px down.
  .box {
    transform: translate(50px, 30px);
  }
Enter fullscreen mode Exit fullscreen mode

In this example, the .box element will move 50px to the right and 30px down from its original position.


3. Rotate

The rotate() function rotates an element around a fixed point, which is the center of the element by default. The rotation angle is specified in degrees (deg).

  • Example: Rotating an element by 45 degrees.
  .box {
    transform: rotate(45deg);
  }
Enter fullscreen mode Exit fullscreen mode

In this case, the .box element will be rotated 45 degrees clockwise around its center.


4. Scale

The scale() function resizes an element. You can specify scaling factors for the X and Y axes. A value of 1 means the element's original size, while values greater or smaller than 1 increase or decrease the size, respectively.

  • Example: Scaling an element to 1.5 times its original size.
  .box {
    transform: scale(1.5);
  }
Enter fullscreen mode Exit fullscreen mode

In this example, the .box element will be scaled up to 150% of its original size.


5. Skew

The skew() function slants the element along the X and Y axes. The angles are specified in degrees (deg).

  • Example: Skewing an element 20 degrees along the X-axis and 10 degrees along the Y-axis.
  .box {
    transform: skew(20deg, 10deg);
  }
Enter fullscreen mode Exit fullscreen mode

In this case, the .box element will be skewed 20 degrees horizontally and 10 degrees vertically.


6. Combining Transform Functions

You can combine multiple transform functions in a single transform property. The functions are applied in the order they appear.

  • Example: Combining translate, rotate, and scale.
  .box {
    transform: translate(50px, 30px) rotate(45deg) scale(1.5);
  }
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The .box will first be translated, then rotated, and finally scaled. Each transformation is applied sequentially.

7. Transform Origin

The transform-origin property specifies the point around which the transformations occur. By default, this is the center of the element, but you can change it to any point.

  • Example: Changing the transform origin to the top-left corner.
  .box {
    transform-origin: top left;
    transform: rotate(45deg);
  }
Enter fullscreen mode Exit fullscreen mode

In this case, the .box will rotate 45 degrees around its top-left corner rather than its center.


8. 3D Transforms

CSS also supports 3D transformations. You can use functions like perspective(), rotateX(), rotateY(), and translateZ() to create 3D effects.

  • Example: Adding perspective to create a 3D effect.
  .container {
    perspective: 1000px;
  }

  .box {
    transform: rotateY(45deg);
  }
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The .container element provides perspective, making the .box appear to rotate in 3D space.

Practice Exercise

  1. Create a card flip animation using CSS transforms. The card should rotate 180 degrees on hover to reveal the back.
  2. Experiment with combining translate, rotate, and scale to create a unique effect on an element.

Next Up: In the next lecture, we’ll dive into CSS Animations and learn how to create dynamic, animated effects for your web elements. Get ready to bring your designs to life with motion!


Follow me on LinkedIn-

Ridoy Hasan

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player