Understanding CSS Custom Properties for Animations: –sd-animation, –sd-duration, and –sd-easing
CSS custom properties (variables) make it easy to centralize and reuse animation configuration across a site. The trio –sd-animation, –sd-duration, and –sd-easing shown as -sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in; is a compact pattern for declaring animation behavior that can be consumed by component styles or JavaScript. Below is a practical guide to what each property does, how to implement them, and best practices.
What these properties represent
- –sd-animation: Stores the name of an animation or a shorthand token (e.g.,
sd-fadeIn) that a component’s styles will map to actual keyframe usage. Using a semantic token lets you swap animations without editing many rules. - –sd-duration: Controls how long the animation runs (here,
250ms). - –sd-easing: Defines the timing function (
ease-in) that controls acceleration.
Example: Define keyframes and use the variables
CSS:
css
/Keyframes for the semantic token /@keyframes sd-fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); }}
/ Default values at root /:root { –sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;}
/ Component that consumes the vars */.card { animation-name: var(–sd-animation); animation-duration: var(–sd-duration); animation-timing-function: var(–sd-easing); animation-fill-mode: both;}
Using tokens for theming and overrides
- Theme override:
css
.dark-theme { –sd-duration: 350ms; –sd-easing: cubic-bezier(.2, .8, .2, 1);}
- Per-component override:
css
.card.fast { –sd-duration: 150ms;}
Accessibility and performance considerations
- Respect prefers-reduced-motion: disable or simplify animations when users request reduced motion.
css
@media (prefers-reduced-motion: reduce) { .card { animation: none; transition: none; }}
- Keep animations short (150–350ms) for micro-interactions; avoid animating properties that trigger layout (use transform and opacity).
- Use will-change sparingly for complex animations to hint rendering optimizations.
JavaScript usage
You can read or set these variables at runtime:
js
const el = document.querySelector(’.card’);const duration = getComputedStyle(el).getPropertyValue(’–sd-duration’);el.style.setProperty(’–sd-easing’, ‘cubic-bezier(.22,.9,.32,1)’);
When to use this pattern
- Component libraries with themed motion tokens.
- Apps needing per-component or per-theme animation tuning without duplicating keyframes.
- Situations where JavaScript toggles animation behavior by changing CSS variables.
Quick checklist
- Define semantic keyframes (e.g.,
sd-fadeIn) once. - Provide sensible defaults at :root.
- Honor reduced-motion preferences.
- Prefer transform/opacity for smoother performance.
- Use variables for easy overrides and theming.
This pattern keeps motion consistent, themable, and easy to maintain across a codebase while enabling runtime flexibility.
Leave a Reply