transition-property refers to the CSS property you want to transition.
transition-duration refers to the duration of the transition. How long should this transition last? This value is written in seconds with the s suffix (like 3s).
transition-timing-function refers to how the transition occurs. You’ll learn more about this later.
transition-delay refers to the duration to wait before the duration starts. This value is written in seconds with the s suffix.
Triggering transitions
CSS transitions trigger when:
There is a transition-property
The value changes between two states
That means you can trigger transitions directly through pseudo classes like :hover (activates when mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).
The transition-timing-function governs how a transition occurs. It defaults to ease.
.selector {
transition: transform 1s ease;
}
The most common timing functions you’ll see are:
linear
ease
ease-in
ease-out
ease-in-out
cubic-bezier
Note: There’s also a step timing function. I never found much use for it though. You can read more about it here if you’re interested.
Linear
When you set the timing function to linear, your element moves at the same rate over the course of the animation. It doesn’t accelerate or decelerate.
.selector {
transition-timing-function: linear;
}
Easing
Easing is a little easier to explain though a few examples.
Imagine throwing a tennis ball into an open field. When you throw the ball, the ball leaves your hand at its max speed. As it moves, it loses energy, decelerates, and eventually comes to a halt. Right?
This pattern is called ease-out; you start off strong, then decelerates, and finally stops.
ease is weird. Its a faster version of ease-out for some weird reason. I don’t really know how to comment on it. See the difference between ease and other timing functions for yourself in the demo (below).
Don’t worry about x1, y1,, x2, and y2. They’re values you probably wouldn’t write by hand.
What you’ll do usually, when you use cubic-bezier, is use a helper tool. You can find helper tools everywhere on the internet, including in the devtools!
To open up the cubic-bezier helper in both Firefox’s and Chrome’s devtools, you create a transition-timing-function property for the element you want to transition, then click on the icon that shows up.
Note: we won’t get deep into cubic-bezier, as they’re outside the scope of this course. You can learn more about cubic-bezier in this Smashing Magazine article if you’re interested.
Transitioning two or more properties
To transition two (or more) properties, you need to separate each property with a comma.
If you use the shorthand, you’ll want to specify every property, duration, timing function, and delay. It’s always good to be explicit.
.selector {
transition:
background-color 1s ease-out,
color 1s ease-out;
}
You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.
You may be tempted to transition every CSS property with all. Don’t ever do this. This is bad for performance. Always specify the property you’re trying to transition.
/* DON'T EVER DO THIS */
.selector {
transition-property: all;
}
/* ALWAYS DO THIS */
.selector {
transition-property: background-color, color, transform;
}
Exercise
Create the following transitions:
Create an element.
Change its opacity from 1 to 0 over 1 second when you hover on it.
Try using ease, ease-in, ease-out, ease-in-out timing functions.
Create your own timing function with Cubic bezier.
Answers
Change an element’s opacity from 1 to 0 over 1 second when you hover on it.