CSS Transitions — Web Animation

transition makes a CSS change happen gradually. It holds, in order: property, duration, easing (the speed pattern of the movement), delay (the wait before it…

An ordinary light switch: you press it and the light is bright right away. A dimmer is different: you still turn it to "bright", but the light rises step by step. A CSS change without a transition is like the ordinary switch: the value jumps. The transition property makes that change work like a dimmer. You still change the value yourself (through a class, :hover or an attribute), and the browser fills in the values in between, a little at a time. This gradual change is called a transition.

Writing a transition

transition is a shorthand: one property that sets four properties at once. Those four are called longhands:

Longhand What it holds Default
transition-property the property to animate all
transition-duration how long, and it needs a unit: 0.3s or 300ms 0s
transition-timing-function the speed pattern of the movement (easing) ease
transition-delay the wait before it starts 0s

In the shorthand, the order is property, duration, easing, delay. The first time value is always the duration, the second the delay. Several transitions are joined with commas. Here are the night buttons on Mrs. Sri's page:

.nights button {
  color: #1f2937;
  background: #fff;
  border: 1px solid #475569;
  transition: background-color 150ms ease-out, color 150ms ease-out, border-color 150ms ease-out;
}
.nights button[aria-pressed="true"] {
  color: #fff;
  background: #15803d;
  border-color: #15803d;
}

In the exercise, you build a transition value as a string with JavaScript. One transition has three parts separated by spaces. Several transitions are joined with ", " (a comma and a space):

Input Result
"opacity", "0.3s", "ease-out" opacity 0.3s ease-out
two objects: opacity 0.3s ease, transform 0.5s ease-out opacity 0.3s ease, transform 0.5s ease-out
one object: width 1s steps(4) width 1s steps(4), no comma

Delay: when does the transition end?

Total time = delay + duration. For example, transition: opacity 0.3s ease-in 0.1s;. For the first 0.1 seconds, nothing moves. After that, opacity changes for 0.3 seconds. So the transition ends 0.4 seconds after the value changed.

At each stage of a transition, the browser fires an event (a signal that JavaScript can wait for). We measured them for transition: opacity 200ms ease-out 100ms in Chromium 147, WebKit 26.4 and Firefox 146. The times count from the moment the value changed:

Event Meaning Arrives at
transitionrun the transition is created, the delay starts counting 3 to 14 ms
transitionstart the delay is over, the value starts changing 104 to 116 ms
transitionend the value reached its target 303 to 313 ms

The Book button: hover, focus and pressed

The Book button has three states that animate: hover, focused with the keyboard, and pressed.

.book {
  background: #15803d;
  outline: 3px solid transparent;
  outline-offset: 0;
  transition: background-color 150ms ease-out, outline-color 150ms ease-out,
              outline-offset 150ms ease-out, transform 100ms ease-out;
}
.book:hover { background: #166534; }                         /* darker green */
.book:focus-visible { outline-color: #166534; outline-offset: 3px; } /* keyboard focus ring */
.book:active { transform: scale(0.98); }                     /* shrinks a little when pressed */

The browser always uses the transition of the destination state, the state the element is moving to. If transition is only on .book:hover, going into hover animates, because the destination state is .book:hover. Leaving hover jumps, because the destination state, .book, has no transition. Each direction has its own destination state, so each can have its own duration: open in 200 ms, close in 150 ms, like the dialog in the next section.

What can be transitioned

A property can be interpolated when the browser can calculate the values between the old value and the new one. Examples are numbers, lengths, percentages, colours, opacity, transform and box-shadow. Properties whose value is a choice of keywords, like display (none or block) or position, are discrete: there is no half-none. That's why a plain transition cannot animate display. visibility is the exception: it takes part in the transition, and the element stays visible while the transition runs. We measured it in the middle of the transition: visible in all three browsers.

Two newer features let an element appear and disappear with an animation:

The confirmation dialog (class .modal, with the attribute open while it is open) uses both:

.modal {
  opacity: 0;
  transform: scale(0.96);
  transition: opacity 150ms ease-in, transform 150ms ease-in,
              display 150ms allow-discrete, overlay 150ms allow-discrete;
}
.modal[open] {
  opacity: 1;
  transform: none;
  transition-duration: 200ms;          /* opens slower than it closes */
  transition-timing-function: ease-out;
}
@starting-style {
  .modal[open] { opacity: 0; transform: scale(0.96); }
}

We opened and closed this <dialog> with showModal() and close(). The opening animation ran in Chromium 147, WebKit 26.4 and Firefox 146. The closing animation ran in Chromium and WebKit. In Firefox, the dialog disappeared right away. overlay (the dialog stays in the top layer during the closing animation) only exists from Chrome 117. Other browsers safely ignore that line.

transitionend: one event per property

Before reading on, take a guess. The "3 nights" button has transition: all 150ms ease-out (all: every property). When it is picked, its background, text and border colours change. How many times does the transitionend event arrive?

transitionend is the event for a finished transition, and it arrives once for each property that finishes moving. We picked the "3 nights" button with two versions of the CSS, in all three browsers:

transition on the button transitionend events that arrive
all 150ms ease-out 6: background-color, color, border-top-color, border-right-color, border-bottom-color, border-left-color
background-color 150ms ease-out 1: background-color

So 6 times, not 3. border-color is a shorthand for the colour of the border's four sides, and each side fires its own event. That's why a listener (a function that waits for an event) always checks e.propertyName, the name of the property whose transition finished. For example, a listener for the toast:

toast.addEventListener('transitionend', (e) => {
  if (e.propertyName !== 'opacity') return; // wait for one property only
  if (!toast.classList.contains('show')) toast.hidden = true;
});

The third line hides the toast (hidden) once the class show has been removed.

transitionend doesn't arrive in three cases:

⚠️ Transitions that jump, stay still, or never end

TL;DR: transition makes a CSS change happen gradually. It holds, in order: property, duration, easing (the speed pattern of the movement), delay (the wait before it starts), for example opacity 0.3s ease-out. Several transitions are joined with commas, and the duration needs a unit. Total time = delay + duration: opacity 0.3s ease-in 0.1s ends at 0.4 seconds, with the events (signals from the browser) transitionrun at the start, transitionstart after the delay, and transitionend at the end. The browser uses the transition of the destination state (the state the element is moving to), so write transition on the base rule (the one without :hover): then leaving hover animates too. These can be interpolated (their in-between values calculated): numbers, lengths, colours, opacity, transform. display is discrete (its value is a keyword such as none or block), so a plain transition can't animate it. allow-discrete (Chrome 117, Firefox 129, Safari 17.4) and @starting-style (Chrome 117, Firefox 129, Safari 17.5) let an element appear with an animation. An exit through display works in Chrome 117 and Safari 18, not yet in Firefox. transitionend arrives once per property: transition: all on a night button fires 6 events (background, text colour and the border's four sides). It doesn't arrive when the value doesn't change, or when the element becomes display: none (then transitioncancel arrives).

⚡ A transition on the base rule

Write:

.nights button {
  background: #fff;
  transition: background-color 150ms ease-out;
}
.nights button[aria-pressed="true"] {
  background: #15803d;
}

Meaning: When a night button (.nights button) is picked (aria-pressed="true"), its background changes gradually from white to green over 150 ms. transition holds, in order: property, duration, easing, then delay. The duration needs a unit. transition sits on the base rule .nights button, which applies whether the button is picked or not. So it runs both ways: when the button is picked and unpicked.

⚡ The delay counts too

Guess first: how many seconds after the value changes does transition: opacity 0.3s ease-in 0.1s; end?

0.4 seconds. The first time value is the duration, the second the delay. For 0.1 seconds nothing moves, then opacity changes for 0.3 seconds.

Meaning: Total time = delay + duration.

⚡ display has no in-between

Meaning: Numbers, lengths, colours, opacity and transform can be interpolated: the browser can calculate the values between the old value and the new one. display is discrete: its value is a keyword such as none or block, with no half-none. So a plain transition can't animate it. To make an element appear with an animation, use allow-discrete (discrete properties join the transition) and @starting-style (starting values for an element that just appeared).

⚡ One transitionend per property

Guess first: transitionend is the event for a finished transition. The "3 nights" button with transition: all 150ms ease-out changes its background, text and border colour. How many times does transitionend arrive?

6 times, not 3. border-color sets the colour of the border's four sides at once, and each side fires its own event.

Meaning: So a transitionend listener (a function that waits for that event) always checks e.propertyName first: the name of the property whose transition finished. Then it reacts to only one property.

What you will learn