CSS 2D Transforms


CSS 2D Transforms

CSS transformations allow you to move, turn, change size, and tilt elements on a webpage.

Move your mouse over the object below to see a 2D change happening:

2D rotate

In this section, we'll acquaint you with the following CSS property:

  • transform

CSS 2D Transforms Methods

You can use CSS transform to apply these 2D transformation techniques:

  • translate()
  • rotate()
  • scaleX()
  • scaleY()
  • scale()
  • skewX()
  • skewY()
  • skew()
  • matrix()

Tip: You will learn about 3D transformations in the next chapter.


The translate() Method

The translate() method moves an element from where it is now by telling it how far to go left or right (X-axis) and up or down (Y-axis).

In this example, the <div> element is shifted to the right by 50 pixels and moved downward by 100 pixels from its current location.


The rotate() Method

The CSS rotate() function spins an element in either a clockwise or counter-clockwise direction by a certain number of degrees.

This example shows how to make a <div> element turn 20 degrees clockwise.

To rotate the element counter-clockwise, you can use negative values.

The given code example makes a <div> element rotate 20 degrees in a counter-clockwise direction.

The scale() Method

The scale() method changes the size of an element by adjusting its width and height parameters.

The code below enlarges the <div> section to twice its initial width and three times its original height:

The example below reduces the size of the <div> element to half of its original width and height:

The scaleX() Method

The scaleX() function adjusts the width of an element, making it either larger or smaller.

The given illustration enlarges the <div> element to twice its original width.

The scaleY() Method

The scaleY() function adjusts the height of an element, making it larger or smaller.

To make the <div> element three times taller than its original height, refer to the following example:

This example reduces the height of the <div> element to half of its original size:

The skewX() Method

The skewX() function tilts an element horizontally based on the specified angle along the X-axis.

This example tilts the <div> element by 20 degrees along the X-axis:

The skewY() Method

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

This example tilts the <div> element by 20 degrees along the Y-axis:

The skew() Method

The skew() method tilts an element horizontally and vertically based on specified angles along the X and Y-axis.

The given code distorts the <div> element by 20 degrees horizontally (X-axis) and 10 degrees vertically (Y-axis).

If you don't provide a second value, it defaults to zero. In this example, the <div> element is tilted 20 degrees on the X-axis:

The matrix() Method

The matrix() method puts together all the 2D transform methods into a single one.

The matrix() method uses six mathematical functions as parameters. It helps you rotate, scale, move (translate), and skew elements on a webpage.

Here are the parameters: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY())

CSS Transform Properties

The table below shows the properties used for 2D transformations:

Property Description
transform Applies a change in position or appearance to an element, either in two dimensions (2D) or three dimensions (3D).
transform-origin Enables you to adjust the placement of transformed elements.

CSS 2D Transform Methods

Function Description
matrix(n,n,n,n,n,n) Describes a 2D change using a set of six values in a matrix.
translate(x,y) Describes a movement in two dimensions, shifting the element horizontally (X-axis) and vertically (Y-axis).
translateX(n) Describes a 2D shift that moves the element horizontally on the screen.
translateY(n) Describes a movement of an element in a 2D space, specifically shifting it vertically along the Y-axis.
scale(x,y) Describes a transformation that resizes elements in two dimensions by adjusting their width and height.
scaleX(n) Describes a transformation that adjusts the width of an element using a 2D scale.
scaleY(n) Describes a transformation that adjusts the height of an element using a 2D scaling effect.
rotate(angle) Specifies a rotation in two dimensions, with the angle provided as a parameter.
skew(x-angle,y-angle) Describes a tilt in a two-dimensional space along the horizontal (X-axis) and vertical (Y-axis) directions.
skewX(angle) Describes a tilt in two dimensions along the horizontal axis (X-axis).
skewY(angle) Specifies a tilt change in a two-dimensional manner along the vertical axis (Y-axis).