Understanding CSS Custom Properties: ”-sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;”
Custom properties (CSS variables) let you store values once and reuse them across your stylesheet. The snippet –sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in; is an example of using custom properties to parameterize an animation system. Below is a concise guide explaining what each part does, why this pattern is useful, and how to implement and extend it.
What each property means
- –sd-animation: sd-fadeIn;
Holds the name of the animation to apply. Using a variable here allows switching between animation presets (e.g.,sd-slideUp,sd-scaleIn) without changing multiple CSS rules. - –sd-duration: 250ms;
Sets the animation duration. Using a variable makes it easy to tune timing across many components consistently. - –sd-easing: ease-in;
Defines the timing function for the animation, controlling acceleration and deceleration.
Why use this pattern
- Reusability: Centralize animation configuration to keep consistent motion across components.
- Theming: Swap variable values in different themes (dark/light, compact/accessible) to change motion behavior globally.
- Maintainability: Update duration/easing in one place rather than editing multiple animation declarations.
- Declarative component styling: Components declare desired animation via variables and a shared animation utility applies them.
Basic implementation
CSS:
css
:root {–sd-duration: 250ms; –sd-easing: ease-in;}
/* Define keyframes for sd-fadeIn /@keyframes sd-fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); }}
/ Utility that reads variables to apply animation */.sd-animated { animation-name: var(–sd-animation, none); animation-duration: var(–sd-duration, 250ms); animation-timing-function: var(–sd-easing, ease-in); animation-fill-mode: both;}
HTML usage:
html
<div class=“sd-animated” style=”–sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;”> Fade-in content</div>
Advanced tips
- Fallbacks: Provide sensible defaults in property values with the var() fallback syntax.
- Accessibility: Respect user motion preferences — disable or simplify animations for users who prefer reduced motion.
css
@media (prefers-reduced-motion: reduce) { .sd-animated { animation: none !important; transition: none !important; }}
- Composition: Combine multiple animations by setting
–sd-animationto a comma-separated list and corresponding duration/easing lists. - JavaScript control: Toggle or update custom properties at runtime with element.style.setProperty to dynamically change animations per interaction.
Example: Theme switch
css
.theme-fast { –sd-duration: 120ms; –sd-easing: cubic-bezier(.2,.9,.2,1); }.theme-slow { –sd-duration: 400ms; –sd-easing: ease-out; }
Conclusion
Using CSS custom properties like –sd-animation, –sd-duration, and –sd-easing makes animation systems flexible, themeable, and maintainable. They separate motion intent from implementation, enabling consistent, accessible UI motion across a project.
Leave a Reply