Sub topic 13
Pitfalls and debugging in DevTools
This chapter assumes you know CSS selectors and the cascade, how transition works, and the basics of display, dialog and popover. It covers the mistakes that most often stop @starting-style animations from running, and how to find them in browser DevTools.
The working model in one paragraph
A transition needs a before-change style and an after-change style. On the first style update of a new element, or of an element coming out of display: none, there is no before-change style. @starting-style supplies one. To animate an exit, display (and overlay for top-layer elements) must be transitioned with allow-discrete, so the element stays rendered until the exit finishes.
Pitfalls on entry
The starting style loses in the cascade
@starting-style adds no specificity of its own. A rule inside it competes like any other rule, so a later or more specific rule can override it.
.card {
opacity: 1;
transition: opacity 0.4s;
}
@starting-style {
.card {
opacity: 0;
}
}
.list .card {
opacity: 1;
}The last rule has higher specificity than .card, and it wins over the starting style. The card appears with no fade. Raise the starting rule to match, for example .list .card inside @starting-style, or nest it in the rule you want to animate.
The property is not in `transition`
A starting value only helps if the property is transitioned. If .card sets opacity: 1 but its transition lists only transform, opacity jumps. The same applies when a shorthand later in the cascade resets transition.
The starting style does not apply to later changes
A starting style is used only on the first style update. After that, ordinary before-change styles apply. If a card fades in once but not on a later display change, the cause is usually that display is not transitioned.
Pitfalls on exit
Missing `allow-discrete`
display is a discrete property. Without allow-discrete, it flips at the start of the transition and the element disappears at once. Use the transition shorthand form or transition-behavior:
.panel {
transition: opacity 0.3s, display 0.3s allow-discrete;
}Mismatched durations
If display has a shorter duration than opacity, the element is hidden before the fade ends. Give both the same duration and timing.
Missing `overlay`
Popovers and modal dialogs live in the top layer. When they close, they leave the top layer at once unless overlay is transitioned with allow-discrete. The result is an exit that is clipped or drawn under other content.
Debugging in DevTools
- Open the Elements panel and select the element. Look at the Styles pane to see whether your starting rule appears and whether it is struck through. A struck-through rule is overridden.
- Check the Computed pane. Confirm that
transition-propertyincludes the properties you expect, includingdisplayandoverlay. - Open the Animations panel, then trigger the change. A transition that runs appears as a row. If no row appears, no transition started, so look for the entry pitfalls above.
- Slow the animation to 10% in the Animations panel. This makes cascade problems and mismatched durations easy to see.
- Toggle a class or the
openattribute by hand in the Elements panel. This separates CSS problems from JavaScript timing problems. - For a popover or dialog, check the Elements panel for the
#top-layerentry. It stays visible during a working exit, then disappears when the transition ends.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| No fade on entry | Starting style overridden | Match specificity or order |
| Entry jumps | Property missing from `transition` | Add the property |
| Exit disappears instantly | No `display` transition | Add `display` with `allow-discrete` |
| Exit is clipped | No `overlay` transition | Add `overlay` with `allow-discrete` |
| Works only on your machine | Reduced motion setting | Test with the setting off |
Exercise
A modal dialog fades in correctly but vanishes instantly when closed. Its CSS transitions opacity 0.3s. Name two properties you should add to the transition, and the keyword each needs.
Show answer
Add display 0.3s allow-discrete and overlay 0.3s allow-discrete. Without display, the dialog is hidden immediately. Without overlay, it leaves the top layer immediately.
Exercise
A .card has a correct @starting-style block, but a later rule .list .card { opacity: 1; } is present and the card does not fade in. What is wrong, and how do you confirm it in DevTools?
Show answer
The later, more specific rule overrides the starting style. In the Styles pane, the starting rule shows as struck through. Fix it by matching the specificity inside @starting-style or by removing the conflicting rule.