CSS-related events
Definition and purpose
CSS transitions and animations can dispatch lifecycle events. Events report rendering-related lifecycle changes; they should not be the only mechanism that updates essential application state.
Status independent of events
<!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 { opacity: .5; transition: opacity .2s linear; } #a.on { opacity: 1; } </style> </head> <body> <button id="toggle" type="button">Toggle</button> <p id="a">Saved</p> <output id="report" aria-live="polite">Ready</output> <script> const a=document.querySelector("#a"),r=document.querySelector("#report");document.querySelector("#toggle").onclick=()=>{a.classList.toggle("on");r.textContent=a.classList.contains("on")?"Active":"Inactive";};for(const type of ["transitionend","transitioncancel"])a.addEventListener(type,e=>{a.dataset.lastEvent=e.type;}); </script> </body></html>The live status changes immediately on Toggle, while the optional opacity transition may later report completion.
Syntax and mental model
transitionrun reports creation, transitionstart follows delay, transitionend reports completion and transitioncancel reports cancellation. An event may occur per property and may bubble, so filter event.target and propertyName where needed. Animation events include animationstart, animationiteration, animationend and animationcancel. Zero-duration transitions without delay may not run, and reduced-motion rules may remove animation entirely. Update status immediately and treat lifecycle events as optional enhancement or cleanup.
Reduced-motion path
<!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 { opacity: .5; } #a.on { opacity: 1; } </style> </head> <body> <button id="toggle" type="button">Toggle</button> <p id="a">Saved</p> <output id="report" aria-live="polite">Ready</output> <script> const a=document.querySelector("#a"),r=document.querySelector("#report");document.querySelector("#toggle").onclick=()=>{a.classList.toggle("on");r.textContent=a.classList.contains("on")?"Active":"Inactive";};for(const type of ["transitionend","transitioncancel"])a.addEventListener(type,e=>{a.dataset.lastEvent=e.type;}); </script> </body></html>The status remains functional without transitions.
Common mistakes
Do not wait for transitionend to make an action usable; cancellation or zero motion can prevent that event. Do not assume all transitions end simultaneously.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Typed OM and Houdini. Next lesson: Stylesheet loading and critical CSS.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CSS-related events — knowledge check
Five questions on css-related events.
Which event reports a canceled transition?
Why filter propertyName?
When might transitionend not fire?
Which event reports animation repetition?
When should essential status update?