Skip to content
Tutorials Center
My learning

Topics

Lesson 97 of 139 ยท Transforms and Animation

Transitions

Animate CSS changes smoothly with transitions. Learn duration, delay and easing with cubic-bezier(), steps() and linear(), and which properties animate well.

When a CSS value changes, the browser normally applies the new value instantly. A button turns from blue to dark blue in a single frame. A transition tells the browser to take its time instead: to slide from the old value to the new one over, say, 200 milliseconds.

Transitions are the simplest kind of CSS animation. You don't describe any motion yourself. You only say which properties should change gradually, how long the change takes and what speed curve it follows. The trigger is any ordinary style change: a :hover or :focus-visible state, a class added with JavaScript, or a media query that starts to match.

In this lesson you will learn the four transition properties, the easing functions that give motion its character, and which properties are cheap and smooth to animate.

Your first transition

Here is a button whose background and shadow change on hover. The only new line is transition:

CSS
.button {
  background: #2563eb;
  transition: background-color 200ms ease-out;
}

.button:hover {
  background: #1e40af;
}

Notice where the transition goes: on the base rule, not on :hover. The transition describes how the element moves between any two values of that property, so it should apply in both directions. If you put it only on :hover, the change in is smooth but the change back out snaps instantly, because when the pointer leaves, the :hover rule (and its transition) no longer applies.

Transition on the base rule vs on :hover

The four transition properties

The transition shorthand combines four longhand properties:

Property What it sets Default
transition-property Which properties transition, such as opacity or all all
transition-duration How long the change takes, in s or ms 0s
transition-timing-function The speed curve (easing) ease
transition-delay How long to wait before starting 0s

Because the default duration is 0s, nothing animates until you set a duration. In the shorthand, the first time value is always the duration and the second is the delay:

CSS
.card {
  /* property | duration | easing | delay */
  transition: transform 300ms ease-in-out 100ms;
}

To transition several properties with different settings, separate them with commas:

CSS
.card {
  transition:
    transform 300ms ease-out,
    box-shadow 300ms ease-out,
    opacity 150ms linear;
}

You can also write transition: all 300ms, which covers every property that changes. It is quick to type, but it can surprise you later: a color that changes because of a theme switch, or a padding changed in a media query, will suddenly animate too. Listing properties explicitly keeps motion intentional.

Good durations are short. Most interface transitions feel right between 100ms and 300ms. Hover feedback can be on the short end; larger movements such as a panel sliding in can use 300โ€“500ms. Anything longer starts to feel sluggish when users repeat it many times.

Easing: the speed curve

The timing function controls how progress is spread over time. With linear, the value changes at a constant rate. Real objects rarely move like that; they speed up and slow down, so curved easing usually feels more natural. CSS has five keywords:

  • ease (the default): starts fairly quickly and slows down gently.
  • linear: constant speed. Good for colors, opacity and spinners.
  • ease-in: starts slowly, ends fast. Suits things leaving the screen.
  • ease-out: starts fast, ends slowly. Suits things arriving, and most hover effects.
  • ease-in-out: slow at both ends. Suits things moving from one place to another on screen.

Custom curves with cubic-bezier()

Each keyword is really a cubic Bรฉzier curve. You can define your own with cubic-bezier(x1, y1, x2, y2), which sets two control points. The x values (time) must be between 0 and 1, but the y values (progress) can go outside that range. A y value above 1 makes the animation overshoot its target and settle back, which gives a playful "springy" feel:

CSS
transition: transform 400ms cubic-bezier(0.34, 1.56, 0.64, 1);

DevTools in every major browser has a curve editor: click the small curve icon next to an easing value in the Styles panel and drag the handles.

Jumps with steps()

steps(n) divides the transition into n equal jumps with no smooth movement in between. It is useful for ticking clocks, typewriter effects and sprite-sheet animations. An optional second argument, such as jump-start or jump-end (the default), says whether the jump happens at the start or end of each step.

linear() for complex curves

The newer linear() function takes a list of progress points and joins them with straight lines. With enough points it can approximate bounces and springs that cubic-bezier() cannot:

CSS
transition-timing-function: linear(0, 0.6 25%, 1.1 50%, 0.95 75%, 1);

linear() has worked in Chrome, Edge, Firefox and Safari since 2023. Older browsers ignore the declaration and fall back to whatever came before it.

Comparing easing functions

