跪拜 Guibai
← Back to the summary

Why Your Modal Still Pops: The Three-Piece `display: none` Transition Fix, Measured in Chrome

Frontend Styles: Modal Fade In/Out, Why Your transition Doesn't Work

First, consider three scenarios — you've probably hit at least one:

These three have the same root cause: an element with display: none has no box, and without a box there are no intermediate frames. The transition of opacity from 1 to 0 happens after the element has already disappeared; the browser isn't being lazy — it's correctly following the spec.

Since Chrome 117 in 2023, there's a solution: transition-behavior: allow-discrete lets discrete properties participate in transitions, and @starting-style fills in "what the first frame looks like on entry." Both entered Baseline together on 2024-08-06. But written tutorials mostly give a single line transition: display .3s allow-discrete and say "that's enough," so readers who copy it often find only half of the animation works.

This article breaks down the three pieces one by one, and every conclusion comes with measured frame counts from Chrome 152. All tests use the same static page with Puppeteer, reading display and opacity via getComputedStyle each frame, with a uniform duration of 0.4s, sampling 500ms, and intermediate frames defined as those where opacity strictly falls between 0.02 and 0.98.

First, the table: entry and exit are two different mechanisms

Five writing styles, separated by entry and exit:

Writing style transition-behavior computed value Entry intermediate frames Exit intermediate frames
Only opacity, display switches directly normal 0 0
transition: display .4s (missing allow-discrete) normal, normal 0 0
transition: opacity .4s allow-discrete (list lacks display) allow-discrete 0 0
transition: display .4s allow-discrete, opacity .4s (no @starting-style) allow-discrete, normal 0 19
Add @starting-style { opacity: 0 } to the above allow-discrete, normal 19 19

The most notable is the fourth row: 0 entry frames, 19 exit frames. This isn't a misconfigured half; rather, each of the three components handles a different task — without the third, only the exit survives because the element is already rendered. The next section breaks down each one.

First component: put display into the transition list

Scenario

A modal with transition: opacity .4s, and display flips from none to block. Reading the first frame on close, display is already none and opacity is 0 — there are no intermediate frames, the element disappears in the first frame, and there's nowhere for a transition to happen.

Before / Now

/* Before: only opacity transitions, display switches instantly */
.modal { transition: opacity .4s; }

/* Now: include display in the list */
.modal {
  transition: opacity .4s, display .4s allow-discrete;
}

What you'll see

transition only transitions the properties listed. display is not in any implicit list by default; when you write transition: opacity .3s, display isn't even on the field, so no matter how you adjust the duration, it switches instantly.

Let's answer a common question: Does transition: all include display? Yes. In testing, .d { transition: all .4s allow-discrete } with @starting-style gives 19 entry frames and 19 exit frames, identical to listing properties individually. But I don't recommend using all in production: it pulls in layout properties, transform, and box-shadow — any one of them changing value without your notice triggers a transition. Listing them out is more predictable.

Second component: allow-discrete is a license, not an animation

Scenario

Discrete properties have no intermediate values, so they don't participate in transitions by default. allow-discrete tells the browser: handle this property's changes discretely, allow it to "hang on" to the transition.

What you'll see

display is a bit different from typical discrete properties. Properties like content-visibility flip between two values at the midpoint of the transition, but display is special-cased by direction:

Testing confirms this asymmetry. In the full three-piece group, the first entry frame already has display: flex and opacity: 0; on exit, display stays flex until 394ms (duration 0.4s) before becoming none. This detail explains everything: exit has animation because the box is allowed to stay until the end; entry needs a starting point because there's no "previous value" when the box first appears.

So why does entry "pop"? Because the first render of an element doesn't trigger a transition — the spec calls it "non-existent initial style." That's what the third piece addresses.

Third component: @starting-style tells the browser what the first frame looks like

Scenario

When a modal opens, opacity jumps from 0 to 1 with no gradual fade. Because it's the first time the element receives styles, there's no "previous value" to transition from.

Before / Now

.modal { display: none; opacity: 0; transition: opacity .3s, display .3s allow-discrete; }
.modal[open] { display: block; opacity: 1; }

/* Before: stop here, entry then pops */

