Understanding CSS Custom Properties for Animations: ”-sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;”
CSS custom properties (variables) let you create flexible, reusable animation settings. The snippet -sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in; demonstrates a pattern for defining animation type, duration, and easing using custom properties so animations can be adjusted consistently across components.
What each property does
- -sd-animation: sd-fadeIn;
- Declares a custom property (using a vendor-like prefix) that names the animation variant to apply. Here,
sd-fadeInimplies a fade-in keyframe defined elsewhere.
- Declares a custom property (using a vendor-like prefix) that names the animation variant to apply. Here,
- –sd-duration: 250ms;
- Sets the duration of the animation to 250 milliseconds.
- –sd-easing: ease-in;
- Defines the timing function controlling acceleration of the animation.
How to use these properties in CSS
- Define keyframes for the named animation:
css
@keyframes sd-fadeIn {from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); }}
- Create a reusable rule that reads the custom properties:
css
.animated { animation-name: var(–sd-animation-name, none); animation-duration: var(–sd-duration, 300ms); animation-timing-function: var(–sd-easing, ease); animation-fill-mode: both;}
- Map the prefixed property to the animation name (since custom properties are case-sensitive and the example uses a non-standard property name):
css
/* normalize the vendor-like property into a usable –sd-animation-name /.animated { / fallback: if -sd-animation is set, use it; otherwise use var(–sd-animation-name) */ –sd-animation-name: var(-sd-animation, var(–sd-animation-name, none)); animation-name: var(–sd-animation-name);}
- Apply to elements:
html
<div class=“animated” style=”-sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;”> Fade-in content</div>
Benefits of this approach
- Reusability: Centralized control of duration and easing across many components.
- Theming: Easy to swap animation variants or timings by adjusting custom properties.
- Granularity: Per-element overrides without creating new CSS classes for each timing/easing combo.
Best practices
- Provide sensible fallbacks in your CSS when variables are missing.
- Keep keyframe names consistent and documented.
- Prefer logical names for variables (e.g., –sd-duration-short) for clarity.
- Avoid overusing per-element inline styles; use utility classes when possible for maintainability.
Example: short, medium, long duration utilities
css
.u-anim-short { –sd-duration: 150ms; }.u-anim-medium { –sd-duration: 250ms; }.u-anim-long { –sd-duration: 400ms; }
This pattern offers a compact, consistent way to manage animation behavior across a UI, making it easy to iterate on timing and easing without editing multiple style rules.
Leave a Reply