CSS Pseudo Classes
Transitions
The CSS property transition can be used to control the animation
speed of modified CSS property values.
In the pseudo-classes lesson lesson we used CSS to modify property values based on some external factors. When we, for example, hovered over an element the background color instantly changed to another color. With CSS transitions we can make the change take place over a period of time.
Not all CSS properties can be animated, and some may have
unpredictable behavior if any of the start or end states have values
set to auto. A full list of animatable CSS
properties
can be found on the MDN web docs site.
Notation
It is recommended to use the shorthand notation when dealing with CSS transitions as individual transition properties can be hard to debug.
The shorthand notation consists of four parts:
- transition-property: the CSS property to which a transition should be applied
- transition-duration: time the transition should take to reach the end state
- transition-timing-fuction: the function responsible for calculating intermediate property values
- transition-delay: time to wait before starting the transition animation
transition-duration is the only mandatory property that needs to be
specified. transition-property defaults to all,
transition-timing-function defaults to ease and transition-delay
defaults to 0.
Read more on MDN
Using CSS transitions
Code example: background-color
div {transition: background-color 0.5s;background-color: blue;font-size: 1rem;color: white;width: 32rem;height: 32rem;}div:hover {font-size: 2rem;background-color: red;}
produces (try hovering over the blue square below)
ABC
Notice how the background color changes over 500ms, but the font size
changes instantly. This is because we used the shorthand notation
where we limited the transition property to background-color.
Code example: all
div {transition: all 0.5s;background-color: blue;font-size: 1rem;color: white;width: 32rem;height: 32rem;}div:hover {font-size: 2rem;background-color: red;}
produces (try hovering over the blue square below)
ABC
The only change made from the previous example is the
transition-property value which is now sat to all. Notice how the
font size also animates.
Code example: timing function (ease)
div {transition: all 0.5s ease;background-color: blue;font-size: 1rem;color: white;width: 32rem;height: 32rem;}div:hover {font-size: 2rem;width: 64rem;background-color: red;}
produces (try hovering over the blue square below)
ABC
Code example: timing function (custom)
div {transition: all 0.5s cubic-bezier(.29, 1.01, 1, -0.68);background-color: blue;font-size: 1rem;color: white;width: 32rem;height: 32rem;}div:hover {font-size: 2rem;width: 64rem;background-color: red;}
produces (try hovering over the blue square below)
ABC
Test yourself
Description
Add a declaration that modifies the border radius of the .inner
element on hover to
0 2em 0 2em
and change the transition timing function of the .inner element to
cubic-bezier(0.29, 0.5, 0.13, -0.68)
All relevant rules have been defined for you. Add declarations where appropriate.
Tests
- 1
Your
.innerelement should have a border radius of0 2em 0 2emwhen hovered. - 2
Your
.innerelement should have the transition propertyall. - 3
Your
.innerelement should have the transition duration0.5s. - 4
Your
.innerelement should have the transition timing functioncubic-bezier(0.29, 0.5, 0.13, -0.68). - 5
Your
.innerelement should have a border radius of2em 0 2em 0when not hovered.