CSS3 transitions
CSS3 transitions
In CSS3, we can change from one style to another to add an effect without using Flash animation or JavaScript. Move the mouse over the elements below:
Move the mouse over the elements below:
CSS3
Transition
Browser support
The numbers in the table indicate the first browser version that supports this property.
A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.
| property | |||||
|---|---|---|---|---|---|
| transition | 26.0 | ||||
| 4.0 -webkit- | 10.0 | 16.0 | |||
| 4.0 -moz- | 6.1 | ||||
| 3.1 -webkit- | 12.1 | ||||
| 10.5 -o- | |||||
| transition-delay | 26.0 | ||||
| 4.0 -webkit- | 10.0 | 16.0 | |||
| 4.0 -moz- | 6.1 | ||||
| 3.1 -webkit- | 12.1 | ||||
| 10.5 -o- | |||||
| transition-duration | 26.0 | ||||
| 4.0 -webkit- | 10.0 | 16.0 | |||
| 4.0 -moz- | 6.1 | ||||
| 3.1 -webkit- | 12.1 | ||||
| 10.5 -o- | |||||
| transition-property | 26.0 | ||||
| 4.0 -webkit- | 10.0 | 16.0 | |||
| 4.0 -moz- | 6.1 | ||||
| 3.1 -webkit- | 12.1 | ||||
| 10.5 -o- | |||||
| transition-timing-function | 26.0 | ||||
| 4.0 -webkit- | 10.0 | 16.0 | |||
| 4.0 -moz- | 6.1 | ||||
| 3.1 -webkit- | 12.1 | ||||
| 10.5 -o- |
How Does It Work?
A CSS3 transition is the effect of an element gradually changing from one style to another.
To achieve this, two things must be specified:
- Specify the CSS property to which the effect should be added
- Specify the duration of the effect.
Example
A transition effect applied to the width property, with a duration of 2 seconds:
div
{
transition:
width
2
s
; -
webkit-transition:
width
2
s
;
/*
Safari
*/
}
Note: If the duration is not specified, the transition will have no effect, because the default value is 0.
The effect occurs when the value of the specified CSS property changes. A typical CSS property change is when the user places the mouse over an element:
Example
Specifies what happens when the mouse pointer hovers (:hover) over the
div
:hover
{
width:
300
px
;
}
Try it »
Note: When the mouse cursor moves over the element, it gradually changes its original style
Multiple Changes
To add transition effects for multiple styles, the added properties are separated by commas:
Example
Width, height, and transform effects are added:
div
{
transition:
width
2
s
,
height
2
s
,
transform
2
s
; -
webkit-transition:
width
2
s
,
height
2
s
, -
webkit-transform
2
s
;
}
Try it »
Transition Properties
The following table lists all the transition properties:
| property | Description | CSS |
|---|---|---|
| transition | A shorthand property for setting the four transition properties in one property. | 3 |
| transition-property | Specifies the name of the CSS property to which the transition is applied. | 3 |
| transition-duration | Defines how long the transition effect takes. The default is 0. | 3 |
| transition-timing-function | Specifies the timing curve of the transition effect. The default is "ease". | 3 |
| transition-delay | Specifies when the transition effect starts. The default is 0. | 3 |
The following two examples set all the transition properties:
Example
Using all transition properties in one example:
div
{
transition-property:
width
;
transition-duration:
1
s
;
transition-timing-function:
linear
;
transition-delay:
2
s
;
/*
Safari
*/
webkit-transition-property:
width
; -
webkit-transition-duration:
1
s
; -
webkit-transition-timing-function:
linear
; -
webkit-transition-delay:
2
s
;
}
Try it »
Example
The same transition effect as the example above, but using the shorthand transition property:
div
{
transition:
width
1
s
linear
2
s
;
/*
Safari
*/
webkit-transition:
width
1
s
linear
2
s
;
}
Try it »