All seven dots take exactly 1.2 seconds. They only distribute their progress differently. Click Run again while they are moving: a transition interrupted halfway simply reverses from wherever it is.

Delays and staggering

transition-delay waits before a transition starts. A small delay on a hover menu stops it from flickering open when the pointer merely passes over it. Delays can also stagger a group, so items move one after another. A custom property per item keeps the CSS short:

CSS
.item {
  transition: opacity 300ms ease-out, translate 300ms ease-out;
  transition-delay: calc(var(--i) * 60ms);
}

A negative delay starts the transition partway through, as if it had already been running for that long.

Which properties can transition?

A property can transition smoothly only if the browser knows how to calculate the values in between. Lengths, percentages, colors, numbers, shadows, transforms and gradients' positions all interpolate. Keywords usually do not: there is no value halfway between display: block and display: none, or between text-align: left and center. Such properties are discrete: they flip at the midpoint, or don't animate at all.

Even among animatable properties, some are much cheaper than others:

  • transform, translate, rotate, scale and opacity are the best choices. The browser can usually animate them without recalculating the page layout, so they stay smooth even on slow phones.
  • Colors, shadows and filter require repainting pixels but not layout. Fine for small elements.
  • width, height, top, left, margin and padding change layout, so the browser must recompute positions of other elements on every frame. Use them sparingly.

You will learn the reasons behind this list in Motion, Performance and prefers-reduced-motion.

The height: auto problem

A classic frustration: you cannot transition to or from height: auto, because auto is a keyword, not a number. Common workarounds are animating max-height to a large value, animating grid-template-rows from 0fr to 1fr, or using a transform. Chromium browsers also support interpolate-size: allow-keywords, which makes auto animatable; other browsers simply jump, which is an acceptable fallback.

Transitions from display: none

Transitions only run when a value changes on an element that is already rendered. An element appearing from display: none has no "before" style, so it just pops in. Two newer features solve this:

  • @starting-style { ... } defines the styles an element should transition from when it first appears.
  • transition-behavior: allow-discrete lets discrete properties like display take part, flipping at the right moment so a fade-out can finish before the element disappears.

Both are supported in current Chrome, Edge, Firefox and Safari (since 2024). Older browsers show and hide the element instantly.

Fade in from display: none with @starting-style

Notice that allow-discrete is written inside the shorthand for display. The console also shows the transitionend event, which fires once per finished property. JavaScript can listen for it to clean up after an animation.

Respecting reduced motion

Some people get dizzy or nauseous from movement on screen, and their operating systems let them ask for less of it. CSS exposes that setting as the prefers-reduced-motion media query. Every demo in this chapter includes a block like this:

CSS
@media (prefers-reduced-motion: reduce) {
  .panel { transition-duration: 1ms; }
}

Color and opacity fades are generally safe to keep. Large movement, zooming and spinning are the things to tone down. Motion, Performance and prefers-reduced-motion covers strategies in depth.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should transition usually go on the base rule instead of the :hover rule?

    When the pointer leaves, the :hover rule stops applying, and so does any transition declared inside it. On the base rule, the transition applies in both directions.
  2. 2. In transition: opacity 0.3s ease 0.1s, what is 0.1s?

    In the shorthand, the first time value is the duration and the second is the delay.
  3. 3. What does a cubic-bezier() y value greater than 1 produce?

    The y axis is progress. Values above 1 mean the property briefly goes beyond its end value, which looks springy.
  4. 4. Which pair of properties is the cheapest and smoothest to animate?

    Transforms and opacity can usually be animated without recalculating layout, so browsers keep them smooth even on slower devices.
  5. 5. Why does an element shown by switching from display: none normally pop in instead of fading?

    Transitions need a before value. @starting-style provides one, and transition-behavior: allow-discrete lets display join the transition.

Summary

  • A transition animates a property between its old and new values whenever a style changes.
  • Set transition-property, transition-duration, transition-timing-function and transition-delay, or the transition shorthand; the first time is the duration.
  • Put transitions on the base rule so they run in both directions, and list properties instead of all.
  • Easing shapes the motion: keywords, cubic-bezier() (with overshoot), steps() and linear().
  • transform and opacity animate most cheaply; discrete keywords and height: auto need special techniques.
  • @starting-style and transition-behavior: allow-discrete animate elements in from display: none.
  • Tone down motion inside @media (prefers-reduced-motion: reduce).

Next: 2D Transforms โ€” move, rotate, scale and skew elements without disturbing the layout.