# CSS3 Transitions and Transforms From Scratch

There are some amazing examples of CSS transforms and transitions, and whilst you may be blown away by them, there's a good chance that you're also overwhelmed and a bit intimidated! This tutorial will take you back to the very basics. We're going to create some fundamental CSS3 transitional movements, step by step.

### A Quick Note on Browser Support:

Support across browsers is already pretty reasonable. Firefox 3.5, Chrome, Opera 10.5, Safari 3.1, and Internet Explorer 9.0 have you covered where transforms are concerned. IE is a little less accommodating with regard to transitions, though support is planned for IE10.

## The Axes and Grid

To help understand the movement easily we'll be working on an axis grid (which you'll probably recognize from basic math). We'll be using x and y coordinates to move our objects.

The only (crucial) difference is that on our axis the -y value is above the x axis, whilst it would ordinarily be below it. Why? Well, HTML and CSS (along with other web technologies like ActionScript) use an inverted Cartesian coordinate system because web pages start from top-left and read downwards. So now you know :)

Note: I'm going to assume that you're already familiar with HTML and CSS file structure. I'm going to skip explaining how to set up the CSS file, placing images and styling the Axis. Our focus will be on animating the objects. If you're not confident that your HTML + CSS skills are up to scratch, take a look at the new Tuts+ Premium HTML & CSS in 30 days (which is free) and you’ll learn everything you need to know.

## 1. Horizontal Movement

The first movement we'll demonstrate is "horizontal"; we'll animate the object to move to the right and to the left.

### Moving to the Right

To move an object from its initial position we use: transform: translate(x,y);, where the x value should be positive and the y value should be 0 to move the object to the right.

#### HTML

Open your favorite Text Editor and enter the following html markup, then save the file.

 1 
 2   3 


We've assigned three classes to the image:

• object: We use this class to set general rules for all the objects we will use.
• van: We're going to use different objects to demonstrate each animation, so we'll apply different classes to them as well. This way we can position each object separately.
• move-right: We'll move the object using this class, each movement will have different class.

#### CSS

Firstly, we'll position the object (our van image) to the center of the grid.

 1 .object {  2  position: absolute;  3 }  4 .van {  5  top: 45%;  6  left: 44%;  7 } 

In this example we are going to move the object 350px to the right. The syntax is transform: translate(350px,0); and the object will move when the Axis is hovered over. We therefore declare it with #axis:hover .move-right.

 1 #axis:hover .move-right{  2  transform: translate(350px,0);  3  -webkit-transform: translate(350px,0); /** Chrome & Safari **/  4  -o-transform: translate(350px,0); /** Opera **/  5  -moz-transform: translate(350px,0); /** Firefox **/  6 } 

The CSS transform property will only move the object from one point to another, it will not animate between the two states. To do this we need to add a transition property in the .object class.

 1 .object {  2  position: absolute;  3  transition: all 2s ease-in-out;  4  -webkit-transition: all 2s ease-in-out; /** Chrome & Safari **/  5  -moz-transition: all 2s ease-in-out; /** Firefox **/  6  -o-transition: all 2s ease-in-out; /** Opera **/  7 } 

This transition rule will tell the browser to animate all properties attached to the object for 2 seconds using an ease-in-out timing function (tween), without delay.

We can use 6 types of transition-timing-functions:

• linear: the transition will have constant speed from start to end.
• ease: the transition will start slowly, then get faster, before ending slowly.
• ease-in: the transition will start slowly.
• ease-out: the transition will end slowly.
• ease-in-out: the transition starts and ends slowly.
• cubic-bezier: define specific values for your own transition.
View Demo

### Moving to the Left

To move an object to the left we simply need to enter a negative value in the x coordinate, while the y coordinate should remain 0. In this example we will move the object -350px to the left.

#### HTML

Create another HTML file and enter the following markup.

 1 
 2   3 


This time we use the move-left class to set the css rule for moving the object to the left.

#### CSS

Then, we enter -350px as the x coordinate. transform: translate(-350px,0); to move the object to the left. Quite easy, right?

 1 #axis:hover .move-left {  2  transform: translate(-350px,0);  3  -webkit-transform: translate(-350px,0); /** Safari & Chrome **/  4  -o-transform: translate(-350px,0); /** Opera **/  5  -moz-transform: translate(-350px,0); /** Firefox **/  6 } 

Since we have previously set the transition rule in our .object class, we don’t need to set it again.

View Demo

## 2. Vertical Movement

Moving an object vertically is pretty easy, almost identical to moving horizontally. The only difference is that we will use the -y value to move the object upward and the y value to move downward.

### Moving Upwards

#### HTML

The HTML markup is identical to the previous two examples. However, we're replacing the object with a rocket (for illustrative purposes), and the class that we use for our upwards movement is move-up.

 1 
 2   3 


#### CSS

As with our van, we'll position the rocket in the center of the grid:

 1 .rocket {  2  top: 43%;  3  left: 44%;  4 } 

