Sub topic 3
Easing with ease, cubic-bezier() and steps()
This chapter assumes you can write a basic transition or keyframe animation. You should know the transition and animation shorthands, and you should know that a duration is the time an effect takes. Here you learn how to control the speed of a change while it runs.
What a timing function does
A timing function maps progress through time to progress through the effect. Both scales run from 0 to 1. At 50% of the duration, a linear function reports 50% of the change. An easing function can report 80% instead, so the element moves quickly at first and slows near the end.
The duration stays the same. Only the distribution of the change inside that duration differs.
The same property name is used for both features:
transition-timing-functionsets the curve for a transition.animation-timing-functionsets the curve for an animation.- The default for both is
ease.
You can also put the function inside the transition or animation shorthand, as the earlier chapters did.
The keywords
Every keyword is a shorthand for a cubic Bézier curve, except linear, which is a straight line.
| Keyword | Equivalent | Feel |
|---|---|---|
| `linear` | `cubic-bezier(0, 0, 1, 1)` | Constant speed |
| `ease` | `cubic-bezier(0.25, 0.1, 0.25, 1)` | Quick start, long gentle stop |
| `ease-in` | `cubic-bezier(0.42, 0, 1, 1)` | Slow start, fast end |
| `ease-out` | `cubic-bezier(0, 0, 0.58, 1)` | Fast start, slow end |
| `ease-in-out` | `cubic-bezier(0.42, 0, 0.58, 1)` | Slow start, fast middle, slow end |
A practical rule of thumb comes from how motion looks in the physical world:
- Use
ease-outfor elements that enter or respond to a user action. They arrive quickly and settle, so the interface feels responsive. - Use
ease-infor elements that leave the screen. They accelerate away. - Use
ease-in-outfor elements that move from one visible place to another. - Use
linearfor motion that has no start or end, such as a spinner, or for effects that are not about position, such as a progress bar.
.menu {
transform: translateY(-8px);
opacity: 0;
transition: transform 200ms ease-out, opacity 200ms ease-out;
}
.menu.is-open {
transform: none;
opacity: 1;
}
.menu.is-leaving {
transition-timing-function: ease-in;
}Writing a curve with cubic-bezier()
cubic-bezier(x1, y1, x2, y2) describes a curve that starts at (0, 0) and ends at (1, 1). The four numbers are the coordinates of two control points that bend the curve between them.
- The horizontal axis is time progress.
- The vertical axis is effect progress.
- A steep section of the curve means fast movement. A flat section means slow movement.
Rules for the numbers
x1andx2must be between 0 and 1. A value outside that range makes the whole declaration invalid, and the browser ignores it. The element then falls back to the inherited or default timing function, with no error message.y1andy2can be any number. A value above 1 or below 0 makes the effect overshoot its target and come back, or pull back before it starts.
This overshoot curve makes a panel slide slightly past its resting position and settle:
.panel {
transform: translateX(-100%);
transition: transform 450ms cubic-bezier(0.34, 1.56, 0.64, 1);
}
.panel.is-visible {
transform: translateX(0);
}Overshoot only makes sense for properties where values outside the range are legal. A transform can overshoot freely. An opacity value is clamped to the range 0 to 1, so an overshooting curve has no visible effect there.
Choosing numbers
Writing control points by hand is slow. Use the curve editor in your browser's developer tools (covered in a later chapter) or a site such as cubic-bezier.com. Drag the handles, watch the preview, and copy the result. Keep a small set of named curves in custom properties so the whole interface shares one feel:
:root {
--ease-out-strong: cubic-bezier(0.16, 1, 0.3, 1);
--ease-in-strong: cubic-bezier(0.7, 0, 0.84, 0);
}
.toast {
transition: transform 300ms var(--ease-out-strong);
}Jumping in discrete steps with steps()
steps(n, jump-term) splits the duration into n equal intervals. The value does not change smoothly. It holds, then jumps to the next level at the start of each interval.
Use it for effects that are naturally discrete:
- Sprite-sheet animation, where each frame is a separate picture.
- A typewriter effect, where text appears one character at a time.
- A segmented progress indicator.
The jump terms
The second argument decides where the jumps happen. This table shows the output values for steps(4, ...), as fractions of the total change.
| Jump term | Values shown over time | Notes |
|---|---|---|
| `jump-start` | 25%, 50%, 75%, 100% | Jumps immediately at the start. Never shows the 0% value after the start. |
| `jump-end` | 0%, 25%, 50%, 75% | The default. The final value appears only when the effect ends. |
| `jump-none` | 0%, 33%, 67%, 100% | Holds both the first and last values for a full interval. `n` must be at least 2. |
| `jump-both` | 20%, 40%, 60%, 80% | Jumps at both ends, so neither the first nor last value is shown during the run. |
The older words start and end still work and mean jump-start and jump-end. The keywords step-start and step-end are shorthand for steps(1, jump-start) and steps(1, jump-end).
A typewriter example
The text below is 14 characters long. A monospace font gives every character the same width, and the ch unit measures that width, so 14 steps reveal one character at a time.
<p class="typewriter">Hello, motion!</p>.typewriter {
font-family: monospace;
width: 14ch;
white-space: nowrap;
overflow: hidden;
border-right: 2px solid currentColor;
animation: type 2s steps(14, jump-end) both;
}
@keyframes type {
from {
width: 0;
}
to {
width: 14ch;
}
}Animating width triggers layout, so this is fine for a short heading but not a good model for large or frequently repeated effects.
A common mistake with steps
With the default jump-end, the last step is not shown until the animation ends. A sprite animation of 8 frames that uses steps(8) and moves by the full sheet width shows frame 8 for no time at all, and shows frame 1 again at the loop point. Match the keyframe distance to the steps. For a sheet of 8 frames that are each 64px wide, move the background by 8 frame widths and use steps(8):
.sprite {
width: 64px;
height: 64px;
background: url("walk.png") 0 0 / 512px 64px;
animation: walk 800ms steps(8) infinite;
}
@keyframes walk {
to {
background-position-x: -512px;
}
}The steps(8) here uses jump-end: the offsets shown are 0, -64, ..., -448 pixels, so all 8 frames appear. The final offset of -512px is never displayed, because the loop restarts first.
Where the curve applies
Transitions
For a transition, the curve covers the whole change from the old value to the new value. Each property in a comma-separated list can have its own curve.
The timing function that counts is the one declared on the element in its destination state. If the base rule says transition: transform 200ms ease-out and the :hover rule says transition: transform 600ms linear, hovering uses the slow linear curve, and un-hovering uses the base rule's curve. This lets you make entering and leaving feel different, as in the .menu example above.
Keyframe animations
In an animation, animation-timing-function applies between each pair of keyframes, not across the whole animation. A three-step animation with ease-in-out eases in and out of every segment, so the element slows to a near stop at each keyframe.
You can override the curve inside a single keyframe. The curve declared in a keyframe controls the segment that starts at that 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;
}The ball decelerates on the way up and accelerates on the way down, which matches gravity.
Two related behaviours are worth knowing:
- With
animation-direction: reverseor on the backward legs ofalternate, the timing functions are also reversed. Anease-insegment plays asease-outgoing backward. - A looping animation with a plain
easecurve slows to a stop at the end of each iteration and then starts again. For continuous motion such as a spinner, uselinear.
Choosing a curve
Common errors
- Out-of-range x value.
cubic-bezier(1.2, 0, 0.5, 1)is invalid. The declaration is dropped silently. - Missing jump-term logic.
steps(1, jump-none)is invalid becausejump-noneneeds at least 2 steps. - Expecting overshoot on opacity. Values are clamped, so you see no bounce.
- Eased loops.
easeon a repeating 0%-to-100% animation produces a visible pause at every loop. - Long ease-in on feedback. A 500ms
ease-inon a button makes it feel unresponsive, because almost nothing moves for the first half of the effect. Keep interaction feedback short and useease-out.
Beyond cubic curves
The linear() function accepts a list of points and draws a piecewise straight line through them. With enough points it approximates springs and bounces that a single Bézier curve cannot produce. It is supported in current versions of all major browsers. Generate the point list with a tool rather than writing it by hand.
Exercise
A reveal animation uses animation: reveal 1s cubic-bezier(1.5, 0, 0.5, 1) and the element appears to ignore the custom curve and move as if it used ease. What is wrong, and how do you get a fast-start curve that overshoots slightly?
Show answer
The first control point has x = 1.5, which is outside the allowed range of 0 to 1, so the declaration is invalid and the default ease is used. Keep both x values between 0 and 1 and put the overshoot in a y value instead, for example cubic-bezier(0.34, 1.56, 0.64, 1).
Exercise
You build a loading bar of 5 visible segments with animation: fill 2s steps(5, jump-end), going from width: 0 to width: 100%. The bar never shows a fully filled state until the animation ends. Which jump term would show 5 filled segments, with the first one already visible at the start, and what do the values over time become?
Show answer
Use steps(5, jump-start). It jumps at the start of each interval, so the values are 20%, 40%, 60%, 80% and 100%. The first segment is visible immediately and the bar is full for the last interval.