← All posts

CSS transition not working with display: none? How to animate elements in and out (visibility, @starting-style, dialog)

Why a fade doesn't play on an element switching from display: none to block, and three reliable ways to animate menus, panels and modals: visibility, @starting-style with allow-discrete, and a little JavaScript as a fallback.

CSS transition not working with display: none? How to animate elements in and out (visibility, @starting-style, dialog)

Key points

  • display isn’t animatable like other properties: when the element appears, the browser has no starting state to animate from, so there’s no fade.
  • The battle-tested fix combines opacity and visibility with a delay; the modern one uses @starting-style and transition-behavior: allow-discrete.
  • A merely transparent element is still clickable and keyboard-focusable: hide it properly once the animation ends.

This one comes up on nearly every front-end build: you want a menu, a filter panel or a modal to appear with a gentle fade. You add a transition on opacity, toggle a class that switches the element from display: none to display: block… and nothing animates. It pops in and vanishes instantly. That isn’t a bug in your code or the browser; it’s simply how display behaves. Here’s why, and the three approaches we pick between depending on context, along with the accessibility traps that come with them.

Why the transition never runs

A CSS transition interpolates between two computed values: the state before the style change and the state after. But an element with display: none isn’t rendered at all. It has no box, no position, and its opacity has never actually been “on screen”.

.panel { display: none; opacity: 0; transition: opacity .3s; }
.panel.open { display: block; opacity: 1; } /* pops in instantly, no fade */

When the class is added, the browser computes styles in one pass: the element becomes display: block and opacity: 1 at the same moment. There’s no rendered state with opacity: 0 to animate from. Closing is the mirror image: display: none applies immediately and the element is gone before the fade has a chance to play.

Historically display was called “not animatable”. Recent specs treat it as a “discrete” property: it can take part in a transition, but it flips from one value to the other with no in-between, and only if you explicitly allow it. That’s the basis of the modern fix, but let’s start with the one that works everywhere.

The battle-tested fix: opacity plus visibility

Stop using display to hide the element and combine opacity with visibility instead. visibility has one decisive advantage: an element with visibility: hidden can’t be clicked, can’t receive keyboard focus and isn’t announced by screen readers. And it can be delayed inside a transition.

.panel {
  opacity: 0;
  visibility: hidden;
  transform: translateY(-8px);
  transition: opacity .25s ease, transform .25s ease, visibility 0s linear .25s;
}
.panel.open {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity .25s ease, transform .25s ease, visibility 0s;
}

The trick lives in the two transition declarations. When opening, visibility switches to visible with no delay, then the fade plays. When closing, the fade plays first and visibility only switches to hidden after 0.25s, at the end of the animation.

The limitation: the element still takes up its space in the layout, as if it were there. For a dropdown or a modal positioned absolute or fixed, that’s fine. For a block in the normal flow (something that pushes content down), animate it differently or use one of the next approaches.

The trap we find most often in audits is an element hidden with opacity: 0 alone. It’s invisible but very much present. It swallows clicks meant for what’s underneath, and pressing Tab moves focus into links nobody can see. If you keep only one rule from this article, keep this one: a hidden element must be genuinely unreachable, not just transparent.

The modern fix: @starting-style and allow-discrete

Two recent CSS additions solve the problem at the source. transition-behavior: allow-discrete (or the allow-discrete keyword in the transition shorthand) lets display take part in the transition: on close, the switch to none waits until the duration has elapsed. @starting-style defines the starting state to use when the element has just been rendered, which is exactly what was missing on open.

.panel {
  display: none;
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity .25s, transform .25s, display .25s allow-discrete;
}
.panel.open {
  display: block;
  opacity: 1;
  transform: none;
}
/* Animation start point when the element goes from display: none to block */
@starting-style {
  .panel.open {
    opacity: 0;
    transform: translateY(-8px);
  }
}

With this, the element really leaves the layout when closed, and both directions animate. Current browsers support it; check caniuse.com against your audience. The fallback is graceful: a browser that doesn’t understand these rules simply shows and hides the element without animation, which remains perfectly usable. That’s why we’re happy to ship it in production as a progressive enhancement.

The same idea applies to <dialog> elements and popovers, with one more property: overlay. It controls whether the element sits in the page’s “top layer”, above everything else; including it in the transition keeps the element on top while it animates out.

dialog {
  opacity: 0;
  scale: .96;
  transition: opacity .2s, scale .2s,
              display .2s allow-discrete, overlay .2s allow-discrete;
}
dialog[open] { opacity: 1; scale: 1; }
@starting-style {
  dialog[open] { opacity: 0; scale: .96; }
}

dialog::backdrop {
  background: rgb(0 0 0 / 0);
  transition: background .2s, display .2s allow-discrete, overlay .2s allow-discrete;
}
dialog[open]::backdrop { background: rgb(0 0 0 / .5); }
@starting-style {
  dialog[open]::backdrop { background: rgb(0 0 0 / 0); }
}

@media (prefers-reduced-motion: reduce) {
  dialog, dialog::backdrop { transition: none; }
}

Note the prefers-reduced-motion query: some people ask their operating system to cut down on motion because animations make them feel sick or break their concentration. Removing the transition in that case costs nothing, and we always include it.

The JavaScript fallback, and when you need it

There are still cases CSS alone doesn’t cover: a component that must be hidden with the hidden attribute for a third-party library, support for older browsers with real removal from the layout, or something to trigger once the animation finishes (returning focus to the button that opened the panel, say). The pattern: remove hidden, wait for the browser to render the element, then add the class that triggers the transition; on close, remove the class and set hidden again when the transition ends.

function closePanel(el) {
  el.classList.remove('open');
  el.addEventListener('transitionend', function done(e) {
    if (e.propertyName !== 'opacity') return; // once only, not for every property
    el.hidden = true;
    el.removeEventListener('transitionend', done);
  });
}

function openPanel(el) {
  el.hidden = false;
  requestAnimationFrame(() => {
    requestAnimationFrame(() => el.classList.add('open'));
  });
}

Two details that save hours of debugging. The double requestAnimationFrame guarantees at least one frame has been painted in the starting state; with a single call, some browsers batch both changes and the animation is skipped. And transitionend fires once per animated property: without the propertyName filter your code runs several times. Also handle the case where no transition runs at all (reduced motion, zero duration): transitionend then never fires and the element would never be hidden. A fallback timer, or checking the computed duration first, takes care of it.

FAQ

Why not animate max-height to reveal a block?

It’s a common technique but an imprecise one: you have to pick an arbitrary maximum height, and the animation feels too slow or too fast depending on the real content. For accordions, grid-based techniques or interpolate-size give a cleaner result.

Do these animations slow the site down?

Not if you animate opacity and transform, which the browser can handle without recalculating layout. Avoid animating width, height, top or margin on large elements, which forces recalculation on every frame.

Need an interface that feels smooth while staying accessible and lightweight? We build your designs with modern, maintainable CSS and no heavyweight libraries. Let’s talk.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →