Sub topic 6
Discrete properties and transition-behavior: allow-discrete
This chapter assumes you know how a transition uses the before-change style, and that you can write an @starting-style rule for an entry animation. It also assumes basic JavaScript for toggling classes. Here you will learn why an exit animation that ends in display: none does not run by default, and how transition-behavior: allow-discrete fixes that.
The problem: exits that never play
Consider a panel that should fade out when you hide it:
.panel {
opacity: 1;
transition: opacity 0.3s ease;
}
.panel.is-hidden {
opacity: 0;
display: none;
}Adding is-hidden makes the panel vanish at once. The opacity change from 1 to 0 is a valid transition, but display: none removes the element from rendering in the same style update. The browser stops running the transition because the element has no box left to paint.
To fix this, display has to stay at its old value until the fade finishes. A transition on display can do that, but display is not an ordinary animatable property.
Continuous and discrete properties
Every animatable CSS property has an interpolation type. This type tells the browser how to compute values between the start and end.
- A continuous property has intermediate values.
opacityat 0.5 is halfway between 0 and 1, andtransformandcolorwork the same way. - A discrete property has no in-between values.
display: blockanddisplay: nonehave nothing between them. The browser can only switch from one value to the other.
By default, a discrete property that would flip at the 50% point of an animation does not take part in transitions at all. Its new value applies immediately, as if no transition were declared. This is the normal behavior.
| Property | Interpolation | Notes |
|---|---|---|
| `opacity`, `transform`, `color` | Continuous | Transition by default |
| `visibility` | Discrete with a special rule | Stays `visible` for the whole transition if either end is `visible`. Transitions without `allow-discrete`. |
| `display` | Discrete with a special rule | Needs `allow-discrete`. Stays non-`none` for the whole transition. |
| `content-visibility` | Discrete with a special rule | Needs `allow-discrete`. Stays visible for the whole transition. |
| `overlay` | Discrete with a special rule | Needs `allow-discrete`. Keeps an element in the top layer during an exit. |
| Most other discrete properties | Discrete | Needs `allow-discrete`. Flips at the 50% point. |
The special rule for display is the key idea in this chapter. Most discrete properties flip halfway through the duration. For display, the browser applies the non-none value for the entire transition. Going from none to block, it switches to block at the start. Going from block to none, it switches to none at the end.
The transition-behavior property
transition-behavior takes one of two keywords:
normalis the default. Discrete properties do not transition.allow-discretelets discrete properties transition, using the rules in the table.
You can set it as a longhand, with one value for each entry in transition-property:
.panel {
transition-property: opacity, display;
transition-duration: 0.3s;
transition-behavior: allow-discrete;
}Or you can put the keyword in the transition shorthand, next to the property it applies to:
.panel {
transition:
opacity 0.3s ease,
display 0.3s allow-discrete;
}In the shorthand, allow-discrete applies only to the comma-separated item it appears in. Here opacity stays normal, which is fine because it is continuous.
The transition shorthand also resets transition-behavior to normal for any item that omits the keyword. If you write the longhand first and then a later transition shorthand, you lose allow-discrete. Put the longhand after the shorthand, or put the keyword in the shorthand.
How display is timed
The next diagram shows what display and opacity do when you hide the panel with a 0.3s transition on each.
Showing the panel again runs the sequence in reverse. display becomes block at once. Because the element has just been rendered, it has no before-change style, so @starting-style supplies the starting opacity: 0. The two features cooperate: allow-discrete handles the exit, and @starting-style handles the entry.
A complete show and hide panel
This page runs as written in a current Chromium, Safari or Firefox build.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Panel with display transition</title>
<style>
.panel {
opacity: 1;
padding: 1rem;
background: #eef;
transition:
opacity 0.3s ease,
display 0.3s allow-discrete;
@starting-style {
opacity: 0;
}
}
.panel.is-hidden {
display: none;
opacity: 0;
pointer-events: none;
}
</style>
</head>
<body>
<button id="toggle">Toggle panel</button>
<div id="panel" class="panel">Hello, I fade in and out.</div>
<script>
const panel = document.getElementById('panel');
document.getElementById('toggle').addEventListener('click', () => {
panel.classList.toggle('is-hidden');
});
</script>
</body>
</html>Step through one hide and one show:
- Clicking the button adds
is-hidden. The computedopacitygoes from 1 to 0 anddisplayfromblocktonone. - Both properties have a 0.3s transition.
opacityfades, whiledisplaystaysblockuntil the end. - At 0.3s the panel flips to
display: none. - Clicking again removes
is-hidden.displayflips toblockat once. - The panel has just been rendered, so the
@starting-stylevalue ofopacity: 0is the before-change style. The panel fades up to 1.
I added pointer-events: none to the hidden state. During the exit the panel is still rendered, so it would otherwise stay clickable while it fades.
Keeping the durations in step
The display transition has its own duration and does not follow the others. A mismatch causes visible bugs:
- If
displayis shorter thanopacity, the element disappears before the fade ends and the animation looks cut off. - If
displayis longer thanopacity, the element sits fully transparent but still rendered. It still takes up space and can still receive clicks untildisplayflips.
Give display the longest duration among the properties you animate, which is usually equal to the others. The same applies to transition-delay. If you delay opacity by 0.1s, add the delay to display as well:
.panel {
transition:
opacity 0.3s ease 0.1s,
display 0.4s allow-discrete;
}The timing function on display has no visible effect, because the flip happens only at the start or the end.
Using all
If you have several properties to animate, all is shorter:
.panel {
transition: all 0.3s allow-discrete;
}With allow-discrete, all includes display, overlay and content-visibility. This is convenient but broad. Every future change to any property on the element will transition, including ones you did not plan for, such as a height change from a layout shift. Naming properties is easier to debug.
Fallbacks for older browsers
Browsers that do not know allow-discrete ignore the keyword, so the whole declaration may be dropped. If your fallback must work, write the plain declaration first and then override it:
.panel {
transition: opacity 0.3s ease;
}
@supports (transition-behavior: allow-discrete) {
.panel {
transition:
opacity 0.3s ease,
display 0.3s allow-discrete;
}
}Without support, the panel still hides and shows correctly, only without the exit fade. This is a reasonable result for a purely decorative animation.
Common mistakes
- Writing
display 0.3swith noallow-discrete. The transition is ignored, and the panel disappears instantly. - Declaring
allow-discreteonly on the hidden state. Transitions use the destination state'stransitionvalue, so the hidden state is the one that controls the exit. Declare it on the base rule so it covers both directions. - Animating
visibilityanddisplaytogether and expecting a different result.visibilityalready transitions withoutallow-discrete. It keeps the element in layout, so it does not remove the space the element takes up. - Setting
transition-duration: 0s, for example from a reduced-motion rule. A duration of zero cancels the transition and thedisplayflip happens immediately. Use0.01msinstead if you want a near-instant change that still fires transition events. - Forgetting that
displayflipping tononeends the transition and firestransitionendfordisplay. If your script waits fortransitionend, checkevent.propertyName, becauseopacityanddisplayeach fire their own event.
Exercises
Exercise
A modal's CSS has .modal { transition: opacity 0.3s; } .modal.closed { opacity: 0; display: none; }. The fade never plays when you add closed. Give a corrected transition declaration and explain why it works.
Show answer
Use transition: opacity 0.3s, display 0.3s allow-discrete;. By default, display is a discrete property and does not transition, so it flips to none in the same update that starts the fade, and the element stops rendering. With allow-discrete, the browser holds display at its old value until the 0.3s duration ends. The element stays visible while opacity falls to 0 and is then switched to display: none.
Exercise
A panel uses transition: opacity 0.5s, display 0.2s allow-discrete;. What happens during a hide, and what would you change?
Show answer
display flips to none at 0.2s, so the panel disappears while opacity is still only 40% of the way through its fade. The exit looks cut off. Change the display duration to 0.5s so it matches the opacity duration.