Sub topic 7
Animating entry and exit with display: none
This chapter assumes you know how CSS transitions work (transition-property, duration, timing function). It also assumes you know what display: none does and can toggle a class with JavaScript. You should have met @starting-style in the earlier chapters. Here it is combined with display to animate both directions of a show/hide panel.
Why display: none blocks both directions
A transition needs two computed styles for the same element: a before-change style and an after-change style. display: none breaks that in two ways:
- Entry. An element with
display: nonehas no rendered box, so it keeps no computed style from the previous frame. When the class is removed, the element is rendered for the first time. There is no before-change style, so the browser applies the final values at once. - Exit.
displaywas historically not animatable. When a class setsdisplay: none, the element is removed from rendering on the same frame. A fade-out onopacitynever gets to run, because the element is already gone.
Two features fix this. @starting-style supplies the missing before-change style for entry. transition-behavior: allow-discrete lets display take part in a transition, which delays the switch to none for exit.
Discrete properties and allow-discrete
Most properties are interpolated. A width from 0 to 100px passes through every value in between. A discrete property such as display has no in-between values. Browsers flip it from the start value to the end value at some moment. By default, discrete properties do not transition at all, so the flip is instant.
Setting transition-behavior: allow-discrete on the property opts in. You can write it as a longhand, or inside the transition shorthand as shown in this chapter:
.panel {
transition: opacity 0.3s ease, display 0.3s allow-discrete;
}For most discrete properties, the flip happens at 50% of the duration. display is a special case. When either end of the transition is none, the other value is used for the entire duration. The element therefore stays rendered while the other properties animate.
| Direction | When `display` flips | What you see |
|---|---|---|
| `block` to `none` (exit) | At the end (100%) | Element stays rendered while `opacity` fades out, then disappears |
| `none` to `block` (entry) | At the start (0%) | Element is rendered immediately, so `opacity` can animate from the starting style |
content-visibility follows the same rule. It is useful for other collapsing patterns, but this chapter stays with display.
Putting entry and exit together
This is the panel from the chapter's example, as a complete page you can open in a current Chromium, Safari or Firefox build:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated panel</title>
<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;
}
</style>
</head>
<body>
<button id="toggle">Toggle panel</button>
<div class="panel">Hello. I fade in and out.</div>
<script>
const panel = document.querySelector('.panel');
document.getElementById('toggle').addEventListener('click', () => {
panel.classList.toggle('is-hidden');
});
</script>
</body>
</html>Trace the two clicks to see which rule does the work.
Hiding (first click). The class is-hidden is added.
- The after-change style is
display: none; opacity: 0. The before-change style isdisplay: block; opacity: 1. - Both
opacityanddisplayhave a transition declared. The transition list that counts is the one on the after-change style, and.panel.is-hiddenstill matches.panel, so the list is inherited. opacityfades from 1 to 0 over 0.3s.displaykeeps its old value,block, for the same 0.3s, then switches tonone.
Showing (second click). The class is removed.
- The element changes from
display: nonetodisplay: block, so it is newly rendered. Its before-change style comes from@starting-style:opacity: 0. opacitytransitions from 0 to 1.displayflips toblockat the start of the transition.
Why the duration must match
The display transition has no visible effect of its own. It only holds display at block for as long as it runs. If display is given 0.1s and opacity takes 0.3s, the element vanishes after 0.1s, and the fade is cut short. If display is given 0.5s, the element stays rendered but invisible for 0.2s after the fade finishes. It can still capture clicks during that time.
A practical pattern is to put the duration in a custom property:
.panel {
--dur: 0.3s;
transition:
opacity var(--dur) ease,
translate var(--dur) ease,
display var(--dur) allow-discrete;
}Every property that moves uses the same variable as display, so they cannot drift apart.
Animating more than opacity
Any animatable property can join the effect. A slide-and-fade needs the property in three places: the base state, the starting style and the hidden state.
.panel {
opacity: 1;
translate: 0 0;
transition:
opacity 0.3s ease,
translate 0.3s ease,
display 0.3s allow-discrete;
@starting-style {
opacity: 0;
translate: 0 -0.5rem;
}
}
.panel.is-hidden {
display: none;
opacity: 0;
translate: 0 -0.5rem;
}The hidden state repeats the starting values so that exit mirrors entry. If you leave out translate in .is-hidden, the panel fades out in place, then slides in on the next show. That asymmetry is a common reason an animation "feels wrong".
Interrupted transitions
Users click quickly. Suppose the panel is half-way through fading out and the class is removed again.
- The element is still rendered, because
displayhas not flipped yet. This is not an entry, so@starting-styleis not used. - The browser reverses the running transition from its current opacity back toward 1.
- The pending flip to
noneis cancelled.
This works because display and opacity are in the same transition list with the same duration. If only opacity were transitioned and display was set by a timer, you would have to cancel that timer yourself.
Behavior during the exit
For the length of the exit, the panel is still a live element. It can be clicked, it can hold focus, and it is still in the accessibility tree. Only when display becomes none does it leave all three. Two small additions reduce surprises:
.panel.is-hidden {
display: none;
opacity: 0;
pointer-events: none;
}pointer-events: none stops stray clicks during the fade. Focus is harder. If a focused control sits inside the panel, it loses focus only at the end of the exit. If your interface depends on moving focus when something closes, move it in your own script when you add the class, rather than waiting for the transition.
Reduced motion
Entry and exit effects are often large movements, so respect the user's setting. Make the transitions near-instant instead of removing them, so the display flip still happens:
@media (prefers-reduced-motion: reduce) {
.panel {
transition-duration: 0.01ms;
}
}A duration of 0.01ms keeps all three parts working: the final state is reached, the display change is applied, and transitionend still fires. Setting transition: none would also work for this simple panel, but the near-zero duration is safer when scripts wait on transitionend. A longer-term alternative is to keep the fade and drop only the movement.
Common mistakes
displaymissing from the transition list. The fade-in works, but the exit is instant. Entry never neededdisplayin the list, so this error only shows on close.allow-discretemissing. Writingdisplay 0.3swithout the keyword leavesdisplaynon-transitioning.- The shorthand reset. A later rule such as
transition: opacity 0.2sreplaces the whole list, includingdisplay. Check that the active rule still lists it. - No
@starting-style. The exit works, but the next entry pops in. - Starting style overridden. A rule of the same specificity placed later in the source can win over the starting style for the first frame.
- Mismatched durations. See the section above.
In DevTools, toggle the class and open the Animations panel. If the entry shows no transition, look at the computed value of opacity on the first frame. If the exit is cut short, check the display entry in the transition list.
Exercise
A panel has transition: opacity 0.3s; and a .is-hidden class with display: none; opacity: 0. Adding the class hides it instantly. What two changes are needed to get a fade-out, and what is still missing for a fade-in on removal?
Show answer
Add display 0.3s allow-discrete to the transition list, so display stays block until the opacity fade ends. The fade-out then works. For the fade-in, the element needs a before-change style, because it goes from display: none to rendered. Add @starting-style { opacity: 0; } to the .panel rule.
Exercise
You set transition: opacity 0.5s, display 0.2s allow-discrete; on a panel. Describe what the user sees on exit and on entry.
Show answer
On exit, display flips to none after 0.2s, so the panel disappears when the 0.5s fade is only 40% done. The fade is cut short. On entry, display flips at the start, and opacity animates from the starting style over the full 0.5s, so entry looks correct. Fix it by giving display the same 0.5s duration.