Transitions between states
index.html
<button class="action" type="button">Save</button>
motion.css
.action {
--duration: 160ms;
border: 2px solid #334155;
border-radius: 0.5rem;
padding: 0.75rem 1rem;
background: #fff;
color: #172033;
}
.action:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}
.action:hover,
.action:focus-visible {
background: #e2e8f0;
}
@media (prefers-reduced-motion: no-preference) {
.action {
transition: transform var(--duration) ease-out;
}
.action:active {
transform: scale(0.98);
}
}
Use a transition when a computed property changes, for example on hover or keyboard focus. Declare it on the base selector so both directions animate. Explicitly name the properties instead of using transition: all; unrelated changes should not acquire a delay. [1]
Keyframes and timing
notice.css
.notice {
opacity: 1;
transform: none;
}
@keyframes notice-enter {
from {
opacity: 0;
transform: translateY(0.5rem);
}
to {
opacity: 1;
transform: none;
}
}
@media (prefers-reduced-motion: no-preference) {
.notice {
animation: notice-enter 240ms ease-out;
animation-timing-function: linear(0, 0.7 45%, 1);
}
}
Use @keyframes for a sequence or an entry effect. The base style must represent the final usable state; fill modes do not permanently rewrite it. ease-out decelerates; cubic-bezier() provides custom curves, and linear() interpolates between supplied stops. Keep an earlier easing declaration as a fallback. [2][3]
Rendering and performance
Changing width or top can trigger layout; shadows and colors can require paint. Transform and opacity are often cheaper, but compositing depends on the element and browser. Avoid blanket will-change: layers consume memory. Compare traces with and without motion in DevTools, inspecting layout, paint and dropped frames on a slower device. [4]
Reduced motion and WCAG
Start with a static interface and enable optional motion only under no-preference. Preserve focus and feedback without movement. WCAG 2.2 criterion 2.2.2 (A) requires controls for qualifying automatically moving content lasting over five seconds alongside other content, unless essential. Criterion 2.3.3 (AAA) addresses disabling nonessential interaction-triggered motion. [5][6][7]
Scroll-driven animation
decoration.css
.section-decoration {
transform: none;
}
@keyframes reveal-decoration {
from {
transform: scaleX(0.25);
}
to {
transform: scaleX(1);
}
}
@supports (animation-timeline: view()) and
(animation-range: entry 0% entry 100%) {
@media (prefers-reduced-motion: no-preference) {
.section-decoration {
transform-origin: left center;
animation: reveal-decoration auto linear both;
animation-timeline: view();
animation-range: entry 0% entry 100%;
}
}
}
A view timeline maps animation progress to an element’s movement through a scrollport, rather than elapsed time. Apply the effect to a decorative child to avoid hiding content. Check support for timeline and range, and declare the timeline after the animation shorthand, which resets it. Without support, keep the static decoration. [8]
Web Animations API and cleanup
index.html
<button type="button" data-replay>Replay effect</button>
<span class="effect" data-effect aria-hidden="true">●</span>
effect.ts
const EFFECT_DURATION_MS = 240;
const MOTION_QUERY = "(prefers-reduced-motion: reduce)";
export function mountEffect(root: ParentNode): () => void {
const button = root.querySelector("[data-replay]");
const candidate = root.querySelector("[data-effect]");
if (
!(button instanceof HTMLButtonElement) ||
!(candidate instanceof HTMLElement)
) {
return () => {};
}
const effect = candidate;
const preference = window.matchMedia(MOTION_QUERY);
const listeners = new AbortController();
let animation: Animation | undefined;
function stop(): void {
animation?.cancel();
animation = undefined;
}
function replay(): void {
stop();
if (preference.matches || typeof effect.animate !== "function") {
return;
}
animation = effect.animate(
[
{ transform: "scale(0.8)" },
{ transform: "scale(1)" },
],
{
duration: EFFECT_DURATION_MS,
easing: "ease-out",
},
);
}
function onPreferenceChange(event: MediaQueryListEvent): void {
if (event.matches) {
stop();
}
}
button.addEventListener("click", replay, { signal: listeners.signal });
preference.addEventListener("change", onPreferenceChange, {
signal: listeners.signal,
});
return () => {
listeners.abort();
stop();
};
}
effect.css
.effect {
display: inline-block;
}
Use element.animate() when application logic must control playback. The example uses a dedicated decorative element, cancels the previous effect before replaying, listens for motion preference changes and removes listeners on disposal. A canceled finished promise rejects with AbortError; handle it if awaiting completion. Business state must not depend on an animation finishing. [9][10]
Verification before release
Test keyboard focus, touch, repeated activation, disabled motion at startup and a preference change during playback. Remove the component during animation and check for outstanding listeners. Disable scroll-timeline support to verify the fallback. Measure the actual page; a 60 Hz frame interval is about 16.7 ms, not an exclusive JavaScript budget. [4][5][8]
