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

24 Best Creative Portfolio Websites in 2023

For anyone working in a digital creative field, whether design, illustration, animation, video, or a combination of…

15 Best New Fonts, September 2023

Nothing upgrades your designs like selecting the right font. It’s all too easy to fall into the trap of using the same…

Weekly Design News #1

Every Sunday we’re rounding up the best of the previous week’s stories from webdesignernews.com, and in this issue #1,…

The 20 Most Controversial Logos of All Time (Ranked)

When you hire graphic designers to create your company's logo, what do you expect? Professional designs, culturally…

LimeWire AI Studio Generative Art App

If you’re looking for the most exciting way to launch a career in AI-generated art, then you’re in the right place.

20 Best New Websites, September 2023

Are you in need of design inspiration? Are you looking for the best websites designed in 2023 to pull ideas,…

The Dangers of Deceptive Design Patterns (And How to Avoid Them)

As web designers, our role in crafting user-friendly digital landscapes is critical. We are tasked with creating user…

10 Best Ecommerce WordPress Themes in 2023 [September update]

You plan to set up shop with an online store. You know there’ll be competition. And to compete with or beat that…

5 Marketing Tools Every Designer Needs

Yes, designers do need marketing tools. From freelance graphic designers who need to land more work to designers who…

Exciting New Tools For Designers, September 2023

At the end of another summer, we are all getting ready to knuckle down for some serious work in the fall. But we want…

Elon Musk calls LinkedIn ‘Cringe’—Announces Competitor

Elon Musk recently announced his intentions to create a direct competitor to LinkedIn. Musk’s grand plan is to make his…

Everything You Need to Know to Embrace the Y2K Design Trend

The turn of the millennium was a major cultural shift, and the Y2K aesthetic emerged as a visualization of what the…