Sub topic 6
Discrete properties and transition-behavior: allow-discrete
This chapter assumes you know how transition-property, duration and timing functions work, what display: none does, and how the cascade picks a winning value. It also assumes you have seen @starting-style in the previous chapters.
What a discrete property is
Most CSS properties are interpolated. The browser computes values between the start and end, so opacity can pass through 0.25 and 0.5 on its way from 0 to 1.
A discrete property has no in-between values. display, visibility (partly), content-visibility and overlay are examples. Between block and none there is nothing to compute, so the browser flips the value at a single moment.
By default, discrete properties do not transition. Even if you list display in transition-property, the value switches instantly and no transition starts. This is why a fade-out followed by display: none normally needs JavaScript that waits for transitionend.
Turning on discrete transitions
The transition-behavior property has two values:
normalis the default. Discrete properties do not transition.allow-discretelets discrete properties take part in a transition.
You can set it on its own or as the last part of the transition shorthand:
.panel {
transition: opacity 0.3s ease, display 0.3s allow-discrete;
}The longhand form is equivalent:
.panel {
transition-property: opacity, display;
transition-duration: 0.3s;
transition-behavior: normal, allow-discrete;
}Note that transition-behavior takes a list, one entry per property. Putting allow-discrete only on display leaves opacity as normal, which is fine because opacity is already interpolated.
If you use transition: all, you can write transition: all 0.3s allow-discrete to include discrete properties. Being explicit is usually clearer.
When the value flips
A discrete transition does not blend values. It runs for the full duration and swaps the value at a defined point:
- For
display, the flip happens at the start of the transition when going to a visible value (such asnonetoblock), and at the end when going tonone. - That rule is what makes exit animations work. The element stays rendered while
opacityfades, and only then becomesdisplay: none.
A show and hide panel
This panel combines allow-discrete with @starting-style:
.panel {
opacity: 1;
transition: opacity 0.3s ease, display 0.3s allow-discrete;
@starting-style {
opacity: 0;
}
}
.panel.is-hidden {
display: none;
opacity: 0;
}Adding is-hidden changes display to none and opacity to 0. Because display is in the transition list with allow-discrete, the browser keeps the element rendered for 0.3 seconds while opacity fades, then applies none. Removing the class changes display back to block at once. The element is then rendered for the first time since it was hidden, so @starting-style supplies opacity: 0 as the before-change style and the fade-in runs.
The JavaScript is a one-liner:
document.querySelector('.panel').classList.toggle('is-hidden');Common errors
- Forgetting
displayin the transition list. Without it, the element disappears the instant the class is added and no exit fade is visible. - Using
transition: opacity 0.3sonly. The shorthand resetstransition-behaviortonormalfor every property it lists. Always writeallow-discreteafter thedisplayentry. - Setting the final
displayin a different rule that loses the cascade. The transition only starts if the computed value changes, so check which rule wins. - Expecting interpolation.
displaynever passes through intermediate values. Onlyopacity,transformand similar properties animate smoothly. - Mismatched durations. If
displayhas a shorter duration thanopacity, the element vanishes before the fade ends. Give both the same duration.
Exercise
A .toast fades its opacity out over 0.5s when it gets the class gone, which also sets display: none. The toast vanishes instantly with no fade. The rule is transition: opacity 0.5s;. What is missing, and why does adding it fix the problem?
Show answer
Add display 0.5s allow-discrete, so the rule becomes transition: opacity 0.5s, display 0.5s allow-discrete;. display is a discrete property and does not transition by default, so it switches to none immediately and hides the element before the fade can be seen. With allow-discrete, the browser delays the switch to none until the end of the 0.5s transition, so the opacity fade is visible first.