Animate display: none in CSS (No JavaScript)
Animating display none without workarounds
You couldn't transition display. Workarounds were JS that set display: none after transition end, or visibility plus opacity and pointer-events. Now discrete properties can participate.
The old hack vs the modern fix
View demo.panel {
transition: opacity .2s;
}
.panel.hidden {
opacity: 0;
visibility: hidden;
}
/* JS: after the transition, set display: none and pointer-events */
el.addEventListener('transitionend', () => {
el.style.display = 'none';
});.panel {
transition: opacity .2s, display .2s allow-discrete;
}
.panel.hidden {
opacity: 0;
display: none;
}
@starting-style {
.panel { opacity: 0; }
}Transition behavior browser support
Live demo
Why the modern way wins
Real display
Animate to display: none. No need to keep the element in the layout with visibility.
No transitionend
Browser handles the discrete flip at the right time. No JS listening for transitionend.
Overlay too
overlay is another discrete property. Useful for popovers and modals.
- Lines Saved
- 11 → 10
- No JS for hide
- Old Approach
- visibility + JS
- transitionend then display:none
- Modern Approach
- allow-discrete
- Animate display and overlay
How it works
display isn't interpolable, so it was left out of transitions. To hide after a fade you either listened for transitionend in JS and then set display: none, or you kept the element in layout with visibility: hidden and pointer-events: none so it didn't block clicks.
transition-behavior: allow-discrete lets discrete properties like display and overlay participate. The browser runs the interpolable transition (e.g. opacity), then flips the discrete value at the right moment. No JS. Add @starting-style so the element also fades in when it comes back from display: none.