Curso Animações Web

Easing and Bézier Curves in CSS - The Definitive Guide

What is easing

Easing is the curve that defines how an animation progresses over time. Two animations with the same distance and the same duration can feel completely different: one speeds up at the start and slows down at the end, the other keeps the same pace from start to finish. In CSS, this curve goes in transition-timing-function or animation-timing-function, and when you don't set one, the browser uses ease by default.

To read an easing curve, picture it on a graph where the horizontal axis is time and the vertical axis is the animation's progress. Where the curve rises fast, the element moves fast, and where it lies almost flat, it's slowing down. An ease-out curve, for example, starts steep and ends flat: the element leaves quickly and arrives slowly.

cubic-bezier()

The keywords ease, ease-in, ease-out and ease-in-out are Bézier curves with preset values. In the cubic-bezier(x1, y1, x2, y2) function you set them yourself, and the values are the two control points of a curve that always starts at (0, 0) and ends at (1, 1). The x is time and must stay between 0 and 1. The y is progress and can go outside those limits. Above 1, the element passes its destination and comes back (as if a rubber band pulled it, also called overshoot). Below 0, it pulls back a little before leaving.

The keywords and their equivalent cubic-bezier()
Keyword Equivalent to When to use it
linear cubic-bezier(0, 0, 1, 1) continuous spins, like a loading spinner
ease cubic-bezier(0.25, 0.1, 0.25, 1) the default, when no easing is set
ease-in cubic-bezier(0.42, 0, 1, 1) elements leaving the screen
ease-out cubic-bezier(0, 0, 0.58, 1) elements entering the screen
ease-in-out cubic-bezier(0.42, 0, 0.58, 1) an element moving from one point to another

To keep them straight: in is the start of the animation, out is the end, and the name tells you where the slow part is. ease-in leaves slowly and speeds up, ease-out leaves fast and brakes, and ease-in-out is slow at both ends.

Animation libraries and sites like Easings.net use names such as easeOutQuint. They tell you how strong the curve is. In the in version, progress is time raised to a power: quad is t^2, cubic is t^3, quart is t^4 and quint is t^5. Each step up in power speeds up and brakes harder.

The names of the classic curves, from gentlest to strongest
Name Curve ease-out as cubic-bezier()
quad t^2, soft, close to ease-out cubic-bezier(0.5, 1, 0.89, 1)
cubic t^3, the middle ground cubic-bezier(0.33, 1, 0.68, 1)
quart t^4, strong cubic-bezier(0.25, 1, 0.5, 1)
quint t^5, the strongest cubic-bezier(0.22, 1, 0.36, 1)

linear() and spring animations in CSS

A cubic-bezier overshoots its destination at most once, while a spring goes, comes back and goes again until it settles. For that, CSS got the linear() function, which takes a list of points and connects each one to the next with a straight line. With enough points, those lines can draw any curve, including spring, bounce and elastic.

.spring {
  /* rises to 1.2 at 60% of the time, comes back to 0.9 at 80% and ends at 1 */
  transition-timing-function: linear(0, 1.2 60%, 0.9 80%, 1);
}

When a point has no percentage, CSS spreads the points at equal intervals across the duration. A convincing spring needs a lot of points, and you usually don't write them by hand. Use a ready-made one, like the examples on this site (in the linear() tab), which simulate the physics of a spring. Or grab one from sites like Easing Wizard, which has plenty of different examples.

steps() and stepped animations

The steps() function splits the duration into equal intervals and makes the value jump from one to the next, with nothing in between, like the second hand of a clock. One use is animating a sprite, where each frame of the image appears all at once.

The number is how many intervals there are. The second parameter is optional and says when the value jumps: with jump-end (the default, also written end) it jumps at the end of each interval and with jump-start (or start) at the beginning. There's also jump-none, which shows the start and end values as full steps and jump-both, which jumps both at the beginning and at the end.

/* second hand: 60 steps in 60 seconds */
.second-hand {
  animation: spin 60s steps(60) infinite;
}

@keyframes spin {
  to {
    rotate: 1turn;
  }
}

How to use the easing in your CSS

Any of these values goes where the easing goes, in both transition and animation, either in the shorthand property or in transition-timing-function and animation-timing-function.

.card {
  transition: translate 0.4s
    cubic-bezier(0.4, 1.6, 0.6, 1);
}

.alert {
  animation: enter 0.6s
    linear(0, 0.365, 0.83, 1.04, 1.07, 1.035, 1.01, 0.995, 1);
}

@keyframes enter {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

The easing applies to each interval between two keyframes, not to the whole animation. With keyframes at 0%, 50% and 100%, the curve runs twice, once in each half. If you want a single curve from start to end, use only from and to.

How long should an animation last

A lot of people use 300ms as a default, and that number sits in the middle of the 200ms to 500ms range, which is grounded in perception. In the Model Human Processor (Card, Moran and Newell, 1986), the eye takes 230ms on average to move to something and fixate on it. Well below that, an animation can read as a jump, and from around 500ms it starts to feel slow.

That doesn't mean the duration should always be around 300ms, though. The Bézier curve has a direct impact on the total duration. What we perceive is how long the element takes to get close to its destination. With cubic-bezier(0.22, 1, 0.36, 1) and 900ms, it covers 90% of the distance in about 340ms, and the rest happens while the element brakes. The animation lasts 900ms and still feels fast.

Frequently asked questions

Which easing should I use for most interface animations?

For elements entering the screen, use an ease-out or a stronger version of it, like cubic-bezier(0.22, 1, 0.36, 1). It starts fast, which feels like an instant response, and slows down gently at the end. For elements leaving, ease-in, and for continuous spins (like a loading spinner), linear.

Can I make a spring animation with CSS only?

Yes, with linear(). It isn't real physics, but you can simulate a spring with it. Use examples from sites like Easing Wizard.

Does linear() work in every browser?

It works in modern, up-to-date browsers. Chrome, Firefox and Safari have supported it since 2023. The full table is on MDN.

References and inspiration

The easing tools I use and recommend the most, and the ones that inspired this lab:

  • cubic-bezier.com, by Lea Verou. The original, and forever the coolest one in CSS. It's where a lot of people (me included) learned to drag the handles of a cubic-bezier().
  • Easings.net, the catalog of classic curves like easeOutQuint and easeInOutBack, each one with its graph, motion and code.
  • Easing Wizard, by Matthias Martin. One of the coolest easing tools out there, with Bézier, spring, bounce and overshoot editors and the CSS ready to copy.