In this tutorial, weâre taking a look at how to animate components using CSS transitions!
A transition occurs when we tell a CSS property value to change, over a specified period of time.
We do this with the transition
property, which is a shorthand of the following properties:
transition-property
transition-duration
transition-timing-function
transition-delay
.element {
transition-property: property;
transition-duration: duration;
transition-timing-function: timing-function;
transition-delay: delay;
}
This is equivalent to:
.element {
transition: property duration timing-function delay;
}
Transition Properties
transition-property
is the property you wish to transition.
transition-duration
is the duration of the transition. The value is specified in seconds (eg. 5s
for 5 seconds).
transition-timing-function
is how the transition occurs. Weâll look at this later.
transition-delay
is how long you want to wait before starting the duration. Again, the value is specified in seconds (eg. 5s
for 5 seconds).
Activating transitions
You can activate a CSS transition with a pseudo-class like :hover
(when a mouse hovers over the element), :focus
(when a user tabs onto or clicks an input element), or :active
(when user clicks the element).
Letâs see an example:
.button {
background-color: pink;
transition: background-color 2s ease-out;
}
.button:hover {
background-color: yellow;
}
What is the transition-timing-function?
The transition-timing-function
dictates how a transition occurs. All transitions are set to linear
by default, meaning the property changes evenly throughout the transition.
For example:
.element {
transition-property: transform;
transition-duration: 1s;
transition-timing-function: linear;
}
Which is the same as:
.element {
transition: transform 1s linear;
}
There are a number of values we can use for our transition-timing-function
:
ease
- the transition has a slow start, fast middle, then slow endlinear
- the transition has a consistent speed from start to endease-in
- the transition will have a slow startease-out
- the transition will have a slow endease-in-out
- the transition has a slow start and endcubic-bezier(n,n,n,n)
- a customizable transition values that we specify ourselves. Itâs handy to use generator tools such as https://cubic-bezier.com/.
Letâs see them all in action:
Transitioning multiple properties
We can transition multiple properties at once, by separating them with a comma.
.element {
transition: color 2s ease-out,
background-color 2s ease-out;
}
Which properties can be animated?
Lots! Hereâs the full list: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties.
Related Posts:
A little about me..
Hey, Iâm Tim! đ
Iâm a developer, tech writer & author. If youâd like to see all of my articles, you're in the right place! Browse the blog categories to find what interests you.
Iâm currently working on building my Complete Guide to Freelancing. The bad news is that itâs not available yet! But if itâs something you might be interested in, you can sign up to be notified when itâs available đ
Thanks for reading!