Sub topic 7
Animating entry and exit with display: none
This chapter assumes you know CSS transitions, the cascade, and how display: none removes an element from rendering. It also assumes you have seen @starting-style at the top level and nested in a rule. Here you combine it with discrete property transitions to fade a panel in and out.
Why display: none breaks transitions
A transition needs a before-change style and an after-change style. An element with display: none has no computed style to start from. When you switch it to display: block, it is rendered for the first time, so the browser has nothing to animate from. Entry therefore needs @starting-style.
Exit has a different problem. display is a discrete property, so it cannot be blended. Without help, it flips on the first frame, and the element disappears before the opacity change is visible.
Allowing display to transition
The value transition-behavior: allow-discrete lets a discrete property take part in a transition. The browser still does not blend display. It chooses when the flip happens:
- When going from
noneto a visible value, the flip happens at the start, so the element can be seen while other properties animate. - When going to
none, the flip happens at the end, after the other properties finish.
You can set it as its own property, or put allow-discrete after the property name in the transition shorthand.
.panel {
opacity: 1;
transition: opacity 0.3s ease, display 0.3s allow-discrete;
@starting-style {
opacity: 0;
}
}
.panel.is-hidden {
display: none;
opacity: 0;
}The rules work together like this:
- Adding
is-hiddensetsopacity: 0anddisplay: none. Opacity fades over 0.3s. Thedisplaychange waits until that transition ends. - Removing
is-hiddenmakes the element render again. It has no before-change style, so@starting-stylesuppliesopacity: 0. - Opacity then transitions to
1, anddisplayflips at the start.
A complete working example
This page toggles the class with a button. Save it as an HTML file and open it in a current Chromium, Safari or Firefox browser.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Panel toggle</title>
<style>
.panel {
padding: 1rem;
background: #eef;
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">Show or hide</button>
<div class="panel" id="panel">Hello</div>
<script>
const panel = document.getElementById('panel');
document.getElementById('toggle').addEventListener('click', () => {
panel.classList.toggle('is-hidden');
});
</script>
</body>
</html>The JavaScript only toggles a class. It does not wait for transitionend or force a reflow.
Common errors
- Exit snaps shut.
displayis missing from thetransitionlist, orallow-discreteis missing. Both are required. - Durations disagree. If the
displayduration is shorter than the opacity duration, the element disappears before the fade finishes. Give them the same duration. - Shorthand overrides. A later
transitionshorthand resetstransition-behaviortonormal. If you set the longhand separately, declare it after the shorthand. - Entry does not fade. The
@starting-stylevalues must differ from the final values. If they match, there is nothing to animate. - Style set only on the hidden class. Put the transition on the base rule, not only on
.is-hidden. Otherwise the entry has no transition defined when the class is removed.
Exercise
A panel fades in correctly but vanishes instantly when you add the class that sets display: none and opacity: 0. Its rule has transition: opacity 0.3s. What is the fix, and why does it work?
Show answer
Add display to the transition with allow-discrete, for example transition: opacity 0.3s, display 0.3s allow-discrete. This lets display take part in the transition. When the target is none, the flip is delayed until the transition ends, so the opacity fade stays visible.