Using CSS3 Transitions to create rich effects
There has been discussions about allowing CSS to help developers create smooth transitions of CSS properties for elements, and it’s something being specified in CSS3 in W3C CSS Transitions Module Level 3. Here I’m going to show you how to implement it in Firefox, Google Chrome, Safari & Opera.
The basics
The basics relies on a few CSS properties:
- transition-property
- Which CSS property to animate, e.g.
background-color. - transition-duration
- The duration of the transition, specified in seconds, e.g.
2s - transition-delay
- Delay before the transition should start, specified in seconds, e.g. 1s
- transition-timing-function
- The function you want to use for the appearance/easing of the transition. Five predefined values are offered: ease (the default value), linear, ease-in, ease-out and ease-in-out. You can also use
cubic-bezierand define it’s values, such ascubic-bezier(0.2, 0.4, 0.7, 0.8)
First simple example
So, let’s create an element with a certain CSS transition applied to it and then use the :hover pseudo-class to change one of the CSS properties we want a transition for. Here we will have a transition for the element’s opacity, going from the value 0.3 to 1.0, after 1 second of being hovered:
<style>
#hello {
opacity: 0.3;
/* Firefox */
-moz-transition-property: opacity;
-moz-transition-duration: 2s;
-moz-transition-delay: 1s;
/* WebKit */
-webkit-transition-property: opacity;
-webkit-transition-duration: 2s;
-webkit-transition-delay: 1s;
/* Opera */
-o-transition-property: opacity;
-o-transition-duration: 2s;
-o-transition-delay: 1s;
/* Standard */
transition-property: opacity;
transition-duration: 2s;
transition-delay: 1s;
}
#hello:hover {
opacity: 1;
}
</style>
<p id="hello">Hello, CSS lover!</p>
Note the syntax with various web browser prefixes to make it work in all of them. As of this writing, none of them supports it without the prefix, but you should still have it in there for future reference.
Using shorthand syntax
Just as with most CSS properties, naturally CSS Transitions offers a shorthand syntax as well. The CSS for the above example, with shorthand syntax, would look like this:
<style>
/* Shorthand version */
#hello {
opacity: 0.3;
/* Firefox */
-moz-transition: opacity 2s ease 1s;
/* WebKit */
-webkit-transition: opacity 2s ease 1s;
/* Opera */
-o-transition: opacity 2s ease 1s;
/* Standard */
transition: opacity 2s ease 1s;
}
</style>
Specifying multiple CSS Transitions
It is very simple to use multiple transition options for a CSS Transition, just specify each comma-separated. In this example, both the opacity and height property will be transitioned:
<style>
/* Shorthand version */
#hello {
height: 20px;
opacity: 0.3;
/* Firefox */
-moz-transition: height 1s ease-out, opacity 1s ease;
/* WebKit */
-webkit-transition: height 1s ease-out, opacity 1s ease;
/* Opera */
-o-transition: height 1s ease-out, opacity 1s ease;
/* Standard */
transition: height 1s ease-out, opacity 1s ease;
}
#hello:hover {
height: 40px;
opacity: 1;
}
</style>
You can see this example live in the CSS3 Transitions example.
Using CSS Transitions and CSS Transforms
CSS Transforms offers you the possibility to scale, rotate, reposition and skew an element through CSS. We can now combine this with CSS Transitions to create a nice example that will both scale and rotate an element when hovered (it utilizes the all keyword for the transition property value, which at this time is not available in Firefox):
<style>
/* Shorthand version */
/* Shorthand version */
.menu-item {
opacity: 0.3;
/* Firefox */
-moz-transition: all 1s ease;
/* WebKit */
-webkit-transition: all 1s ease;
/* Opera */
-o-transition: all 1s ease;
/* Standard */
transition: all 1s ease;
}
.menu-item:hover {
opacity: 1;
/* Firefox */
-moz-transform: scale(2) rotate(30deg) translate(50px);
/* WebKit */
-webkit-transform: scale(1.2) rotate(30deg) translate(50px);
/* Opera */
-o-transform: scale(2) rotate(30deg) translate(50px);
/* Standard */
transform: scale(2) rotate(30deg) translate(50px);
}
</style>
<a href="/" class="menu-item">Menu item 1</a>
<a href="/" class="menu-item">Menu item 2</a>
<a href="/" class="menu-item">Menu item 3</a>
This example can be viewed/tested in the CSS Transitions with rotation example.
Web browser support
CSS Transitions work in these web browsers:
- Firefox 3.7 (upcoming this spring/summer – nightly builds available at http://nightly.mozilla.org/)
- Google Chrome 4.0+
- Safari 3.1+
- Opera 10.5+
Not available in any version of Internet Explorer, and no indication so far that it will be in IE9.
Thoughts
From testing this, I had a few thoughts:
- Firefox lacks support for the
alloption, and also works a bit flimsy (test the demo for rotation and you will see). - WebKit-based web browsers (Safari and Google Chrome) offer the most smooth animations.
- Opera seems to have issues with the font when something is rotated.

24 Comments/Reactions
April 27th, 2010 at 17:28
Yeah I was just reading https://developer.mozilla.org/en/CSS/-moz-transition when i saw ur tweet
Very useful post 10x
April 27th, 2010 at 17:33
Get your programming out of my CSS.
Okay, I guess it’s not all bad.
April 27th, 2010 at 17:43
I’d prefer adding this to standard JavaScript (outside frameworks like JQuery and DOMAssistant), as opposed to mess up CSS. This is behavioural, not presentation.
April 27th, 2010 at 18:01
Transition on transformation doesn’t work yet with Firefox (bug 531344)
April 27th, 2010 at 18:49
I’m sick & tired of using js for simple presentational purposes like rotating some element or something. Why boder with scripts when you can use couple of css properties. And it would be great when browser issues get solved.
April 27th, 2010 at 20:44
Dobromir,
Thanks, glad you like it!
mdmadph, Adrian,
Yeah, I’m kind of on the fence about this too, and many people have discussed and voided their opinions over this. However, since this is reality now in all web browsers but one, I guess we have to adapt and use it in the best manner possible.
paul,
Cool, thanks! Hopefully the flimsiness will go away to, i.e. flickering when rotating an object etc depending on what it believes to be the bounding box.
Dobromir,
Well, yes, many people think so too.
I think CSS Transitions are good in that way, it’s very simple and easy to read.
April 28th, 2010 at 0:05
Playing around on http://css3please.com indicates that
-moz-transition: all 0.3s ease-out;works fine with changes to -moz-border-radius, background-color, color, border, padding, and probably lots more.Also worth noting is that @font-face’d text usually gets totally messed with a transition.
April 28th, 2010 at 9:05
Paul,
It seems so – it’s rather that Firefox can’t handle CSS Transitions for any CSS Transform property.
Interesting about WebKit and fonts. My experience in Firefox 3.7 nightly is that it had problems with font in general.
April 28th, 2010 at 18:48
@Robert
Ah, but the browser that’s left it out is, and will continue to be, the most widely used one of the bunch.
So, I have to leave out features like this from my development!
April 28th, 2010 at 20:15
mdmadph,
With all respect, that’s where I disagree. Just use progressive enhancement and offer some nice CSS Transition to web browsers that support it, and just something more bland for the others.
April 28th, 2010 at 20:42
@Robert
Oh, that’s exactly what I do. The IE views look ugly, and that’s what IE-users deserve!
April 28th, 2010 at 20:44
Good, good.
April 29th, 2010 at 11:00
[...] Using CSS3 Transitions to create rich effects [...]
April 30th, 2010 at 10:51
[...] other day when I wrote Using CSS3 Transitions To Create Rich Effects I was thinking of nice ways to apply this. One of the things that came up was creating the Mac OS X [...]
May 2nd, 2010 at 8:44
[...] other day when I wrote Using CSS3 Transitions To Create Rich Effects I was thinking of nice ways to apply this. One of the things that came up was creating the [...]
May 2nd, 2010 at 20:14
[...] Using CSS3 Transitions to create rich effects – Robert’s talk Så här skapar du css3 transitions. (taggar: css3 transition animation reference ) [...]
May 6th, 2010 at 15:31
[...] recently wrote about CSS3 Transitions and the next step for that is sort of CSS Transitions on steroids: CSS3 Animations (CSS Animations [...]
May 30th, 2010 at 17:53
grate
Write a comment
Twitter reactions
April 27th, 2010 at 17:05
Using CSS Transitions To Create Rich Effects: http://robertnyman.com/2010/04/27/using-css3-transitions-to-create-rich-effects/
This comment was originally posted on Twitter
April 27th, 2010 at 17:25
RT @robertnyman: Using CSS Transitions To Create Rich Effects: http://robertnyman.com/2010/04/27/using-css3-transitions-to-create-rich-effects/
This comment was originally posted on Twitter
April 27th, 2010 at 18:19
Using CSS3 Transitions To Create Rich Effects – http://bit.ly/aJiX1w
This comment was originally posted on Twitter
April 28th, 2010 at 7:32
RT @robertnyman: Using CSS Transitions To Create Rich Effects: http://robertnyman.com/2010/04/27/using-css3-transitions-to-create-rich-effects/
This comment was originally posted on Twitter
April 28th, 2010 at 23:41
Using #CSS3 transitions to create rich effects http://dld.bz/b6xQ
This comment was originally posted on Twitter
May 2nd, 2010 at 14:34
Using CSS3 Transitions to create rich effects http://goo.gl/hE72
This comment was originally posted on Twitter