CSS Entry Animations with @starting-style
Entry animations without JavaScript timing
Transitions only ran when a value changed. To animate in, you added a class in JS after paint. @starting-style defines the before state so CSS can transition from it.
The old hack vs the modern fix
View demo.card {
opacity: 0;
transform: translateY(10px);
transition: opacity .3s, transform .3s;
}
.card.visible {
opacity: 1;
transform: none;
}
/* JS: must run after paint, or the transition won't run */
requestAnimationFrame(() => {
requestAnimationFrame(() => {
el.classList.add('visible');
});
});.card {
transition: opacity .3s, transform .3s;
@starting-style {
opacity: 0;
transform: translateY(10px);
}
}Starting style browser support
Live demo
Why the modern way wins
No JS timing
No double rAF or setTimeout. The browser knows the starting state from CSS.
Declarative
Entry and transition live in one place. Works with dynamic content and frameworks.
Same transition
Uses your existing transition properties. No separate keyframes for enter.
- Lines Saved
- 10 → 6
- No JS for entry
- Old Approach
- Class after paint
- rAF or setTimeout
- Modern Approach
- @starting-style
- CSS-defined start state
How it works
CSS transitions only run when a property changes. If an element appears with opacity: 0 and you want it to transition to 1, the browser never saw the 0, so you had to set the initial state, then add a class in the next frame (requestAnimationFrame, or double rAF) so the change was detected.
@starting-style lets you define the style that applies at the moment the element is first rendered. The browser uses that as the from state and transitions to the element's actual styles. No JavaScript, no timing hacks.
If the entry moves the element, like the translateY here, keep that part inside @media (prefers-reduced-motion: no-preference) and let reduced-motion users get the fade alone. The fade still tells them something new showed up.