Understanding CSS Custom Properties: -sd-animation, –sd-duration, and –sd-easing
CSS custom properties (variables) let developers create reusable, themeable values for styles. The snippet -sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in; demonstrates a pattern of using a custom animation name alongside duration and easing variables to control animation behavior consistently across components.
What each property does
- -sd-animation: holds a custom identifier for an animation (here
sd-fadeIn). Using a single variable for the animation name makes it easy to switch or toggle animations across components. - –sd-duration: defines the animation length (
250ms), allowing uniform timing control and easy adjustments for pacing. - –sd-easing: sets the timing function (
ease-in), determining how the animation accelerates or decelerates.
Why use this pattern
- Consistency: Centralizes animation settings so multiple elements share identical timing and easing without repeating values.
- Theming: Swap durations or easing globally (or per theme) by updating variables.
- Maintainability: Changing the animation behavior becomes a one-line edit rather than hunting through multiple rules.
- Composability: Combine with other custom properties (delays, iteration counts) to build flexible animation systems.
Example usage
css
:root {–sd-duration: 250ms; –sd-easing: ease-in; –sd-delay: 0ms; -sd-animation: sd-fadeIn;}
.card { animation-name: var(-sd-animation); animation-duration: var(–sd-duration); animation-timing-function: var(–sd-easing); animation-delay: var(–sd-delay); animation-fill-mode: both;}
/* keyframes corresponding to the custom name */@keyframes sd-fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); }}
Best practices
- Prefix custom properties (e.g.,
–sd-) to avoid collisions with third-party styles. - Use semantic names for variables (e.g.,
–sd-duration-short,–sd-easing-entrance) for clarity when multiple durations/easings are needed. - Provide sensible fallbacks when referencing variables:
animation-duration: var(–sd-duration, 200ms); - Keep animation durations short for UI feedback (typically 100–400ms) and ensure motion accessibility—respect prefers-reduced-motion by disabling or simplifying animations for users who opt out.
Accessibility
Respect the user’s reduced-motion preference:
css
@media (prefers-reduced-motion: reduce) { .card { animation: none; transition: none; }}
Conclusion
Using a combined pattern of an animation-name variable with separate duration and easing custom properties provides a scalable, maintainable approach to manage UI motion. It simplifies theming, promotes consistency, and makes it easy to adapt animations for accessibility or design changes.
Leave a Reply