CSS3 Transition Property Basics

Share this article

In the second part in this series about CSS3 transitions, we’ll take a detailed look at the properties. But first, we need to know what can and cannot be animated…

Which CSS Properties Can Be Animated?

In general, a transition can be applied to any CSS property which has a discrete value which changes between the start and end states. Typically, this includes layout properties (width, height, padding, margins, borders), positional properties (left, right, top, bottom), transformations, font sizes, colors, background colors and opacities…
  • width, e.g. 10em to 200px
  • padding, e.g. 0px to 10px
  • color, e.g. #F00 to #00F
  • top, e.g. 0px to 300px
  • border-radius, e.g. 10px to 3px
  • transform, e.g. rotate(0deg) to rotate(90deg)
You can also apply transitions to color keywords such as maroon, fuchsia and teal since these are effectively translated to RGB values.

Which CSS Properties CANNOT Be Animated?

Here’s where it can get a little tricky. If you’re like me, the first transition you tried was something like this:
#container p
{
	display: none;
	transition: all 3s ease;
}

#container:hover p
{
	display: block;
}
Rather than nicely fading the element in, you’ll see an abrupt transition like you did in CSS2.1. The reason: it’s impossible to for the browser to determine the in-between states. display:none; removes a block from the page as if it were never there. A block cannot be partially displayed; it’s either there or it’s not. The same is true for visibility; you can’t expect a block to be half hidden which, by definition, would be visible! Fortunately, you can use opacity for fading effects instead. The next issue: you cannot rely on the ‘auto’ dimension, e.g.
#container p
{
	height: 0px;
	transition: all 3s ease;
}

#container:hover p
{
	height: auto;
}
‘auto’ is not a discrete dimension so the browser cannot calculate points between zero and an indeterminate value. However, there is a clever work-around; use max-height and set it to a value greater than the real height of the box. Similarly, you cannot switch properties to ‘auto’ to handle movement. This example would also fail:
#container p
{
	left: 0px;
	right: auto;
	transition: all 3s ease;
}

#container:hover p
{
	left: auto;
	right: 0px;
}
Finally, don’t expect a background-image to magically morph from one photo to another. There’s a possibility it’ll be implemented at a future date and some vendors have had limited success with CSS3 gradients, but it’s probably best avoided for now.

transition-property

The transition-property property declares which CSS properties will have a transition applied, e.g.
#container p.one
{
	transition-property: color;
}

#container p.two
{
	transition-property: width;
}

#container p.three
{
	transition-property: color, width;
}
If you’re happy for everything to change, use a value of all. You can also switch off transitions using a value of none.

transition-duration

transition-duration specifies an animation period in seconds (s) or milliseconds (ms) — one-thousandths of a second. The following durations are identical:
#container p.one
{
	transition-duration: 3s;
}

#container p.two
{
	transition-duration: 3000ms;
}

transition-timing-function

The transition-timing-function determines how the animation varies in speed throughout the duration. There are a number of possible values:
  • ease — the default; the animation starts slowly then accelerates quickly. At the mid-point, it decelerates and slows to a stop.
  • ease-in-out — similar to ease, but with subtler acceleration and deceleration.
  • ease-in — starts slowly, but accelerates and stops abruptly.
  • ease-out — starts quickly, but decelerates to a stop.
  • linear — constant speed throughout the animation; often best used for color or opacity changes.
Finally, we have cubic-bezier which allows you to define your own weird and wonderful timing function. It’s a little complex so we’ll return to it in the next part of this series.

transition-delay

Finally, transition-delay specifies the period in seconds (s) or milliseconds (ms) before a transition will start. The following are identical:
#container p.one
{
	transition-delay: 0.5s;
}

#container p.two
{
	transition-delay: 500ms;
}

CSS Shorthand Notation

