Sub topic 13
Pitfalls and debugging in DevTools
Entry and exit animations with @starting-style fail silently. When the cascade, the transition list, or the DOM timing is wrong, the element just appears or disappears instantly, and the console shows no error. This chapter gives you a checklist for finding the cause and a set of DevTools techniques for seeing what the browser is doing.
What this chapter assumes
You should already know how @starting-style, transition-behavior: allow-discrete, and the display and overlay transitions work. You should also be able to read a cascade (specificity and source order) and know the basics of the dialog element and the popover attribute. This chapter does not reteach those features. It covers how they break.
A working model for debugging
Every failed entry or exit animation comes down to one of three questions.
- Does the browser have a before-change style to start from? For an entry, that is the
@starting-stylevalue that won the cascade. - Is there a transition that covers the property, taken from the after-change style?
- Can the property be interpolated? Discrete properties such as
displayandoverlayneedallow-discrete.
The browser takes the transition definition (property, duration, timing function) from the after-change style. For an entry, that is the normal visible style. For an exit, it is the hidden style. This is the source of several pitfalls below.
Pitfall 1: The starting style loses the cascade
Rules inside @starting-style take part in the normal cascade. The at-rule adds no specificity and no extra priority. If a normal rule with the same specificity comes later in the source, its value wins, and the element's starting style equals its final style. No transition runs.
/* Broken: the later .card rule overrides the starting value */
@starting-style {
.card { opacity: 0; }
}
.card {
opacity: 1;
transition: opacity 0.4s ease;
}There are two fixes. The first is to put the @starting-style block after the main rule. The second, which is more reliable, is to nest it inside the rule, as in the earlier chapters. A nested @starting-style comes after the parent's own declarations in the cascade.
Specificity causes the same problem. If .card.featured { opacity: 1 } is more specific than the starting rule .card { opacity: 0 }, the featured card never gets its starting value. Give the starting style a selector with equal or higher specificity, or nest it inside the more specific rule.
Mixed properties are a related trap. If the main rule animates translate but the starting style sets transform, the two values belong to different properties. Nothing interpolates between them. Use the same property in both places.
Pitfall 2: The transition is declared on only one state
Because the transition definition comes from the after-change style, the state you are moving to must carry the transition.
/* Broken: closing has no transition, so it is instant */
dialog {
opacity: 0;
}
dialog[open] {
opacity: 1;
transition: opacity 0.3s;
}When the dialog closes, the after-change style is the plain dialog rule. It has no transition, so the dialog vanishes. Declare the transition on the base selector so that both directions have it. If you want different timings for entry and exit, put different durations on each state on purpose.
Pitfall 3: `display` is missing or misconfigured
An exit that hides the element instantly almost always has one of these causes.
displayis not in the transition list. A plaintransition: opacity 0.3sflipsdisplay: noneon the first frame.allow-discreteis missing from thedisplayentry.transition-behavior: allow-discretewas set before atransitionshorthand. The shorthand resetstransition-behaviortonormal. Put the longhand after the shorthand, or putallow-discreteinside the shorthand.- The
displayduration is shorter than theopacityduration, so the element disappears before the fade ends.
Here is the broken and fixed version of a show/hide panel.
/* Broken: display flips immediately */
.panel {
opacity: 1;
transition: opacity 0.3s ease;
}
.panel.is-hidden {
display: none;
opacity: 0;
}/* Fixed */
.panel {
opacity: 1;
transition: opacity 0.3s ease, display 0.3s allow-discrete;
@starting-style {
opacity: 0;
}
}
.panel.is-hidden {
display: none;
opacity: 0;
}The display transition flips at the start when going from none to a visible value, and at the end when going to none. That is why both the fade-in and the fade-out are visible.
You can also write transition: all 0.3s allow-discrete. The all keyword includes display and overlay, but it also animates every other property that changes. Use it only when you want that.
Pitfall 4: Top-layer elements without `overlay`
A popover or modal dialog lives in the top layer while it is open. When it closes, the browser removes it from the top layer at once, even though display is still transitioning. The element then drops behind other content mid-fade or disappears. Add overlay 0.3s allow-discrete to the transition list of every top-layer element and its ::backdrop, with the same duration as the other entries.
Two details help here. You cannot set overlay yourself. It is a read-only state that appears only inside transitions. Also, support for overlay has trailed the other features in some browsers. Check a current support table, and make sure the UI still works when the exit simply happens instantly.
Pitfall 5: Removing the element from the DOM
The display trick needs the element to stay in the document. If your script calls card.remove() straight away, there is no exit animation. Add a class that starts the transition, wait for the transitions to finish, and then remove the element.
function removeCard(card) {
card.classList.add('is-leaving');
Promise.allSettled(
card.getAnimations().map((animation) => animation.finished)
).then(() => card.remove());
}.card.is-leaving {
opacity: 0;
transform: translateY(1rem);
}This code waits on getAnimations() instead of listening for transitionend. A transitionend listener never fires if no transition runs, for example when the property was not in the list or the duration is 0s. The element would then stay in the DOM forever. With getAnimations(), an empty list resolves at once and the element is removed anyway.
Pitfall 6: Reduced motion rules that do not apply
A prefers-reduced-motion block can fail to take effect for two reasons.
- A later rule with equal specificity sets
transitionagain and overrides your shortened duration. Place the media query last in the stylesheet. - The block sets
transition: none. That removes thedisplayandoverlaytransitions too, and any script waiting fortransitionendhangs. Keep the transition and settransition-duration: 0.01ms, as in the earlier chapter. Thedisplayflip still works and the events still fire.
Debugging in DevTools
The starting style exists for roughly one frame, so it is hard to catch by eye. These techniques make it visible.
- Slow it down. Temporarily set the duration to
5sin the Styles pane, or set the playback rate of the Animations panel to 10%. Chromium browsers and Firefox both have an animations inspector that records transitions and shows their properties and timing. - Read the rules. In the Styles pane, check that your
@starting-styleblock is listed and not struck through. A struck-through value lost the cascade (Pitfall 1). Chromium-based DevTools list the block with its own@starting-styleheader. Other browsers may show it differently. - Query running animations. The Console shows exactly what the browser started.
document.getAnimations().map((a) => ({
target: a.effect.target.className,
property: a.transitionProperty,
state: a.playState,
}));If the list has no entry for opacity, the transition never started. Look at Pitfalls 1 to 3. If display or overlay is missing from the list while opacity is present, you forgot allow-discrete or the property in the list.
- Check the computed values. The Computed pane shows the transition properties and
transition-behaviorthat applied to the element in its current state. Inspect a hidden element too, since the exit transition comes from that state. - Trigger the state reliably. For popovers, call
$0.togglePopover()on the selected element. For dialogs, call$0.showModal()and$0.close(). This repeats the test without using the page's own script.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Entry appears instantly | Starting style lost the cascade | Nest `@starting-style` or move it after the main rule |
| Entry works, exit is instant | `display` missing from `transition` or no `allow-discrete` | Add `display <time> allow-discrete` |
| Closing has no animation | Transition only on the open state | Move `transition` to the base rule |
| Dialog or popover jumps at close | No `overlay` transition | Add `overlay <time> allow-discrete` |
| Backdrop snaps | `::backdrop` has no own transition or starting style | Give it both |
| Element never leaves the DOM | Script waits for `transitionend` that never fires | Wait on `getAnimations()` |
| Reduced motion has no effect | Overridden by a later rule | Place the media query last |
Exercise
A .toast fades in correctly but disappears instantly when the hidden class is added. The CSS is .toast { opacity: 1; transition: opacity 0.3s; @starting-style { opacity: 0; } } and .toast.hidden { display: none; opacity: 0; }. What is the cause and what is the fix?
Show answer
The transition list lacks display, so display: none applies on the first frame and the fade-out is never seen. Change the transition to transition: opacity 0.3s, display 0.3s allow-discrete;. The entry already works because @starting-style is nested in the main rule.
Exercise
You wrote @starting-style { .card { opacity: 0; } } at the top of your stylesheet and .card { opacity: 1; transition: opacity 0.4s; } below it. Cards appear without a fade. Give one reason and two ways to fix it. Then name one console call that confirms the transition did not start.
Show answer
Both rules have the same specificity, and the later .card rule wins, so the starting style is also opacity: 1. Fix it by nesting @starting-style inside .card, or by moving the top-level block after the .card rule. Calling document.getAnimations() right after inserting a card returns no entry with transitionProperty of opacity.