Home / Learn / How do I change the colors and speed of a library motion?

How do I change the colors and speed of a library motion?

Any motion in the library can get a new color and speed without rewriting it. The idea is to gather colors and duration into CSS variables, so every later edit lives in one place.

Steps

  1. Copy the motion's code into one file and open it in the browser. Run it before any edit so you know how the original looks.
  2. Find the hex or rgb colors in the CSS and move them into variables on :root (for example --accent), then replace each value with var(--accent). Find and Replace in any editor does it.
  3. Gather speed into a --speed variable: write the duration as calc(0.6s / var(--speed)), so a higher value means a shorter time. If the motion is driven by JavaScript, multiply or divide its fixed numbers by the same factor.
  4. For quick experiments, change the variable from JS with root.style.setProperty('--accent', value), as the code below does.
  5. After every change, check the new color's contrast (the ratios are in the contrast article) and test prefers-reduced-motion to make sure the calm version still works.

Sources

The code

HTML

<div class="card" dir="ltr" lang="en"><b>Company name</b></div>
<p>
  <label>Color <input id="c" type="color" value="#ffd166"></label>
  <label>Speed <input id="s" type="range" min="0.5" max="2" step="0.1" value="1"></label>
</p>
<style>
:root{--accent:#ffd166;--speed:1}
.card{display:inline-block;padding:1rem 1.5rem;background:#0d0e11;color:#fff;
  border-inline-start:6px solid var(--accent);font:700 1.4rem Cairo,system-ui,sans-serif}
/* higher speed = shorter duration. 4 runs, so it always stops. */
@media (prefers-reduced-motion: no-preference){
  .card{animation:pop calc(0.6s / var(--speed)) ease-out 4 alternate}
}
@keyframes pop{from{transform:scale(1)}to{transform:scale(1.06)}}
</style>
<script>
const root = document.documentElement;
document.getElementById('c').addEventListener('input',
  e => root.style.setProperty('--accent', e.target.value));
document.getElementById('s').addEventListener('input',
  e => root.style.setProperty('--speed', e.target.value));
</script>

On speed: NN/g says most UI animations should run roughly 100 to 500 ms depending on complexity, and that 500 ms starts to feel like a drag. For video and ads these limits are not binding, but they are a good reference for interfaces.

The original code may have its colors written inline instead of as variables, which is why step 2 exists.

can't find the motion you need? Request it here