Get $500+ of the best After Effects files, video templates and music for only $20!
CSS3 Transitions And Transforms From Scratch
basix

CSS3 Transitions And Transforms From Scratch

Tutorial Details
  • Topic: CSS3 Animation
  • Difficulty: Basix
  • Estimated Completion Time: 45 mins

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 Axis

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.

<div id="axis" class="one">
<img class="object van move-right" src="images/van-to-right.png"/>
</div>

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.

.object {
	position: absolute;
}
.van {
	top: 45%;
	left: 44%;
}

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.

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

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.

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

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.

<div id="axis" class="two">
	<img class="object van move-left" src="images/van-to-left.png"/>
</div>

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?

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

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.

<div id="axis">
	<img class="object rocket move-up" src="images/rocket.png"/>
</div>

CSS

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

.rocket {
	top: 43%;
	left: 44%;
}

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.

#axis:hover .move-up {
	transform: translate(0,-350px);
	-webkit-transform: translate(0,-350px);
	-o-transform: translate(0,-350px);
	-moz-transform: translate(0,-350px);
}
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!

<div id="axis" class="four">
<img class="object coin move-down" src="images/coin.png"/>
</div>

CSS

#axis:hover .move-down {
	transform: translate(0,350px);
	-webkit-transform: translate(0,350px);
	-o-transform: translate(0,350px);
	-moz-transform: translate(0,350px);
}
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.

<div id="axis" class="five">
<img class="object plane move-ne" src="images/paper-plane.png"/>
</div>

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);

#axis:hover .move-ne {
	transform: translate(350px,-350px);
	-webkit-transform: translate(350px,-350px);
	-o-transform: translate(350px,-350px);
	-moz-transform: translate(350px,-350px);

}

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.

<div id="axis" class="six">
<img class="object pencil rotate360cw" src="images/pencil.png"/>
</div>

CSS

And we’ll rotate the object 360 degrees clockwise.

#axis:hover .rotate360cw {
	transform: rotate(360deg);
	-webkit-transform: rotate(360deg);
	-o-transform: rotate(360deg);
	-moz-transform: rotate(360deg);
}
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.

<div id="axis" class="seven">
<img class="object pencil rotate360ccw" src="images/pencil.png"/>
</div>

CSS

#axis:hover .rotate360ccw {
	transform: rotate(-360deg);
	-webkit-transform: rotate(-360deg);
	-o-transform: rotate(-360deg);
	-moz-transform: rotate(-360deg);
}
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.

<div id="axis" class="eight">
<img class="object car larger" src="images/car.png"/>
</div>

CSS

#axis:hover .larger{
	transform: scale(2);
	-webkit-transform: scale(2);
	-o-transform: scale(2);
	-moz-transform: scale(2);
}
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.

<div id="axis" class="ten">
<img class="object boomerang multiple-move" src="images/pencil.png"/>
</div>

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.

#axis:hover .multiple {
	transform: translate(350px, -350px) rotate(360deg);
	-webkit-transform: translate(350px, -350px) rotate(360deg);
	-o-transform: translate(350px, -350px) rotate(360deg);
	-moz-transform: translate(350px, -350px) rotate(360deg);
}
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 :)

Tags: Basixcss3
Add Comment

Discussion 22 Comments

  1. for-w-art says:

    Now if only IE7 would not screw up my transparent png’s…

    Pretty neat stuff though.

  2. André says:

    Hello!
    Great post. I have a suggestion for a post idea!
    What about a post called “How to setup photoshop for web design”

    So you could tell us how to configure the type fonts to be exactly like a browser would render it, prevent the half pixels etc. There are so many thing to configure and optimize, I would love to learn them.

    Thank you!

  3. Rian Ariona says:

    thanks for this Amazing post! and nice illustration! i like it :)

  4. Daniel Piechnick says:

    Looking forward to being able to a lot of this stuff in Edge.

    Daniel Piechnick

  5. dina says:

    very useful tutorial :)

  6. Leonardo says:

    Really cool I think that I will start to use css3

  7. bob marteal says:

    Great explanations and illustrations for us visual learners. Thanks for the unique, and useful, presentation.

  8. Jonas says:

    Could you use the css3 function without the need of hover?

  9. Jonas says:

    Could you use the css3 function without the need of hover?

  10. Dinesh says:

    Really really a very useful tutorial !! loved it :) thanks and keep posting such

  11. Kevin says:

    Awesome Tutorial…Never saw the animation perspective of CSS..Great

  12. datta3 says:

    nice and very intresting

  13. Long Nguyen says:

    Thanks for Amazing post! It’s very useful with me :)

  14. Tobias Jurga says:

    I really really missed tutorials like that on here! Thank you, really great how u did that and how u made all those different demo’s this could be in a “for dummies” book! great article

  15. Dimitris M. says:

    Is there a way to have a first transition and after that a second one? Not simultaneously

  16. Aayush Agarwal says:

    Hey, great post.I wanted to know as how can we trigger these transitions if we want them to run when we hover the mouse on them..Thanks..

  17. Bryan Thomas says:

    Is there a way to scale an object in only one dimension? Also is there a way to draw shapes with CSS? Just something basic like a rectangle or a circle?

  18. Roland says:

    How can I make different speed / time for effects?

    -moz-transition: translate 2s ease-in-out;
    -moz-transition:opacity 2s ease-in-out;

    not working.. thanks

  19. coder-design says:

    It is very cool tutorial. Thanks for sharing.

Add a Comment