CSS @starting-style Discrete Entry.
Transitioning elements into and out of display: none using modern CSS @starting-style and transition-behavior: allow-discrete.
View code
<div class="example"><button type="button" class="action">Switch state</button><div class="badge">NOW IN VIEW</div></div>:root { color-scheme: dark; font: 15px system-ui, sans-serif; color: #dbff55; }
* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; display: grid; place-items: center; overflow: hidden; background: transparent; }
.tile { display: grid; place-items: center; width: 68px; aspect-ratio: 1; border-radius: 18px; background: currentColor; }
.tile { background: #dbff55; color: #202415; font-size: 30px; }
canvas { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; }
button { font: inherit; color: #f3f1e9; background: #191919; border: 1px solid #ffffff66; padding: 8px 14px; border-radius: 24px; cursor: pointer; }
:focus-visible { outline: 2px solid #dbff55; outline-offset: -3px; }
.notice { max-width: 34ch; padding: 16px; color: #fff; }
@media (prefers-reduced-motion: reduce) { .sprite { animation-play-state: paused; } }
.example { display:grid; place-items:center; gap:16px; width:min(86%,500px); }
.badge {
display: none;
opacity: 0;
translate: 0 22px;
padding: 14px;
border: 1px solid;
border-radius: 8px;
transition: opacity .45s, translate .45s, display .45s allow-discrete;
}
.changed .badge { display: block; opacity: 1; translate: 0; }
@starting-style { .changed .badge { opacity: 0; translate: 0 22px; } }
@media (prefers-reduced-motion: reduce) { .badge { transition: none; } }const example = document.querySelector('.example');
document.querySelector('.action').onclick = () => example.classList.toggle('changed');