Sub topic 14
Entry effects with @starting-style and discrete properties
This chapter assumes you can write a basic transition, know that a transition needs a before value and an after value, and can toggle a class with JavaScript. It covers what happens when an element has no before value, and how to animate properties that do not interpolate.
The problem: no before value
A transition runs when a computed value changes between two style updates. A newly inserted element has no earlier style, so nothing changes and nothing animates. The same happens when an element goes from display: none to visible, because it had no computed style while hidden.
@starting-style
The @starting-style rule supplies a before value for an element's first style update. The browser starts from those values and transitions to the normal ones.
.dialog {
opacity: 1;
transition: opacity 300ms ease-out;
}
@starting-style {
.dialog {
opacity: 0;
}
}When .dialog is first rendered or inserted into the page, it fades from 0 to 1. You can also nest the rule inside the selector:
.dialog {
opacity: 1;
transition: opacity 300ms ease-out;
@starting-style {
opacity: 0;
}
}Both forms are equivalent. Use whichever matches your project's conventions.
Discrete properties
Some properties have no in-between values. display switches between none and block, and visibility and content-visibility also change in steps. These are discrete properties. By default they flip at the start of a transition, so you never see the fade-out.
The transition-behavior: allow-discrete setting lets a discrete property take part in a transition. For display, the browser keeps the element rendered for the whole duration when it changes from visible to none. It switches to none at the end.
.panel {
opacity: 1;
transition:
opacity 300ms ease-out,
display 300ms allow-discrete;
}
.panel.is-hidden {
opacity: 0;
display: none;
}
@starting-style {
.panel {
opacity: 0;
}
}In the shorthand, allow-discrete is written after the duration. The longhand is transition-behavior: allow-discrete.
Here is how each direction behaves:
| Direction | What the browser does |
|---|---|
| Show (remove `.is-hidden`) | `display` changes to `block` immediately. The fade starts from the `@starting-style` opacity. |
| Hide (add `.is-hidden`) | Opacity fades to 0. `display: none` applies when the 300 ms ends. |
const panel = document.querySelector('.panel');
function toggle() {
panel.classList.toggle('is-hidden');
}The display transition is listed as its own entry in the transition shorthand, so it has its own duration. Match it to the longest transition on the element, or the element may disappear early.
Common errors
- No entry animation. The
@starting-stylevalues must differ from the normal values, and the element needs atransitionon that property. A starting value alone does nothing. - Instant disappearance. Without
allow-discrete,display: noneapplies at once and cuts the fade-out. - Rule order.
@starting-stylehas the same specificity as a normal rule, so place it after the rule it modifies. - Browser support. Check current support before relying on these features. Browsers without them skip the effect and show the element immediately, which is an acceptable fallback.
Exercise
A .toast element is added to the page with JavaScript and should fade in over 250 ms. It has opacity: 1 and transition: opacity 250ms in its normal rule, but it appears instantly. What do you add to fix it?
Show answer
Add a @starting-style rule for .toast that sets opacity: 0, for example @starting-style { .toast { opacity: 0; } }. A newly inserted element has no earlier style, so the transition has no before value until this rule supplies one.
Exercise
You add .panel.is-hidden { opacity: 0; display: none; } with transition: opacity 300ms. Hiding the panel still cuts off the fade-out. Which change fixes it?
Show answer
Add display 300ms allow-discrete to the transition list. Without allow-discrete, display flips to none at the start, so the panel disappears before the opacity fade can be seen.