Transition to display: none with allow-discrete
Keep an element rendered during its opacity fade by transitioning display with allow-discrete, then let display flip to none at the end.
You clear a checkbox to hide a preview card. The CSS sets opacity: 0 and display: none together. Add allow-discrete to keep the card in layout until the fade finishes.
Give display the same duration as opacity
Put allow-discrete on the display item in the transition list. This makes the browser schedule a switch between the two display values. Opacity changes throughout the interval.
<!doctype html>
<title>Preview dismissal</title>
<style>
.preview {
opacity: 1;
display: block;
transition: opacity 240ms linear, display 240ms allow-discrete;
}
#show:not(:checked) ~ .preview {
opacity: 0;
display: none;
}
@media (prefers-reduced-motion: reduce) {
.preview {
transition: none;
}
}
</style>
<input id="show" type="checkbox" checked>
<label for="show">Show preview</label>
<p class="preview">This is how your label will appear.</p>Open the file and clear the checkbox. The preview fades for 240 ms, then its layout space collapses.
diagram
When the preview leaves layout
allow-discrete lets CSS transition a property that switches between values. On this exit to none, the display transition keeps block for the fade and flips at the end. If you shorten the display duration while keeping the opacity duration, the box disappears before the fade finishes.
Leave the box in the document until the fade ends
Keep the node mounted and change the class or selector that controls display. If your framework removes the node on the click, the browser has no box to fade.
Opening this example again shows the card in one step. Add a starting opacity if you need an entry fade too. For a dismissal that must work with reduced motion or a browser that skips the transition, put the hidden state in the selector itself. Clearing the checkbox then hides the preview even when there is no transition event.