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-bezier
and 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
all
option, 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.
Yeah I was just reading https://developer.mozilla.org/en/CSS/-moz-transit… when i saw ur tweet 🙂 Very useful post 10x
Get your programming out of my CSS. 😛
Okay, I guess it's not all bad.
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.
Transition on transformation doesn't work yet with Firefox (bug 531344)
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. 😉
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.
Playing around on http://css3please.com indicates that <code> -moz-transition: all 0.3s ease-out;</code> 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.
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.
@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! 🙁
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.
@Robert
Oh, that's exactly what I do. The IE views look ugly, and that's what IE-users deserve! 😛
Good, good. 🙂
[…] Using CSS3 Transitions to create rich effects […]
[…] 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 […]
[…] 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 […]
[…] Using CSS3 Transitions to create rich effects – Robert’s talk Så här skapar du css3 transitions. (taggar: css3 transition animation reference ) […]
[…] recently wrote about CSS3 Transitions and the next step for that is sort of CSS Transitions on steroids: CSS3 Animations (CSS Animations […]
grate
Great tutorial 🙂
[…] Using CSS3 Transitions to create rich effects – Robert’s talk. […]
[…] could make three isometric squares stack on top of one another, like Alex’s graphic. (I found this article particularly helpful in its clarity and directness.) Within about 30 minutes I had a working […]
[…] Using CSS3 Transitions to create rich effects – Robert's talk???????????????????????? […]
[…] 12. ???????? ???????? ????????? ?????? ??? ?????? ????. […]
[…] Uso de transiciones CSS3 para crear efectos enriquecidos […]
[…] Acceder al Tutorial: Tutorial CSS 3 – Usando CSS 3 para crear animaciones y efectos […]
[…] Using CSS3 transitions to create rich effects • How nth child works • The CSS3 target pseudo class • Stay on […]
How is it now with Firefox? Is it fixed or still experiencing the problem? Thanx, much appreciated your post.
Nicole,
All the up-to-date information on support in Firefox is available on MDN.
Thank you, Robert! That’s great!