×

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

CSS: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.

CSS Background

CSS Background The properties of CSS background are applied to describe the effects of background on the element. Here are five properties of background that affects various elements of HTML: Background-colorBackground-imageBackground-repeatBackground-attachment Background-position Background-color The color of the background...

7 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

Radial-gradient() CSS function

Radial-gradient() CSS function It defines a CSS in-built function, which produces a smooth transition among multiple colors. It is used to set any radial-gradient that is same as a background-image. This...

4 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

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...

6 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

Difference between HTML and CSS

HTML HTML stands for HyperText Markup Language and is used to create web pages and websites and web applications, which means we can create static web pages. Html is a scripting...

3 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 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 Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

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.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.