/* Now: add the starting point */
@starting-style { .modal[open] { opacity: 0; } }

What you'll see

Declarations inside @starting-style only apply when the element first gets its styles, or when display changes from none to another value, serving as the starting point for the transition. The selector must match the "opened" state.

Testing is clean: removing this rule gives 0 entry frames and first-frame opacity of 1; adding it gives first-frame 0, gradually climbing to 1, with 19 intermediate frames. Exit has 19 frames in both groups — so the answer to "does exit animation need @starting-style?" is: no. On exit the element is still there; the starting point is its current computed value, with no "first render" issue.

This conclusion is useful: it means @starting-style isn't a "two-way animation switch," it only handles entry. If you find your modal "comes in but doesn't go back" or "goes back but doesn't come in," the first step is to identify which piece is missing rather than re-copying all three.

Five writing styles that look correct but quietly break animation

Writing the three pieces correctly is only the baseline. The following five don't produce errors — the console gives no warnings — but the effect is reduced or eliminated.

1. behavior written before the shorthand gets reset

/* Wrong: the shorthand resets the earlier behavior */
.x { transition-behavior: allow-discrete; transition: display .4s, opacity .4s; }

/* Correct: put it after the shorthand */
.x { transition: display .4s, opacity .4s; transition-behavior: allow-discrete; }

Testing the wrong way gives a computed transition-behavior of normal, normal — allow-discrete is gone. The consequence is deceptive: entry still has 19 frames (because @starting-style is still there and opacity transitions normally), but exit has 0 frames and the first frame's display is already none.

In other words, the symptom is exactly "entry works, exit doesn't," which is the opposite of missing @starting-style. These two symptoms are the first branching point when debugging:

Symptom What's missing
Entry pops, exit normal Missing @starting-style, or its selector doesn't match
Entry normal, exit pops allow-discrete not taking effect (reset by shorthand, or property not in list)

After moving transition-behavior after the shorthand, the computed value becomes allow-discrete and exit returns to 19 frames.

2. @starting-style placed wrong gets overridden by the target rule

/* Fails: both have specificity 0,2,0; the later .on wins */
@starting-style { .panel.on { opacity: 0; } }
.panel { transition: display .4s allow-discrete, opacity .4s; opacity: 0; }
.panel.on { opacity: 1; }

/* Fix: move @starting-style after the target rule */
.panel { transition: display .4s allow-discrete, opacity .4s; opacity: 0; }
.panel.on { opacity: 1; }
@starting-style { .panel.on { opacity: 0; } }

Testing the failing version gives 0 entry frames and first-frame opacity of 1; moving @starting-style after .panel.on gives 19 entry frames. The entry's starting value is overridden by the target rule, so "start equals end," leaving no difference to transition.

There are two fixes: place it after the target rule, or make its selector more specific (e.g., add an attribute selector). The former is more robust and readable.

3. An unrecognized value drops the entire declaration

This is a big pitfall during fallback. Suppose the browser doesn't recognize allow-discrete, and you write:

.modal { transition: display .3s allow-discrete, opacity .3s; }

Many think "not recognizing allow-discrete degrades to an opacity transition." That's wrong. Using an intentionally invalid value to simulate: transition: display .4s nonsense-value, opacity .4s, the computed values are transition-property: all, transition-duration: 0s, transition-behavior: normal — the entire declaration is discarded, including the opacity transition.

/* Correct: use @supports to layer, giving old browsers a clean opacity declaration */
.modal { transition: opacity .3s; }
@supports (transition-behavior: allow-discrete) {
  .modal { transition: opacity .3s, display .3s allow-discrete; }
}

In supporting environments, this group gives 19 entry and 19 exit frames, identical to writing all three pieces; in non-supporting environments, it gets an opacity transition and the element still shows and hides normally.

4. A base rule with display: flex overrides the "closed" state

/* Wrong: this makes a closed dialog also flex */
dialog { display: flex; }

The display: none for a closed <dialog> comes from the browser's default styles. Writing display: flex on a base selector has higher specificity and overrides it. In testing, after closing (the open attribute is removed), the element remains display: flex with height 60px, visibly stuck on the page.

