Understanding CSS Custom Properties: -sd-animation, -sd-duration, and -sd-easing
The snippet -sd-animation: sd-fadeIn; –sd-duration: 0ms; –sd-easing: ease-in; shows a pattern for using CSS custom properties (variables) to control animation behavior in a flexible, themeable way. This article explains what each part does, how they might be used, and provides practical examples for implementing them in real projects.
What these properties represent
- -sd-animation: a custom property (likely vendor- or project-prefixed) that holds the name of an animation or an animation shorthand token (e.g.,
sd-fadeIn). - –sd-duration: a standard CSS custom property storing the animation duration (here
0ms). - –sd-easing: a custom property for the timing function (
ease-in).
Using variables like these lets you change animation behavior at different scopes (global, component, or per-instance) without rewriting keyframes or animation declarations.
Example keyframes and usage
Define a reusable keyframe and a component style that reads the variables:
css
@keyframes sd-fadeIn {from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); }}
/* component root /.component { / defaults / –sd-duration: 300ms; –sd-easing: ease-out; -sd-animation: sd-fadeIn;
animation-name: var(-sd-animation); animation-duration: var(–sd-duration); animation-timing-function: var(–sd-easing); animation-fill-mode: both;}
Override per-instance:
css
/ instant appearance /.card.instant { –sd-duration: 0ms; –sd-easing: ease-in;}
/ slower, bouncier intro */.card.slow { –sd-duration: 600ms; –sd-easing: cubic-bezier(.2,1,.3,1);}
Why use custom properties for animation?
- Theming & scope: Easily alter animation timing across a theme or specific components.
- Runtime adjustments: Update CSS variables from JavaScript for dynamic effects without toggling class names.
- Maintainability: Keep keyframes and behavior centralized; only change variables where needed.
Notes and caveats
- The property name
-sd-animationis nonstandard but valid as a custom property if declared with the leading two hyphens (e.g.,–sd-animation). Browsers ignore unknown properties in standard animation declarations unless used via var(). In the example above we usedvar(-sd-animation)—this should bevar(–sd-animation)if following CSS custom property syntax. - Zero-duration (
0ms) makes the animation effectively instantaneous; it still triggers any animation-fill-mode effects. - Ensure fallbacks if variables might be missing:
animation-duration: var(–sd-duration, 300ms);
Quick reference
- Use
@keyframesto define the animation (e.g.,sd-fadeIn). - Assign variables at appropriate scopes.
- Apply animation properties using var(–name) with sensible defaults.
This approach yields flexible, composable animations that are easy to theme and adjust across a codebase.
Leave a Reply