Mouse over the element below to observe a CSS transition effect:
This chapter covers the following CSS properties:
The versions listed in the table indicate the initial browser versions that offer complete support for the property.
To create a transition effect, you need to define:
Keep in mind: If the duration is omitted, the transition won’t occur, as the default duration is 0.
Below is an illustration of a red <div> element measuring 100px by 100px. Additionally, the <div> has been configured to exhibit a transition effect on its width property over a span of 2 seconds.
Example
div { |
The transition effect initiates upon a change in the specified CSS property (width).
Next, we’ll define a new value for the width property when a user hovers over the <div> element:
Example
div:hover { width: 300px; } |
Upon the cursor moving out of the element, it will smoothly transition back to its original style.
In the following example, a transition effect is applied to both the width and height properties. The transition duration is set to 2 seconds for the width and 4 seconds for the height.
Example
div { transition: width 2s, height 4s; } |
The transition-timing-function property determines the pace curve of the transition effect.
It can take the following values:
Below is an example illustrating different speed curves that can be applied.
Example
#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;} |
The transition-delay property introduces a delay (in seconds) before the transition effect begins.
In the example below, there’s a 1-second delay before the transition initiates:
Example
div { |
The subsequent example introduces a transition effect to the transformation.
Example
div { transition: width 2s, height 2s, transform 2s; } |
The CSS transition properties can be individually specified, as demonstrated below:
Example
div { transition-property: width; transition-duration: 2s; transition-timing-function: linear; transition-delay: 1s; } |
Alternatively, you can use the shorthand property “transition” to specify multiple transition properties at once:
Example
div { transition: width 2s linear 1s; } |