To center, use the dialog's built-in margin: auto, or wrap a container inside and apply Flex or Grid to that.

5. ::backdrop doesn't follow the parent element

The modal's backdrop is also a top-layer box. If you only give it a background-color transition, it snaps to transparent on close. Testing: the group with only transition: background-color .4s reads only one background color during closing (rgba(0, 0, 0, 0)), no intermediate values.

After adding display and overlay with allow-discrete to ::backdrop, the same transition reads 23 different background colors, smoothly falling from rgba(0, 0, 0, 0.6) to transparent:

.modal::backdrop {
  background-color: rgba(0, 0, 0, 0);
  transition: background-color .3s, display .3s allow-discrete, overlay .3s allow-discrete;
}
.modal[open]::backdrop { background-color: rgba(0, 0, 0, .6); }
@starting-style { .modal[open]::backdrop { background-color: rgba(0, 0, 0, 0); } }

Using background-color rather than opacity keeps the background color and timing in the same declaration, avoiding a separate set of start/end values for opacity.

Top-layer elements have a fourth piece: overlay

Scenario

<dialog> and popover are placed in the top layer when open and removed when closed. This removal is instant, just like display, and it happens even earlier — the moment the element leaves the top layer, it falls back into the normal document flow, and its position, stacking order, and being clipped by parent all revert. Even if opacity is still transitioning, the viewer sees a semi-transparent square that has dropped to the middle of the page.

You cannot set the overlay property; only the browser can modify it. What you can do is include it in transition-property, so that the "removal from top layer" step is delayed until the transition ends.

What you'll see

Two comparison groups, both recording the element's actual rectangle and overlay computed value frame by frame after close:

Writing style Position sequence after close overlay computed value Explanation
Only display + opacity 350,340 → 350,16 → 0,0 none throughout Falls back to the top of the document flow at 125ms
Add overlay .4s allow-discrete 350,340 until 0,0 auto held until 389ms then none Position doesn't move during the entire exit

Two numbers to remember: without overlay, the element jumps to y=16 about 125ms after close (after the first frame); with it, the element stays at y=340 for the full 400ms exit. This isn't a "looks smoother" issue; it's a matter of "is the element still in its place."

Let's also correct a commonly misused criterion: don't use :modal to check if an element is still in the top layer. Testing shows :modal becomes false at the instant of close, but the group with overlay still has the element in the top layer for the entire exit. :modal matches "an open modal dialog," and once close happens it no longer matches, which is separate from top-layer removal.

<dialog> and popover behave identically on this point; both need overlay. The difference is only in the trigger: dialog uses close(), popover uses hidePopover() or clicking outside.

Does rapid clicking get stuck in an intermediate state?

During planning I listed this as a must-test: if a transition is interrupted and reversed, does the element get stuck semi-transparent?

Testing: a 0.4s transition, toggling display every 100ms for 10 times, sampling each frame. The result: opacity oscillates between 0.36 and 0.96, 67 frames land on intermediate values, and after the last toggle, at 900ms the final state is display: flex / opacity: 1, converging correctly.

The conclusion is it doesn't get stuck. CSS transitions, when interrupted, continue from the current computed value toward the new target; they don't queue or accumulate. This means during rapid clicking, the audience sees "oscillation," not "jamming." If this jitter affects the experience, the thing to adjust isn't the transition syntax but the duration itself: make the exit shorter (say 0.2s) so that by the time the next click arrives, the element has likely landed.

What does get stuck is on the JS side: if you do something in transitionend (unmounting a component, unlocking scroll, removing a class), rapid clicks may cause the event to fire fewer times because the transition is interrupted. This is unrelated to CSS, but both problems often appear together in the same modal — CSS looks fine, but on the JS side the scrollbar is locked.

Compatibility: four capabilities didn't enter Baseline together

This is the easiest section to get wrong, because the four capabilities entered Baseline at different times:

Capability Chrome Edge Firefox Safari Baseline
transition-behavior: allow-discrete 117 117 129 17.4 2024-08-06 Newly available
@starting-style 117 117 129 17.5 2024-08-06 Newly available
display property itself is transitionable 117 117 Unlabeled 18 —
overlay property 117 117 Not supported Not supported Not entered

