Starting styles and discrete transitions
Definition and purpose
Starting styles provide a prior style for a transition when an element first receives a rendered style. Discrete transitions allow some non-interpolating values to participate in a transition.
Entry notice
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { display: none; opacity: 0; transition: opacity 1s linear, display 1s allow-discrete; } #a.shown { display: block; opacity: 1; } @starting-style { #a.shown { opacity: 0; } } </style> </head> <body> <button type="button" id="toggle" onclick="document.querySelector('#a').classList.toggle('shown')">Toggle notice</button> <p id="a">Saved</p> </body></html>Initially hidden. Toggle to see the notice enter and leave.
Syntax and mental model
Use @starting-style after an equally specific final rule, or nest it inside that rule. The entry transition can interpolate opacity while display flips to a visible value at the start; on exit display becomes none at the end when allow-discrete applies. The starting style is not a persistent default. Keep the final visible state independent of animation and disable transitions when reduced motion is requested.
Reduced-motion fallback
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { display: none; opacity: 0; transition: opacity 1s linear, display 1s allow-discrete; } #a.shown { display: block; opacity: 1; } @starting-style { #a.shown { opacity: 0; } } @media (prefers-reduced-motion: reduce) { #a { transition: none; } } </style> </head> <body> <button type="button" id="toggle" onclick="document.querySelector('#a').classList.toggle('shown')">Toggle notice</button> <p id="a">Saved</p> </body></html>The notice remains usable with transitions disabled by the preference query.
Common mistakes
Animating opacity alone does not remove an element from focus or layout. Do not substitute a visually transparent state for a real hidden state.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Scroll and view timelines. Next lesson: View transitions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Starting styles and discrete transitions — knowledge check
Five questions on starting styles and discrete transitions.
When is @starting-style useful?
Which permits discrete display transitions?
When does display become none on exit?
What remains with opacity: 0 alone?
What should reduced motion do?