CSS @starting-style At-Rule
The @starting-style at-rule defines the starting values used when an element first enters rendering.
It makes CSS entry transitions possible for newly inserted or newly displayed elements. A transition must also be declared on the element.
@starting-style Syntax
@starting-style {
.notice {
opacity: 0;
}
}
.notice {
opacity: 1;
transition: opacity 300ms;
} The at-rule supplies a starting value when an element first receives a rendered style. The ordinary rule supplies the destination value and the transition.
How @starting-style Works
Newly rendered elements normally have no earlier computed style from which to transition. @starting-style supplies the before-change value so a transition can run when the element enters rendering.
It does not run on every hover or class change of an element that is already rendered.
Entry Transitions
.message {
opacity: 1;
transform: translateY(0);
transition: opacity 350ms, transform 350ms;
}
@starting-style {
.message {
opacity: 0;
transform: translateY(12px);
}
} When the message first enters rendering, it can fade and slide to its normal position. An appropriate transition declaration is still required.
Nested Form
.message {
opacity: 1;
transition: opacity 350ms;
@starting-style {
opacity: 0;
}
} The nested form expresses the same entry values within the element’s style rule.
Display and Discrete Transitions
.panel {
display: none;
opacity: 0;
transition: opacity 350ms, display 350ms;
transition-behavior: allow-discrete;
}
.panel.is-open {
display: block;
opacity: 1;
@starting-style {
opacity: 0;
}
} To animate an element entering from display: none, use a transition on display with transition-behavior: allow-discrete when needed. The element remains displayed during the opacity transition.
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.panel {
transition: none;
}
} Respect reduced-motion preferences by disabling or simplifying nonessential entry effects.
@starting-style Example
Use the button to add a message to the page. In supporting browsers, it fades in from its starting opacity.
HTML
<button type="button" id="add-message">Show message</button>
<div id="message-area"></div> CSS
.entry-message {
padding: 12px;
background: #edf5fb;
opacity: 1;
transition: opacity 450ms;
}
@starting-style {
.entry-message {
opacity: 0;
}
} JavaScript
document.querySelector("#add-message").addEventListener("click", () => {
document.querySelector("#message-area").innerHTML =
'<p class="entry-message">This message fades in.</p>';
}); Click Show Message to insert the message again and replay its entry transition. In browsers without @starting-style support, the message still appears, but without the entry fade.
Browser Support
@starting-style is available in modern browsers, with differences in older versions. Check Can I Use? for current support. Entry transitions involving display also depend on discrete-transition support.
Specifications
The at-rule is defined in CSS Transitions Level 2.
