-sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;
CSS custom properties like –sd-animation, –sd-duration, and –sd-easing are useful tools for creating consistent, maintainable animation systems across a site or component library. This article explains what these variables represent, how to use them together, and offers practical examples and best practices.
What these properties mean
- –sd-animation: A custom property naming the animation or animation preset to apply (e.g.,
sd-fadeIn). Using a semantic name lets you swap animations easily without changing multiple rule sets. - –sd-duration: The duration of the animation. Express using CSS time units (e.g.,
250ms,0.25s). - –sd-easing: The timing function controlling acceleration (e.g.,
ease-in,linear,cubic-bezier(…)).
Why use custom properties for animation
- Consistency: Centralize timing and easing values for uniform motion across UI.
- Theming: Allow different themes or contexts to override animation behavior.
- Maintainability: Change a single variable to update many components.
- Reactivity: Easily toggle or animate via inline styles, JavaScript, or Web Components.
Basic usage
Define defaults in a root scope:
:root {–sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;}
Use them in component rules:
.my-element { animation-name: var(–sd-animation); animation-duration: var(–sd-duration); animation-timing-function: var(–sd-easing); animation-fill-mode: both;}
Define the referenced animation:
@keyframes sd-fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); }}
Variants and overrides
Create context-specific overrides:
.theme-reduced-motion { –sd-duration: 0ms;}
.modal { –sd-duration: 400ms; –sd-easing: cubic-bezier(.2,.8,.2,1); –sd-animation: sd-slideUp;}
Accessibility: prefers-reduced-motion
Respect user preferences:
@media (prefers-reduced-motion: reduce) { :root { –sd-duration: 0ms; }}
JavaScript control
Toggle or animate dynamically:
const el = document.querySelector(’.my-element’);el.style.setProperty(’–sd-duration’, ‘500ms’);el.classList.add(‘is-visible’); // triggers animation
Best practices
- Use semantic names (
sd-fadeIn,sd-slideUp) rather than implementation details. - Keep durations short for UI micro-interactions (100–300ms) and longer for significant transitions (300–500ms).
- Use easing functions that match the motion intent (ease-out for exit, ease-in for entrance).
- Respect reduced-motion preferences.
- Test cross-browser; provide fallbacks for older browsers.
Conclusion
Using custom properties like –sd-animation, –sd-duration, and –sd-easing creates flexible, themeable animation systems that are easier to maintain and adapt. With sensible defaults, accessibility considerations, and clear naming, you can build consistent motion across your UI.
Leave a Reply