Let’s look at a full transition declaration:
#container p
{
	transition-property: all;
	transition-duration: 3s;
	transition-timing-function: ease-in-out;
	transition-delay: 0.5s;
}
And don’t forget you’ll also need the -webkit-prefixed equivalents. Fortunately, you can save your fingers and sanity using a single transition declaration which accepts the property, duration, timing-function and delay in that order, e.g.
#element
{
	/* starting state styles */
	color: #F00;
	-webkit-transition: all 3s ease-in-out 0.5s;
	transition: all 3s ease-in-out 0.5s;
}

#element:hover
{
	/* ending state styles */
	color: #00F;
}
View an example page using this transition…

Selecting a Selector

In all these examples I have defined the CSS transition within the standard (non-hovered) CSS selector. However, this will cascade to the :hover selector. In other words, it’s identical to:
#element, 
#element:hover
{
	/* starting and ending state styles */
	-webkit-transition: all 3s ease-in-out 0.5s;
	transition: all 3s ease-in-out 0.5s;
}
The same transition will therefore occur in both directions: start to end state and end to start state. In the next part of this series, we’ll examine the cubic-bezier transition timing function in more detail.

Frequently Asked Questions about CSS3 Transition Properties

What is the CSS3 transition property and how does it work?

The CSS3 transition property is a shorthand property for setting the four transition properties into a single line: transition-property, transition-duration, transition-timing-function, and transition-delay. It allows elements to change values over a specified duration, animating the property changes, rather than having them occur immediately. This property can be used to create smooth, gradual transitions between defined CSS values, enhancing the user experience on a website.

How can I specify multiple properties for transition in CSS3?

To specify multiple properties for transition, you can list them out separated by commas. For example, transition: margin 2s, color 2s, width 2s;. This will apply the transition to the margin, color, and width properties of the element.

What is the difference between transition-property and transition in CSS3?

The transition-property is a sub-property of the shorthand transition property. It specifies the name of the CSS property to which the transition effect should be applied. On the other hand, the transition property is a shorthand property for setting all the individual transition properties at once in a single declaration.

How can I use the transition-timing-function property in CSS3?

The transition-timing-function property in CSS3 is used to specify the speed curve of the transition effect. It can take the following values: ease, linear, ease-in, ease-out, ease-in-out, and cubic-bezier(n,n,n,n). Each of these values defines a different type of speed curve for the transition, allowing you to customize the pacing of the transition effect.

What is the purpose of the transition-delay property in CSS3?

The transition-delay property in CSS3 is used to specify when the transition effect will start. It’s defined in seconds (s) or milliseconds (ms). A positive value will delay the start of the transition effect for the specified time, while a negative value will begin the transition immediately, but part way through the transition process.

Can I use CSS3 transition properties with all CSS properties?

No, CSS3 transition properties can’t be used with all CSS properties. They can only be used with properties that have an identifiable halfway point. These include most of the properties that you can assign a numeric value, like width, height, padding, margin, opacity, color properties, etc.

Are CSS3 transition properties supported in all browsers?

Most modern browsers support CSS3 transition properties, including Chrome, Firefox, Safari, Opera, and IE 10 and above. However, for older versions of these browsers, you may need to use vendor prefixes to ensure compatibility.

How can I create a bounce effect with CSS3 transition properties?

Creating a bounce effect with CSS3 transition properties involves using the @keyframes rule to create the bouncing animation and then applying it to an element. The @keyframes rule specifies the animation code, and the animation property is used to bind the animation to the element.

Can I control the direction of the transition effect in CSS3?

No, CSS3 transition properties do not provide a direct way to control the direction of the transition effect. However, you can simulate this by using the transform property in conjunction with transitions to create the illusion of direction.

What is the difference between CSS3 transitions and animations?

CSS3 transitions and animations both allow you to create smooth, gradual changes to an element’s style. The main difference is that transitions require an event to occur before they start, such as a user hovering over an element, while animations can run independently of user interaction and have more control over their behavior with the use of keyframes.

Craig BucklerCraig Buckler
View Author

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.

animationCSS3HTML5 Dev CenterHTML5 Tutorials & Articlestransitions
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week