Skip to main content

CSS 2D Transforms

Moving, rotating, scaling and skewing an element all go through one CSS property, transform, which handles transformations in either 2D or 3D.


CSS 2D Transforms Functions

For work in two dimensions, transform accepts these functions:

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

The CSS translate() Function

Give translate() one value for the X-axis and one for the Y-axis, and the element shifts away from where it currently sits by those amounts.

Example

The <div> here ends up 100 pixels lower and 50 pixels further right than its starting point.

Prompt
Create an HTML page with a div element that uses CSS transform: translate(50px, 100px) to move it 50 pixels right and 100 pixels down from its original position. Show a border or background color so the movement is visible.
Output: complete HTML page

The CSS rotate() Function

To turn an element, pass rotate() a number of degrees. The direction can be either clockwise or counter-clockwise.

Rotate clockwise

A 20-degree clockwise turn is applied to the <div> below.

Prompt
Create an HTML page with a div element that uses CSS transform: rotate(20deg) to rotate it 20 degrees clockwise. Give the div a background color and some padding so the rotation is visible.
Output: complete HTML page

Rotate counter-clockwise

Turning the other way only takes a negative value. Below, the same <div> goes 20 degrees counter-clockwise.

Prompt
Create an HTML page with a div element that uses CSS transform: rotate(-20deg) to rotate it 20 degrees counter-clockwise. Give the div a background color and some padding so the rotation is visible.
Output: complete HTML page

CSS Transform Properties

Every property available for 2D transforms appears in this table.

Transform properties table

index.html
<table>
  <tr><th>Property</th><th>Description</th></tr>
  <tr><td>transform</td><td>Applies a 2D or 3D transformation to an element</td></tr>
  <tr><td>transform-origin</td><td>Allows you to change the position on transformed elements</td></tr>
</table>