How to use the CSS3 transition property

Default avatar.
December 06, 2012
How to use the CSS3 transition property.

ThumbAlong with the introduction of CSS3 comes many new features that are available for use in creating great effects; one of the most useful is the transition property.

The transition property is an important new development in CSS. It can be used to create a dynamic change effect on a div or class using a simple structure:

transition: property duration timing-function delay;

CSS3's transition is a great way to add a little animation to sites without the large overhead of a JavaScript library like jQuery.

Demo

Before we start, you can see a demo here of the transition property in action.

Property

Firstly, in order for the transition property to work, the standard property that it will be applied to needs to be defined. Arguably the two most common properties that will be defined are width and height. To write the property standalone simply use:

transition-property: define property

Size Change

Following on, once the property has been defined then the start and end values need to be assigned. In the case of values such as width or height the property needs to be set with a start value and then an end value with some other condition.

For example, here we set the transition property to width, then the start value of width and then set the end value when the element is hovered over:

#mainheader {
transition-property:width;
width:50px;
}
#mainheader:hover {
width:75px;
}

Duration

Now that we have defined the property to transform, the start and end values, we need to define the duration of the transition. This is achieved by defining a length in either seconds or milliseconds as below:

transition-duration: duration;

Building this into the example the following code is created:

#mainheader {
transition-property:width;
transition-duration:0.5s;
width:500px;
}
#mainheader:hover {
width:750px;
}

This means that the mainheader div will expand by 25px over a duration of 5 seconds.

Timing Function

The code is sufficient to create a nice effect however we can further utilise the CSS3 transition property by using timing-function Using this property it is possible to alter the speed curve of the transition duration. The transition property is set to a linear curve by default. However, you can define ease, ease-in, ease-out, ease-in-out and even cubic-bezier to alter the speed curve. Cubic-bezier allows you to define your own values using (n,n,n,n) where n can be between 0 and 1 (for example linear would be (0,0,1,1)).

Adding in this code to our example results in:

#mainheader {
transition-property:width;
transition-timing-function:ease-in-out;
transition-duration:0.5s;
width:500px;
}
#mainheader:hover {
width:750px;
}

Delay

Furthermore, much like transition-duration, using the transition-delay property defines a pause before the transition effect begins:

transition-delay: time;

Conclusion

Finally, it is important to consider two things when using the CSS3 transition property. Firstly, most browsers in circulation at present require a browser prefix to use it (the exceptions being IE10, Opera and Firefox16+):

-moz-transition: for Firefox 15
-webkit-transition: for Chrome and Safari

(Bear in mind that IE9 and lower does not support the transition property at all.)

Secondly, although I've used long hand in the examples above for clarity, it's considered best practice to write in short form, as follows:

#mainheader {
-moz-transition: width ease-in-out 0.5s 0.1s; /* for Firefox 15 */
-webkit-transition: width ease-in-out 0.5s 0.1s; /* for Chrome and Safari */
transition: width ease-in-out 0.5s 0.1s;
width:500px;
}
#mainheader:hover {
width:750px;
}

Do use the CSS3's transition property? How does it compare to jQuery-based tweens? Let us know in the comments.

Featured image/thumbnail, motion image via Shutterstock.

David Pickett

David Pickett is a musician and web designer from the UK. Follow him on twitter.

Read Next

3 Essential Design Trends, November 2024

Touchable texture, distinct grids, and two-column designs are some of the most trending website design elements of…

20 Best New Websites, October 2024

Something we’re seeing more and more of is the ‘customizable’ site. Most often, this means a button to swap between…

Exciting New Tools for Designers, October 2024

We’ve got goodies for designers, developers, SEO-ers, content managers, and those of you who wear multiple hats. And,…

15 Best New Fonts, September 2024

Welcome to our roundup of the best new fonts we’ve found on the web in the previous four weeks. In this month’s edition…

3 Essential Design Trends, October 2024

This article is brought to you by Constantino, a renowned company offering premium and affordable website design You…

A Beginner’s Guide to Using BlueSky for Business Success

In today’s fast-paced digital world, businesses are always on the lookout for new ways to connect with their audience.…

The Importance of Title Tags: Tips and Tricks to Optimize for SEO

When it comes to on-page SEO, there’s one element that plays a pivotal role in both search engine rankings and user…

20 Best New Websites, September 2024

We have a mixed bag for you with both minimalist and maximalist designs, and single pagers alongside much bigger, but…

Exciting New Tools for Designers, September 2024

This time around we are aiming to simplify life, with some light and fast analytics, an all-in-one productivity…

3 Essential Design Trends, September 2024

September's web design trends have a fun, fall feeling ... and we love it. See what's trending in website design this…

Crafting Personalized Experiences with AI

Picture this: You open Netflix, and it’s like the platform just knows what you’re in the mood for. Or maybe you’re…

15 Best New Fonts, August 2024

Welcome to August’s roundup of the best fonts we’ve found over the last few weeks. 2024’s trend for flowing curves and…