As we've mentioned before, the y coordinate should be negative, in order to move the rocket upwards. in this case we move it 350px up.

 1 #axis:hover .move-up {  2  transform: translate(0,-350px);  3  -webkit-transform: translate(0,-350px);  4  -o-transform: translate(0,-350px);  5  -moz-transform: translate(0,-350px);  6 } 
View Demo

### Moving Downwards

The method for moving an object downwards is (surprise, surprise) the opposite of moving upwards; the y coordinate value should be positive and the x coordinate should remain 0. The syntax is transform: translate(0,y);

#### HTML

In this example, we'll demonstrate downwards movement using a coin. Genius!

 1 
 2   3 


#### CSS

 1 #axis:hover .move-down {  2  transform: translate(0,350px);  3  -webkit-transform: translate(0,350px);  4  -o-transform: translate(0,350px);  5  -moz-transform: translate(0,350px);  6 } 
View Demo

## 3. Diagonal Movement

To create a diagonal transition, we'll combine values of both coordinates x and y. The syntax should be transform: translate(x,y). Depending on the direction, the value of x and y could be negative or positive.

#### HTML

And to demonstrate our diagonal movement, we'll use a paper plane.

 1 
 2   3 


#### CSS

We'll direct movement toward the north east. For the x coordinate value we enter a positive value (350px) and for the y coordinates we enter a negative value (-350px). The syntax will therefore look like this: transform: translate(350px,-350px);

 1 #axis:hover .move-ne {  2  transform: translate(350px,-350px);  3  -webkit-transform: translate(350px,-350px);  4  -o-transform: translate(350px,-350px);  5  -moz-transform: translate(350px,-350px);  6   7 } 

Feel free to experiment and direct the movement of objects along the other diagonal axes.

View Demo

## 4. Rotation

Rotational movement in CSS3 is regulated using a radial coordinate from 0° to 360°. To rotate an object simply apply the following css property: transform: rotate(ndeg); where n is the degree of rotation.

### 360° Clockwise

To rotate an object clockwise, we enter a positive value for the rotate(ndeg) property.

#### HTML

For this example we'll use a pencil to demonstrate the movement.

 1 
 2   3 


#### CSS

And we'll rotate the object 360 degrees clockwise.

 1 #axis:hover .rotate360cw {  2  transform: rotate(360deg);  3  -webkit-transform: rotate(360deg);  4  -o-transform: rotate(360deg);  5  -moz-transform: rotate(360deg);  6 } 
View Demo

### 360° Counter Clockwise

To perform counter-clockwise rotation we enter (you guessed it) a negative value.

#### HTML

We're still using the pencil as our object, but we'll change its class to .rotate360ccw.

 1 
 2   3 


#### CSS

 1 #axis:hover .rotate360ccw {  2  transform: rotate(-360deg);  3  -webkit-transform: rotate(-360deg);  4  -o-transform: rotate(-360deg);  5  -moz-transform: rotate(-360deg);  6 } 
View Demo

## 5. Scaling

Scale is an interesting feature in CSS3. By using the scale(n) or scale(x,y) property we can enlarge or shrink an object within our HTML. The object will be scaled according to n/x,y value, where the x-axis is for the width and the y-axis represents the height. For example, if we enter scale(2), the object will be scaled twice (200% larger) along both axes, from its initial dimension.

Let’s take a look at the example below.

In this example we'll increase the size of the car by 200%, giving the illusion that the car is moving closer (hopefully).

#### HTML

Again, the HTML markup has barely changed, but this time we use a car as the object.

 1 
 2   3 


#### CSS

 1 #axis:hover .larger{  2  transform: scale(2);  3  -webkit-transform: scale(2);  4  -o-transform: scale(2);  5  -moz-transform: scale(2);  6 } 
View Demo

## 6. Multiple Movements

After having played with basic movements and transforms, we'll now try to combine some of them. Let's take a look.

#### HTML

This time, we'll be using a boomerang to demonstrate the animation.

 1 
 2   3 


#### CSS

The plan is to move the boomerang diagonally, while performing rotations at the same time. In order to do this, we simply have to list the transformations, separated by spaces.

 1 #axis:hover .multiple {  2  transform: translate(350px, -350px) rotate(360deg);  3  -webkit-transform: translate(350px, -350px) rotate(360deg);  4  -o-transform: translate(350px, -350px) rotate(360deg);  5  -moz-transform: translate(350px, -350px) rotate(360deg);  6 } 
View Demo

## Conclusion

These are basic examples, and there's huge scope for developing them further! Remember: take browser support into account when using CSS3 properties, and don't go crazy with these effects - they should enhance your design, not drown it!

## More CSS3 Transition Examples

For more inspiration, take a look at some of the cool CSS3 animations and effects on Envato Market. Here are a few examples:

### 1. Social Mosaic Wall

This item lets you build your own mosaic grid with your social media for staff and more. It includes 28 social icons and a range of different grid configurations.