×

CSS 2d Transform

Transform property in CSS is used mainly for enhancing the effects and to animate the elements.

Transform is an effect that changes the shape, size and position of an element.

It helps to manipulate an element without interrupting the default document.

There are two types of transformation 2D and 3D. In this article we are going to learn about 2D transform property of CSS in detail.

2D - Transform Property

transform :

transform property is used inside <style> tag. Various functions like translate(), rotate(), scale() and skew() can be used using this property.

Syntax:

transform: method (values) ;

Example:

transform: rotate (20 deg) ;

Here, the function rotate () is used to rotate the element by 20 degrees.

transform-origin :

By default, the origin point of an element is centre. To change the origin point of the transformation there another property called as transform-origin.

CSS 2d Transform

Syntax:

transform-origin: values ;

Example:

transform-origin: left top;

Changes the origin of transformation to the left top of the element.

transform-origin: 30% 50%;

Changes the origin of x-axis by 30% and y-axis by 50%.

2D Transform Methods:

translate() method:

This method translates the element from one point to another.

There are three types of translate () methods:

  1. translate(x, y)
  2. translateX(n)
  3. translateY(n)

translate(x, y):

Translates the element along the x-axis and y-axis respectively.

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: orange;
margin: 50px 40px;
}
</style>
</head>
<body>
<h1>CSS Transform</h1>
<div id="square">Example for Transform property</div>
</body>
</html>
Before using translate(x,y):
CSS 2d Transform

Output after using translate(x.y):

CSS 2d Transform

translateX(n):

This method takes only x co-ordinate as a parameter and moves the element along x-axis.

Example:

<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: orange;
margin: 50px 40px;
transform: translateX(100px);
}
</style>
</head>
<body>
<h1>CSS Transform</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Output: Moves the square to 100px along x-axis

CSS 2d Transform

translateY(n):

This method takes only y co-ordinate as a parameter and moves the element along y-axis.

Example:

<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#sqaure{
width: 250px;
height: 150px;
background: orange;
margin: 50px 40px;
transform: translateY(150px);
}
</style>
</head>
<body>
<h1>CSS Transform</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Output: Moves the square to 120px along y-axis

CSS 2d Transform

rotate() method:

The rotate() method rotates the element according to the given angle.

It can rotate the element clockwise or anti clockwise.

Syntax:

transform: rotate(angle);

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: skyblue;
margin: 50px 40px;
transform: rotate(45deg);
}
</style>
</head>
<body>
<h1>CSS Transform Rotate</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Before rotation:

CSS 2d Transform

Output after using rotate(45):

CSS 2d Transform
  • To rotate the element anti clockwise, you can use negative angle:

Example:

transform: rotate(-60deg);

scale() method:

This method changes the size of an element according to its width and height.

There are three types of scale() methods:

  1. scale(x, y)
  2. scaleX(n)
  3. scaleY(n)

scale(x, y):

This CSS transform method takes the parameters as width and height and changes the size of that element accordingly.

Syntax:

transform: scale(x, y);

where, x and y increment the size of the element x times and y times, respective to the width and height.

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: green;
margin: 50px 40px;
transform: scale(2,2);
transform-origin: left top;
}
</style>
</head>
<body>
<h1>CSS Transform Scale</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Before using scale():

CSS 2d Transform

Output after using scale(x, y):

CSS 2d Transform

Here, the size of the square is incremented 2 times of its width and 2 times of its height. Also the transform-origin : left top; shifts the origin to the top left of the element thus facilitating the scaling inside the screen.

scaleX(n):

Increments the size of an element using the width.

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: green;
margin: 50px 40px;
transform: scaleX(3);
transform-origin: left top;
}
</style>
</head>
<body>
<h1>CSS Transform Scale</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Output:

CSS 2d Transform

Here the square element is scaled 3 times its width using scaleX(3).

scaleY(n):

Increments the size of an element using the height.

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: green;
margin: 50px 40px;
transform: scaleY(2);
transform-origin: left top;
}
</style>
</head>
<body>
<h1>CSS Transform Scale</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Output:

CSS 2d Transform

Here the square element is scaled 2 times its height using scaleY(2).

skew() method:

This transform method skews the element along the x-axis or y-axis according to its types:

  1. skewX(n)
  2. skewY(n)

skewX(angle):

This method is used to skew the element according to its X-axis.

Syntax:

transform: skewX(angle);

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: brown;
margin: 50px 40px;
transform: skewX(30deg);
}
</style>
</head>
<body>
<h1>CSS Transform Skew</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Before using skewX():

CSS 2d Transform

Output after using skewX():

CSS 2d Transform

Here the element is skewed by 30 degrees along the x-axis using skewX(30deg) method.

skewY():

This method is used to skew the element according to its Y-axis.

Syntax:
transform: skewY(angle);

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: brown;
margin: 50px 40px;
transform: skewY(30deg);
}
</style>
</head>
<body>
<h1>CSS Transform Skew</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Output:

CSS 2d Transform

Here the element is skewed by 30 degrees along the y-axis using skewY(30deg) method.

matrix() method:

matrix() method of CSS transform allows you to combine all the four methods i.e. translate(), skew(), rotate() and scale() together.

Syntax:

transform: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY());

Example:

<!DOCTYPE html>
<html>
<head>
<title>CSS Transform</title>
<style type="text/css">
body{
font-family: cursive;
}
#square{
width: 250px;
height: 150px;
background: brown;
margin: 50px 40px;
transform: matrix(1, -0.2, 0, 1, 0 ,0);
}
</style>
</head>
<body>
<h1>CSS Transform Skew</h1>
<div id="square">Example for Transform property</div>
</body>
</html>

Output:

CSS 2d Transform

2D Transform for Cross Browser:

CSS 2D transform property can be used for all types of browsers.

You have to use following prefixes before transform property for the respective browsers:

Prefix Browser name
-moz-transform: methodName(parameters) Mozilla Firefox
-webkit-transform: methodName(parameters) Google Chrome, Safari, Android, iPhone
-o-transform: methodName(parameters) Opera browser

So in this way we have studied the 2D Transform property of CSS and its various methods to create animated web pages.


Related Topics

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

How to include CSS

How to include CSS CSS is included in HTML pages for formatting the documents based on the information inside a style sheet. We can add CSS in HTML documents in the following three ways: Inline...

5 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS Syntax

The CSS syntax contains the selector and the declaration block, which will be clearer with the following example: Selector: A CSS selector specifies the HTML tag we wish to style. A selector can be...

2 minutes read.

How to center a table in CSS

The table is used to store and arrange the data in tabular format. The data here can be of any form like text, image, links, etc. The table is made...

3 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

CSS Color

CSS Colors: The CSS color property is used for setting the HTML element’s color. The color of the fonts and the color of the background can be set-up by using...

5 minutes read.

CSS Text-align

Text-align The text-align property of CSS sets the table-cell box’s horizontal alignment or any block element. The text-align property is the same as the CSS property vertical-align but towards any horizontal...

1 minute read.