Performant Web Animation
Eliminate the #1 cause of janky web animation: animating properties that force the browser to recalculate layout (reflow) or repaint on every frame. The browser renders in stages — layout → paint → composite. Animating width, height, top, left, margin, padding, box-shadow, or filter re-runs layout and/or paint each frame, blocking the main thread. Animating transform and opacity runs entirely on the compositor (often the GPU), skipping layout and paint, which is what makes animation smooth at 60/120fps.
When to use
Use when an animation stutters or drops frames, when a hover/scroll effect feels heavy, when animating size/position/shadow, when a layout change needs to animate smoothly (cards reordering, an element moving between containers), when animating height: auto, or when reviewing animation code for performance.
Core rule: animate only transform and opacity
Map every "expensive" animation to a cheap equivalent.
Position and size via transform
scaleX distorts inner content (text stretches). For true resizing without distortion, use FLIP.
Cheap box-shadow via pseudo-element opacity
Animating box-shadow repaints a large blurred region every frame. Instead paint the shadow once on a ::after, then animate only its opacity.
The blurred shadow is rasterized once; hovering only changes a compositor opacity — smooth at any frame rate.
FLIP: animate layout changes cheaply
FLIP (First, Last, Invert, Play) animates a layout change (reorder, resize, move between containers) using only transform. Measure where the element was (First) and will be (Last), apply an inverting transform so it visually appears unmoved, then animate the transform back to identity (Play). The DOM ends in its real final layout; the motion is pure compositor work.
The Web Animations API animate() runs the transform on the compositor. For many elements, batch all getBoundingClientRect() reads before any mutation (see layout thrashing below).
Animating height: auto
height: auto is not interpolatable historically. Modern and fallback approaches:
The grid trick animates the track size (compositor-friendly enough and broadly supported) and needs no JS height measurement. Use interpolate-size: allow-keywords / calc-size(auto, size) where the target browsers support it; keep the grid technique as the cross-browser default.
Avoid layout thrashing (batch reads, then writes)
Reading a layout property (offsetWidth, getBoundingClientRect, scrollTop, getComputedStyle) after a write forces a synchronous reflow. Interleaving reads and writes in a loop ("layout thrashing") can run dozens of forced reflows per frame.
For frame-synced work, read in a requestAnimationFrame callback and apply writes; libraries like fastdom formalize this read/write scheduling.
will-change: use sparingly
will-change: transform promotes an element to its own compositor layer ahead of time, avoiding a hitch at animation start. But every promoted layer costs GPU memory, and over-use degrades performance.
Rules: apply just before the animation (e.g. on hover/parent state), remove it after (will-change: auto) when idle, never blanket-apply to many elements, and never leave it permanently on large/numerous nodes. A single transform: translateZ(0) hack does the same promotion but is harder to undo — prefer will-change.
Deliver & verify (standalone HTML)
Packaged helper (
scripts/):scripts/seek-shot.sh anim.html 0 1.5 3freezes the?t=Nharness and screenshots each moment;scripts/contact-sheet.sh sheet.png frame-*.pngtiles them for one-glance review. Seescripts/README.md.
The deliverable is one self-contained .html that opens directly in a browser — the markup, the CSS/JS animation, and a freeze harness in one file. For this skill, verification is two-pronged: the frame must look right and be cheap to produce (compositor-only).
Output contract:
- One
.html, deps via CDN if any, animation driven by CSS transitions/@keyframesor the Web Animations API. - A freeze mechanism so a screenshot lands on a deterministic frame:
- CSS
@keyframes→?t=Nsetsel.style.animationDelay = (-N)+'s'; el.style.animationPlayState = 'paused'. - WAAPI / JS → keep the animation object and
anim.pause(); anim.currentTime = N*1000.
- CSS
Verify loop — freeze → screenshot, then profile for jank:
- Open headless at start / mid / end (
?t=0, mid, end), screenshot each; confirm the motion is visually correct (no clipped/stretched text fromscaleX, FLIP lands on the real layout, shadow/height transitions look right). - Confirm it's compositor-only — the whole point of this skill. Either:
- DevTools → Performance: record the animation, check there is no purple "Layout" or green "Paint" band per frame (only "Composite Layers").
- Or headless trace:
npx playwright screenshotfor the visual, plus a CDP/tracingcapture, and assert noLayout/Paintevents fire during the animation window.
- Watch the FPS/“Frame Rendering Stats” overlay stays at 60 — dropped frames mean an expensive property slipped back in.
Before you finish:
- Opens standalone — no console errors.
- Only
transform/opacityanimate per frame; DevTools shows no per-frame Layout/Paint. - Screenshotted at start / mid / end — correct, no
scaleXtext distortion, FLIP lands true. - Holds 60fps;
will-changeapplied just-in-time and removed when idle (no leftover layers). prefers-reduced-motionhonored.
Quick reference
Gotchas
scaleX/scaleYstretches text and children; use FLIP when content must stay crisp.transformpercentages are relative to the element's own box, not the parent — different fromleft: %.- Overusing
will-changeortranslateZ(0)creates too many layers and hurts performance; promote only what animates. filterandbackdrop-filterare compositor-related but still expensive; animate their presence via opacity cross-fades rather than animating the blur radius.- The grid
1fr→0frcontent must haveoverflow: hiddenandmin-height: 0or it won't collapse. - Always gate non-essential motion behind
@media (prefers-reduced-motion: reduce).
Reference files
references/patterns-and-profiling.md— full runnable examples (accordion, reorder list, parallax), DevTools profiling walkthrough to confirm compositor-only frames, reduced-motion patterns, and a property-cost cheat sheet.

