Curriculum
Course: CSS Advanced
Login

Curriculum

CSS Advanced

CSS Rounded Corners

0/1

CSS Border Images

0/1

CSS Color Keywords

0/1

CSS Text Effects

0/1

CSS 2D Transforms

0/1

CSS 3D Transforms

0/1

CSS Transitions

0/1

CSS Animations

0/1

CSS Tooltip

0/1

CSS Style Images

0/1

CSS Image Reflection

0/1

CSS Masking

0/1

CSS Buttons

0/1

CSS Multiple Columns

0/1

CSS User Interface

0/1

CSS Box Sizing

0/1

CSS Media Queries

0/1
Text lesson

CSS 2D Transforms

CSS 2D Transforms

CSS transforms enable the manipulation of elements by moving, rotating, scaling, and skewing them.

Hover over the element below to observe a 2D transformation:

2D rotate

 

This chapter covers the following CSS property:

  • transform

Browser Support

The numbers in the table indicate the initial browser version that offers complete support for the property.

Image

CSS 2D Transforms Methods

Using the CSS transform property, you can employ the following 2D transformation techniques:

  • translate()
  • rotate()
  • scaleX()
  • scaleY()
  • scale()
  • skewX()
  • skewY()
  • skew()
  • matrix()
Note: The next chapter will cover 3D transformations.

The translate() Method

Image

The translate() method relocates an element from its current position based on the specified parameters for the X-axis and the Y-axis.

In the following example, the <div> element is shifted 50 pixels to the right and 100 pixels downward from its current position:

Example 

div {
  transform: translate(50px, 100px);
}

The rotate() Method

Image

The rotate() method rotates an element either clockwise or counterclockwise by a specified degree.

In the following example, the <div> element is rotated clockwise by 20 degrees:

Example 

div {
  transform: rotate(20deg);
}

Negative values can be used to rotate the element counterclockwise.

In the following example, the <div> element is rotated counterclockwise by 20 degrees:

Example 

div {
  transform: rotate(-20deg);
}

The scale() Method

Image

The scale() method enlarges or reduces the size of an element based on the specified parameters for width and height.

In the following example, the <div> element is expanded to twice its original width and three times its original height:

Example 

div {
  transform: scale(2, 3);
}

In the following example, the <div> element is reduced to half its original width and height:

Example 

div {
  transform: scale(0.5, 0.5);
}

The scaleX() Method

The scaleX() method adjusts the width of an element, either enlarging or reducing it.

In the following example, the <div> element is expanded to twice its original width:

Example 

div {
  transform: scaleX(2);
}

In the following example, the <div> element is reduced to half its original width:

Example 

div {
  transform: scaleX(0.5);
}

The scaleY() Method

The scaleY() method adjusts the height of an element, either enlarging or reducing it.

In the following example, the <div> element is expanded to three times its original height:

Example 

div {
  transform: scaleY(3);
}

In this example, the <div> element is resized to half of its original height.

Example 

div {
  transform: scaleY(0.5);
}

The skewX() Method

The skewX() method tilts an element along the X-axis by the specified angle.

Here’s an example where the <div> element is tilted 20 degrees along the X-axis:

Example 

div {
  transform: skewX(20deg);
}

The skewY() Method

The skewY() method tilts an element along the Y-axis by the specified angle.

Here’s an example where the <div> element is skewed by 20 degrees along the Y-axis:

Example 

div {
  transform: skewY(20deg);
}

The skew() Method

The skew() method tilts an element along both the X and Y axes based on the specified angles.

In the following example, the <div> element is tilted 20 degrees along the X-axis and 10 degrees along the Y-axis:

Example 

div {
  transform: skew(20deg, 10deg);
}

If the second parameter is omitted, it defaults to zero. Therefore, the following example skews the <div> element 20 degrees along the X-axis:

Example 

div {
  transform: skew(20deg);
}

The matrix() Method

Image

The matrix() method consolidates all 2D transform operations into a single function.

It accepts six parameters representing mathematical functions, enabling rotation, scaling, translation, and skewing of elements.

The parameters are as follows: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY())

Example 

div {
  transform: matrix(1, -0.3, 0, 1, 0, 0);
}