Sub topic 14
Entry effects with @starting-style and discrete properties
This chapter assumes you can write a basic transition, you know that a transition needs a start value and an end value, and you can toggle a class or open a <dialog> with JavaScript. It covers two problems that plain transitions cannot solve. The first is fading in an element the moment it is created or shown. The second is animating display, which has no in-between values.
Why a plain transition fails here
A transition runs when the browser sees a computed value change between two style updates. Two situations give it nothing to compare.
- A new element. When you insert an element into the DOM, its first style update has no earlier value. The element simply appears at its final style.
- A change involving
display: none. An element withdisplay: nonehas no computed styles for its box. Going fromnonetoblocktherefore has no before state to animate from. Going back tononeremoves the element instantly, so there is no time to fade it out.
Developers used to work around this with a forced reflow, a requestAnimationFrame double-hop, or a setTimeout that waited for the exit animation before hiding the element. Two CSS features now replace those workarounds: @starting-style for entry, and transition-behavior: allow-discrete for display.
@starting-style: defining the "before" state
@starting-style gives an element a set of values to treat as its previous style on the first style update. The browser transitions from those values to the element's normal values.
.toast {
opacity: 1;
transform: none;
transition: opacity 300ms ease-out, transform 300ms ease-out;
}
@starting-style {
.toast {
opacity: 0;
transform: translateY(12px);
}
}When a .toast is inserted, it starts at opacity: 0 and moves to opacity: 1 over 300 ms. The same applies when an element goes from display: none to a displayed state, because it has no previous style in that case either.
You can also nest the rule inside the element's own rule when you use CSS nesting:
.toast {
opacity: 1;
transition: opacity 300ms ease-out;
@starting-style {
opacity: 0;
}
}Rules that cause common mistakes
- Starting values apply only once. The
@starting-stylevalues are used for the first style update only. After that, normal transition rules apply. Hovering or class changes later do not use them. - Order and specificity still matter. A
@starting-stylerule has the same specificity as the equivalent normal rule. If your normal rule is more specific or comes later, it wins and the entry effect silently disappears. Place the starting-style block after the rule it modifies and keep the selectors equal. - A transition must be declared.
@starting-styleonly supplies the start value. Without atransitionon the properties involved, the element still jumps. - Declare the transition on the normal rule. Putting
transitiononly inside@starting-styleis a common error. The transition belongs on the element's regular state.
Discrete properties and interpolation
Most properties you animate, such as opacity, transform and background-color, are continuous. The browser computes values between the start and end. Some properties are discrete: they have no meaningful midpoint. Examples are display, content-visibility, overlay (which tracks whether an element is in the top layer) and many keyword-valued properties.
By default, a discrete property does not transition. It flips between its two values at the 50% point of an animation, and only when a transition is allowed to run on it. For transitions, it flips immediately, because the property is not in the transition list by default.
The transition-behavior property controls this. Its values are:
| Value | Meaning |
|---|---|
| `normal` | Only continuous properties transition. Discrete properties change instantly. This is the default. |
| `allow-discrete` | Discrete properties also take part in the transition and flip at the point set by the property's rules. |
You can set it as a longhand, or write allow-discrete inside the transition shorthand for a single property:
.panel {
transition: opacity 250ms ease, display 250ms allow-discrete;
}How display flips
For display, allow-discrete uses a special rule so the element stays visible during the whole transition.
- Going from
noneto a visible value,displayflips at the start (0%). The element is rendered for the entire duration, so the other properties can animate. - Going from a visible value to
none,displayflips at the end (100%). The element stays rendered until the exit animation finishes, then is removed.
content-visibility follows the same pattern. Other discrete properties use the default 50% flip.
A complete exit and entry for display: none
This example fades a panel in and out when you toggle a class. The panel is display: none while hidden.
<button id="toggle" type="button">Toggle panel</button>
<div class="panel" id="panel">Hello</div>
<script>
const panel = document.getElementById('panel');
document.getElementById('toggle').addEventListener('click', () => {
panel.classList.toggle('is-open');
});
</script>
<style>
.panel {
display: none;
opacity: 0;
transition:
opacity 300ms ease-out,
display 300ms allow-discrete;
}
.panel.is-open {
display: block;
opacity: 1;
}
@starting-style {
.panel.is-open {
opacity: 0;
}
}
</style>The three parts each have a job:
opacitytransition. It does the visible fading in both directions.display 300ms allow-discrete. It delays the switch tononeuntil the fade ends, so the exit is visible. On the way in, it makes the element render from the first frame.@starting-style. It supplies theopacity: 0start value, because the element had no rendered style before it opened.
Note that the base .panel rule already sets opacity: 0, which handles the exit. The starting-style rule handles the entry.
Dialogs and popovers: the overlay property
Elements in the top layer, such as an open <dialog> shown with showModal() or a popover, are also removed from the top layer when they close. Without extra care, they leave it instantly and cut the exit animation short. The overlay property reports whether the element is in the top layer, and it is discrete. Add it to the transition list so the element stays in the top layer until the exit finishes.
dialog {
opacity: 0;
transform: translateY(8px);
transition:
opacity 300ms ease-out,
transform 300ms ease-out,
overlay 300ms allow-discrete,
display 300ms allow-discrete;
}
dialog[open] {
opacity: 1;
transform: none;
}
@starting-style {
dialog[open] {
opacity: 0;
transform: translateY(8px);
}
}
dialog::backdrop {
background-color: rgb(0 0 0 / 0);
transition:
background-color 300ms ease-out,
overlay 300ms allow-discrete,
display 300ms allow-discrete;
}
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0.5);
}
@starting-style {
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0);
}
}The backdrop is a separate pseudo-element, so it needs its own transition and its own starting style. For popovers, use the :popover-open selector in place of [open].
Reacting to the end of the transition
The transitionend event fires for each property that finishes. With allow-discrete, you may also receive events for display and overlay. Always check event.propertyName so your handler runs once, for the property you care about.
const panel = document.getElementById('panel');
panel.addEventListener('transitionend', (event) => {
if (event.propertyName === 'opacity' && !panel.classList.contains('is-open')) {
console.log('Panel finished fading out');
}
});If a transition is interrupted, for example by toggling the class again mid-fade, you get transitioncancel instead of transitionend.
Failure modes and debugging
- No entry effect. Check that the
@starting-styleselector matches the element's final-state rule, that it comes after that rule, and that atransitioncovers the property. - No exit effect. The
displayoroverlayentry is missing from thetransitionlist, or it lacksallow-discrete. - Effect works the first time only.
@starting-styleapplies when an element first renders or leavesdisplay: none. Changing a class on a visible element does not use it. Use a normal transition for that case. - Reduced motion. Entry and exit effects are motion. Wrap them in a
prefers-reduced-motionquery, as covered in the earlier chapter, and set the durations to near zero sodisplaystill flips correctly. - Browser support. Both features are in current versions of Chrome, Safari and Firefox, but older releases ignore them. Without support, elements appear and disappear instantly, which is an acceptable fallback. Check a compatibility table for your target browsers.
In developer tools, the Animations panel shows entry transitions. If nothing appears there when the element is inserted, the starting style was not applied.
Exercises
Exercise
A notification is inserted into the page with JavaScript. Its CSS sets opacity: 1 and transition: opacity 300ms, but it still appears instantly. What do you add, and why?
Show answer
Add an @starting-style rule that sets opacity: 0 for the same selector, after the normal rule. A newly inserted element has no previous style, so without a starting value there is nothing to transition from.
Exercise
A menu hides with display: none and fades out with an opacity transition, but on close it vanishes immediately. What change fixes the exit, and at what point in the transition does display flip to none?
Show answer
Add display 300ms allow-discrete (matching the opacity duration) to the transition list. With allow-discrete, display flips to none at the end (100%) of the transition, so the fade is visible first.