CSS Transitions
CSS Transitions facilitate a sense of motion by replicating a natural change from one state to another. When you drive your car it does not go from zero to 60 km/hr in a flick, it is a gradual change, the same concept is applied using transition, rather than changing a property value ina flick we apply a sense of gradual change.
CSS transitions offer control over how a property or properties change by transitioning the change between beginning state and end state. Developers, designers and authors can control the duration, timing and when a transition begins. The list of properties that can be transitioned is extensive but keep in mind that not all properties can be animated.
Transition Properties
Transitions are controlled by four properties: transition-property
, transition-duration
, transition-delay
, transition-timing-function
.
.box {
transition-property: background;
transition-duration: 0.3s;
transition-timing-function: ease-out;
transition-delay: 0.5s;
}
transition-property
Specifies the name or names of the CSS properties to which transitions should be applied. Only properties listed here are animated during transitions; changes to all other properties occur instantaneously as usual.
NOTE
The all
keyword can be used to apply the transition to all properties.
Default value: all
transition-duration
Specifies the duration over which transitions should occur. You can specify a single duration that applies to all properties during the transition, or multiple values to allow each property to transition over a different period of time.
Default value: 0s
transition-timing-function
Specifies the timing function to use during the transition. Timing functions are used to determine the rate of the transition over the duration. For example, does the transition happen quickly at the beginning and slow down at the end.
Default value: ease
transition-delay
Defines how long to wait between the time a property is changed and the transition actually begins.
Default value: 0s
transition
The transition
property can be used as a shorthand for the transition properties. Order does not matter, but the first time value given will be applied to the transition-duration
.
.box {
/* transition shorthand */
transition: background 0.3s ease-out 0.5s;
}
Compound Transitions
While it is possible to use the all
to add a transition affect to multiple properties, it is inadequate if those properties need to have different duration or delay times. It might be tempting to add a second transition
property as shown below.
.box {
/* will not work as expected */
transition: width 0.3s 0.5s;
transition: height 0.5s 1s;
}
Note
Instead of add both transitions, the first will be overwritten by the second. Fortunately there is a way to add multiple transitions to the same transition
property, by separating each transition statement with a comma.
.box {
/* the right way */
transition: width 0.3s 0.5s, height 0.5s 1s;
}
Examples
Further Review
Review the following material and be sure to download the lesson file to begin exploring transitions.
- Read Using CSS transitions - CSS: Cascading Style Sheets | MDN
- Watch All About CSS Transitions - YouTube
- Watch CSS Transitions and Card Effects - YouTube
- Watch Apply CSS transitions | LinkedIn Learning, formerly Lynda.com
- Watch Manage transition timing | LinkedIn Learning, formerly Lynda.com
- Watch Steps and delays | LinkedIn Learning, formerly Lynda.com
- Read Add Touch to Your Site | Web Fundamentals | Google Developers
See Also
- This Ain’t Disney: A practical guide to CSS transitions and animations
- Creating Elegant Navigation Using CSS3 Transitions | Pluralsight
- Using CSS3 transitions: A comprehensive guide | Adobe Developer Connection
- Smart Transitions In User Experience Design — Smashing Magazine
- Navigation transitions - Material Design
References
- transition - CSS: Cascading Style Sheets | MDN
- transition-delay - CSS: Cascading Style Sheets | MDN
- transition-duration - CSS: Cascading Style Sheets | MDN
- transition-timing-function - CSS: Cascading Style Sheets | MDN
- transition-property - CSS: Cascading Style Sheets | MDN
- timing-function - CSS: Cascading Style Sheets | MDN
- Transitions - CSS Reference
- Animatable CSS properties - CSS: Cascading Style Sheets | MDN
Reach Ahead
- CSS: Transforms and Transitions - Welcome | LinkedIn Learning, formerly Lynda.com
- Animations | Web Fundamentals | Google Developers
This YouTube video was created by Steve Griffith.