×

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 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 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 Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Font

CSS Font: CSS has font property, which makes the text more attractive. We can alter the entire look by changing font color, font size, font style, and much more. In...

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

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS Button Style

CSS button style: Using CSS properties, we can easily modify the default BUTTON and enhance its appearances using simple graphic commands. TYPES OF BUTTON STYLE: - Program to change button using CSS ...

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

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

CSS Page-break-after Attribute

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

5 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Float

CSS Float: The float property of CSS is the positioning property. This property is applied to elements to push them either to left or right. It permits for wrapping facility...

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.

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

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.