Sub topic 3
Easing with ease, cubic-bezier() and steps()
This chapter assumes you can write a basic transition or animation declaration and know what duration and delay do. It covers the part that controls how a value moves between its start and end: the timing function.
What a timing function does
A timing function maps elapsed time to progress. Time runs from 0 to 1 across the duration, and the function returns how far along the value is. A straight line gives constant speed. A curve makes the motion speed up, slow down, or both.
You set it with transition-timing-function or animation-timing-function. In the shorthands it is the word or function that is not a time and not a property name.
Keyword easing
The keywords are shortcuts for specific curves.
| Keyword | Feel | Typical use |
|---|---|---|
| `linear` | Constant speed | Spinners, progress fills |
| `ease` | Quick start, long gentle stop (the default) | General purpose |
| `ease-in` | Slow start, fast end | Elements leaving the screen |
| `ease-out` | Fast start, slow end | Elements entering or responding to input |
| `ease-in-out` | Slow start and end | Looping or back-and-forth motion |
A common error is using ease-in for hover effects. The slow start makes the interface feel like it ignores the pointer for the first moments. Prefer ease-out for responses to user input.
Custom curves with cubic-bezier()
cubic-bezier(x1, y1, x2, y2) defines a curve through four numbers. These are the two inner control points of a Bézier curve that starts at (0, 0) and ends at (1, 1). The x values are time and must stay between 0 and 1. The y values are progress and may go outside that range.
Values of y above 1 or below 0 produce overshoot or anticipation. This card overshoots its end position and settles back:
.card {
transform: translateX(0);
transition: transform 500ms cubic-bezier(0.34, 1.56, 0.64, 1);
}
.card:hover {
transform: translateX(120px);
}The second number, 1.56, pushes progress past 100% before the curve returns to 1. The keyword ease equals cubic-bezier(0.25, 0.1, 0.25, 1).
Stepped motion with steps()
steps(n, position) splits the duration into n equal jumps with no in-between values. It suits sprite sheets, typewriter effects, and clock-like ticks.
.sprite {
width: 64px;
height: 64px;
background: url("walk.png") 0 0 / 512px 64px;
animation: walk 800ms steps(8) infinite;
}
@keyframes walk {
to {
background-position: -512px 0;
}
}The sheet holds 8 frames of 64px each. steps(8) moves the background in 8 jumps, so each frame shows cleanly instead of sliding between two frames.
The optional second argument sets when the jumps occur:
jump-end(default, same asend): the first value holds at the start and the final jump lands at the end.jump-start(orstart): the first jump happens immediately.jump-none: both the first and last values are held, so there aren - 1jumps.jump-both: extra jumps occur at both ends, givingn + 1distinct values.
Easing inside keyframes
In an animation, animation-timing-function applies to each segment between keyframes, not to the animation as a whole. You can override it per keyframe:
@keyframes bounce {
0% {
transform: translateY(0);
animation-timing-function: ease-out;
}
50% {
transform: translateY(-40px);
animation-timing-function: ease-in;
}
100% {
transform: translateY(0);
}
}
.ball {
animation: bounce 900ms infinite;
}Here the ball rises with ease-out and falls with ease-in, which resembles gravity. If you set ease-in-out on the .ball rule instead, every segment would ease in and out separately, and the ball would pause briefly at the midpoint.
Exercise
You wrote transition: opacity 300ms cubic-bezier(1.2, 0, 0.5, 1); and the fade runs with the default easing. Why, and what is one valid change that keeps the same general shape?
Show answer
The first x value, 1.2, is outside the allowed range of 0 to 1, so the cubic-bezier() is invalid and the declaration is dropped, leaving the default ease. Changing it to something like cubic-bezier(0.9, 0, 0.5, 1) makes it valid. Only the y values may go outside 0 to 1.