The key difference among the first three rows: "syntax recognized" and "display actually transitions" are two different things. Safari 17.4 had transition-behavior, 17.5 had @starting-style, but combining them to transition display only works in Safari 18; Firefox 129 supports both syntaxes, but display transition is still not labeled as supported.

I verified these two points from two sources: in BCD 8.1.9 (2026-11-17), css.properties.display.transitionable_display only marks Chrome 117, Edge 117, and Safari 18; another English review from mid-2026 agrees. Both are secondary sources, so I mark them as "to be verified on real devices" rather than definitive. If you truly rely on this in old Safari or Firefox, run a quick test rather than trust the table.

overlay is simpler: only Chromium has it, MDN still marks it experimental, and it hasn't entered Baseline. It's classic progressive enhancement — when unsupported, the element still opens and closes, it just falls back to the document flow on exit.

This yields a fallback strategy: only the animation can be degraded, not usability. When the three pieces fail, the element still shows and hides normally, which the @supports layer covers (see the third point in the previous section). The only hard pitfall to avoid is the "whole declaration dropped" issue, which also removes the opacity transition that could otherwise work.

A copyable modal

Putting all constraints together yields a version that's unadorned but directionally correct:

.modal {
  /* Closed state: display comes from the browser's default, only visibility is written here */
  opacity: 0;
  transition: opacity .3s, display .3s allow-discrete, overlay .3s allow-discrete;
  transition-behavior: allow-discrete; /* Placed after the shorthand, won't be reset */
}
.modal[open] { opacity: 1; }
@starting-style { .modal[open] { opacity: 0; } }

.modal::backdrop {
  background-color: rgba(0, 0, 0, 0);
  transition: background-color .3s, display .3s allow-discrete, overlay .3s allow-discrete;
}
.modal[open]::backdrop { background-color: rgba(0, 0, 0, .6); }
@starting-style { .modal[open]::backdrop { background-color: rgba(0, 0, 0, 0); } }

Three details: the base rule doesn't write display, preserving the browser's default dialog:not([open]) { display: none }; transition-behavior is on its own line and after the shorthand, managing both display and overlay; ::backdrop gets its own set, don't expect it to follow the parent.

If the project also runs on unsupported environments, wrap it in a layer:

@supports not (transition-behavior: allow-discrete) {
  .modal { transition: opacity .3s; }
  .modal::backdrop { transition: background-color .3s; }
}

Judgment: missing one piece is worse than not writing it

Putting it all together, it boils down to: entry relies on @starting-style to provide a starting point, exit relies on display and overlay with allow-discrete to hold off the endpoint, and the two directions are separate systems that must be verified independently.

In day-to-day work, my check order is:

  1. First look at direction: is entry or exit snapping? For entry, check @starting-style and its selector order; for exit, check if transition-behavior was reset by the shorthand.
  2. Then check the list: is display in transition-property, and is overlay in there (only for top-layer elements)?
  3. Finally check fallback: is there an @supports layer to ensure old browsers get a clean declaration containing only interpolable properties?
  4. Only doing entry and not exit is worse than not doing it at all. One-way animation makes "open" look refined and "close" look cheap; two different feels in the same component are more harmful than both being blunt.

As for compatibility, transition-behavior and @starting-style are newly available, so you can ship them as "nice to have, harmless without." overlay and "display actually transitions" should be decided based on real-device testing of your target browser, not just CSS.supports returning true — on your local Chrome, CSS.supports('overlay', 'auto') is true, but that only proves "this machine can parse it," and has nothing to do with Firefox or Safari.

Finally, back to the original modal. Its problem isn't "the animation isn't good enough," but that a task requiring three coordinated pieces was treated as one: transition controls duration, display needs a license, the first frame needs a starting point, and top-layer elements also need something to hold them from leaving early. When all four are in place, display: none becomes a truly transitionable value.

Comments

Top 1 from juejin.cn, machine-translated. The original thread is authoritative.

组合搭配函数

The three-piece set plus overlay is explained thoroughly; the root cause of modal exit jumping was right here. Following the troubleshooting sequence at the end of the article step by step can save you from several pitfalls.