Categories
CSS Web development

CSS 2D Transforms

CSS transforms allow you to move, rotate, scale, and skew elements by using the CSS transform property.

CSS 2d transforms

With the CSS transform property you can use the following 2D transformation methods:

  1. translate()
  2. rotate()
  3. scale()
  4. scaleX()
  5. scaleY()
  6. skew()
  7. skewX()
  8. skewY()
  9. matrix()

Translate()

The translate() method moves an element from its current position (according to the parameters given for the X-axis and the Y-axis).

The following example moves the <div> element 50 pixels to the right, and 100 pixels down from its current position:

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 300px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
  -ms-transform: translate(50px,100px); 
  transform: translate(50px,100px); 
}
</style>
</head>
<body>

<p>The translate() method moves an element from its current position:</p>

<div>
This div element is moved 50 pixels to the right, and 100 pixels down from its current position.
</div>

</body>
</html>

Output:

Rotate()

The rotate() method rotates an element clockwise or counter-clockwise according to a given degree.

The following example rotates the <div> element clockwise with 20 degrees:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 300px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
}

div#myDiv {
  -ms-transform: rotate(20deg); 
  transform: rotate(20deg); 
}
</style>
</head>
<body>

<div>
This a normal div element.
</div>

<div id="myDiv">
This div element is rotated clockwise 20 degrees.
</div>

</body>
</html>

Output:

Scale()

The scale() method increases or decreases the size of an element (according to the parameters given for the width and height).

The following example increases the <div> element to be two times of its original width, and three times of its original height: 

<!DOCTYPE html>
<html>
<head>
<style>
div {
  margin: 150px;
  width: 200px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
  -ms-transform: scale(2,3); 
  transform: scale(2,3); 
}
</style>
</head>
<body>

<div>
This div element is two times of its original width, and three times of its original height.
</div>

</body>
</html>

Output:

ScaleX()

The ScaleX() method increases or decreases the width of an element.

The following example increases the <div> element to be two times of its original width:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  margin: 150px;
  width: 200px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
  -ms-transform: scaleX(2); 
  transform: scaleX(2); 
  
</style>
</head>
<body>



<div>
This div element is two times of its original width.
</div>

</body>
</html>

Output:

ScaleY()

The ScaleY() method increases or decreases the height of an element.

The following example increases the <div> element to be three times of its original height: 

<!DOCTYPE html>
<html>
<head>
<style>
div {
  margin: 150px;
  width: 200px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
  -ms-transform: scaleY(3); 
  transform: scaleY(3); 
}
</style>
</head>
<body>

<div>
This div element is three times of its original height.
</div>

</body>
</html>

Output:

Skew()

The Skew() method skews an element along the X and Y-axis by the given angles.

The following example skews the <div> element 20 degrees along the X-axis, and 10 degrees along the Y-axis:

<!DOCTYPE html>
<html>
<head>
<style>
div {

  width: 300px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
  
}

div#myDiv {
 
  -ms-transform: skew(20deg,10deg); 
  transform: skew(20deg,10deg);
    
}
</style>
</head>
<body>

<div>
This a normal div element.
</div>

<div id="myDiv">
This div element is skewed 20 degrees along the X-axis, and 10 degrees along the Y-axis.
</div>

</body>
</html>

Output:

SkewX()

The SkewX() method skews an element along the X-axis by the given angle.

The following example skews the <div> element 20 degrees along the X-axis:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 300px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
}

div#myDiv {
  -ms-transform: skewX(20deg); 
  transform: skewX(20deg); 
}
</style>
</head>
<body>

<div>
This a normal div element.
</div>

<div id="myDiv">
This div element is skewed 20 degrees along the X-axis.
</div>

</body>
</html>

Output:

SkewY()

The SkewY() method skews an element along the Y-axis by the given angle.

The following example skews the <div> element 20 degrees along the Y-axis:

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 300px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
}

div#myDiv {
  -ms-transform: skewY(20deg); 
  transform: skewY(20deg); 
}
</style>
</head>
<body>

<div>
This a normal div element.
</div>

<div id="myDiv">
This div element is skewed 20 degrees along the Y-axis.
</div>

</body>
</html>

Output:

Matrix()

The Matrix() method combines all the 2D transform methods into one.

The matrix() method take six parameters, containing mathematic functions, which allows you to rotate, scale, move (translate), and skew elements.

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 300px;
  height: 100px;
  background-color: lightpink;
  border: 1px solid blue;
}

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

div#myDiv2 {
  -ms-transform: matrix(1, 0, 0.5, 1, 150, 0); 
  transform: matrix(1, 0, 0.5, 1, 150, 0);
}
</style>
</head>
<body>

<div>
This a normal div element.
</div>

<div id="myDiv1">
Using the matrix() method.
</div>

<div id="myDiv2">
Another use of the matrix() method.
</div>

</body>
</html>

Output:

Enjoy coding!

Leave a Reply