This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.

Smartphone icons created by Freepik - Flaticon

13.3 CSS Transitions

CSS transitions allows you to change property values smoothly, over a given duration. Mouse over the element below to see a CSS transition effect:

CSS transition

Notice that when the cursor mouses out of the element, it will gradually change back to its original style.

In this chapter you will learn about the following properties:

Browser Support for Transitions

The numbers in the table specify the first browser version that fully supports the property.

Property Chrome Windows Edge Firefox Safari Opera
transition 26.0 10.0 16.0 6.1 12.1
transition-delay 26.0 10.0 16.0 6.1 12.1
transition-duration 26.0 10.0 16.0 6.1 12.1
transition-property 26.0 10.0 16.0 6.1 12.1
transition-timing-function 26.0 10.0 16.0 6.1 12.1

How to Use CSS Transitions?

To create a transition effect, you must specify two things:

Note: If the duration part is not specified, the transition will have no effect, because the default value is 0.

The following example shows a 100px * 100px red <div> element. The <div> element has also specified a transition effect for the width property, with a duration of 2 seconds:

Example 1a: The transition property
div {
  width: 100px;
  height: 100px;
  background: red;
  transition: width 2s;
}

The transition effect will start when the specified CSS property (width) changes value. Now, let us specify a new value for the width property when a user mouses over the <div> element:

Example 1b: The transition property
div:hover {
  width: 300px;
}

Notice that when the cursor mouses out of the element, it will gradually change back to its original style.

Change Several Property Values

The following example adds a transition effect for both the width and height property, with a duration of 2 seconds for the width and 4 seconds for the height:

Example 2: The transition property (multiple transitions)
div {
  transition: width 2s, height 4s;
}

Specify the Speed Curve of the Transition

The transition-timing-function property specifies the speed curve of the transition effect. The transition-timing-function property can have the following values:

The following example shows the some of the different speed curves that can be used:

Example 3: The transition-timing-function property
#div1 {transition-timing-function: linear;}
#div2 {transition-timing-function: ease;}
#div3 {transition-timing-function: ease-in;}
#div4 {transition-timing-function: ease-out;}
#div5 {transition-timing-function: ease-in-out;}

Delay the Transition Effect

The transition-delay property specifies a delay (in seconds) for the transition effect. The following example has a 1 second delay before starting:

Example 4: The transition-delay property
div {
  transition-delay: 1s;
}

Transition + Transformation

The following example adds a transition effect to the transformation:

Example 5: Transition + transform
div {
  transition: width 2s, height 2s, transform 2s;
}

More Transition Examples

The CSS transition properties can be specified one by one, like this:

Example 6: transition properties
div {
  transition-property: width;
  transition-duration: 2s;
  transition-timing-function: linear;
  transition-delay: 1s;
}

or by using the shorthand property transition:

Example 7: The transition shorthand property
div {
  transition: width 2s linear 1s;
}

CSS Transition Properties

The following table lists all the CSS transition properties:

Property Description
transition A shorthand property for setting the four transition properties into a single property
transition-delay Specifies a delay (in seconds) for the transition effect
transition-duration Specifies how many seconds or milliseconds a transition effect takes to complete
transition-property Specifies the name of the CSS property the transition effect is for
transition-timing-function Specifies the speed curve of the transition effect