More CSS answers

by Danny
Published 3 min read

Transitions fail silently when a property can't animate, such as display: none or height: auto. Learn the common causes and the modern fixes, including allow-discrete.

The short answer

#

A transition needs a start value and an end value the browser can interpolate between. It will not run if the property is display, if one side is height: auto, or if the element has just been added to the page. Animate opacity and transform instead, or use transition-behavior: allow-discrete with @starting-style for elements that appear and disappear.


_15
.panel {
_15
opacity: 1;
_15
transition: opacity 200ms, display 200ms allow-discrete;
_15
}
_15
_15
.panel[hidden] {
_15
display: none;
_15
opacity: 0;
_15
}
_15
_15
@starting-style {
_15
.panel {
_15
opacity: 0;
_15
}
_15
}

Why it happens

#

A CSS transition runs when a property's computed value changes from one value to another. The browser then draws the in-between values over the duration you set. That only works when:

  • The property is animatable and both values are of a type that can be mixed, such as two lengths or two colours.
  • The element already had a computed style before the change. A newly inserted element has no "before", so it just appears in its final state.
  • The transition declaration applies to the state the element is moving into, or to both states.

display is a discrete property: there is no halfway point between block and none. When it flips to none, the element is removed from rendering immediately, so any opacity fade alongside it is cut off. Keywords such as auto, min-content and fit-content are not numbers either, so height: 0 to height: auto cannot be interpolated by default.

How to fix it

#

Transition entry and exit with allow-discrete

transition-behavior: allow-discrete (or the allow-discrete keyword in the shorthand) lets discrete properties like display take part. The browser keeps the element visible for the length of the exit transition and only switches display at the end. @starting-style gives an element its "before" values the first time it renders, so the entry animates too. Both are supported in current Chrome, Edge, Firefox and Safari, and have been since 2024.

This pattern works well for popovers and dialogs, which move in and out of the top layer:


_17
[popover] {
_17
opacity: 0;
_17
transition:
_17
opacity 200ms,
_17
overlay 200ms allow-discrete,
_17
display 200ms allow-discrete;
_17
}
_17
_17
[popover]:popover-open {
_17
opacity: 1;
_17
}
_17
_17
@starting-style {
_17
[popover]:popover-open {
_17
opacity: 0;
_17
}
_17
}

In older browsers the element simply appears and disappears without animation, which is a safe fallback.

Animate to height: auto

interpolate-size: allow-keywords lets the browser transition between a length and a sizing keyword like auto. Set it on :root to opt in for the whole page. At the time of writing it is only supported in Chromium browsers (Chrome and Edge), so treat it as an enhancement: other browsers jump straight to the end state.


_13
:root {
_13
interpolate-size: allow-keywords;
_13
}
_13
_13
.drawer {
_13
height: 0;
_13
overflow: hidden;
_13
transition: height 300ms ease;
_13
}
_13
_13
.drawer.is-open {
_13
height: auto;
_13
}

For a cross-browser approach today, animate a grid track from 0fr to 1fr. Grid tracks interpolate in every modern browser.


_13
.drawer {
_13
display: grid;
_13
grid-template-rows: 0fr;
_13
transition: grid-template-rows 300ms ease;
_13
}
_13
_13
.drawer.is-open {
_13
grid-template-rows: 1fr;
_13
}
_13
_13
.drawer > .inner {
_13
overflow: hidden;
_13
}

Wait a frame for elements added with JavaScript

If you insert an element and add a class in the same task, the browser never sees the starting style. Use @starting-style where you can. Otherwise, force a style calculation or wait for the next frame before adding the class.


_10
list.append(item);
_10
requestAnimationFrame(() => {
_10
item.classList.add('is-visible');
_10
});

Check that the transition rule applies

Open DevTools, select the element and look at the computed transition value in both states. A more specific rule, a utility class or a framework reset may be overriding it, or the transition may only be declared on the hover state, so it plays one way but not back.

Common pitfalls

#
  • Transitioning all. It is easy to animate properties you did not mean to, and it hides which property is really changing. List the properties.
  • Misspelling a property or unit in the shorthand. An invalid transition value is dropped completely, with no error.
  • Animating width, height or top for movement. They trigger layout on every frame. transform and opacity are much cheaper.
  • Forgetting users who prefer less motion. Wrap large movement in @media (prefers-reduced-motion: no-preference).
  • Putting allow-discrete on the whole shorthand with all. Be explicit about which properties are discrete.
#

Was this resource helpful?