Using Custom CSS Variables for Smooth Micro-Animations: ”-sd-animation: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;”
Micro-animations give interfaces personality and clarity when used sparingly. The shorthand CSS snippet
-sd-animation: sd-fadeIn;–sd-duration: 250ms;–sd-easing: ease-in;
encodes an intent: apply a fade-in animation named sd-fadeIn, run it for 250 milliseconds, and ease the start. Below is a practical guide to implementing this pattern accessibly and maintainably.
What these properties mean
- -sd-animation: sd-fadeIn — a custom shorthand (vendor-like) property indicating which animation to apply; map this to actual animation rules in your stylesheet or component library.
- –sd-duration: 250ms — a CSS custom property controlling the animation length.
- –sd-easing: ease-in — a timing function that accelerates the animation from a slow start.
Implementation pattern
- Define the keyframes for sd-fadeIn:
css
@keyframes sd-fadeIn {from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); }}
- Create a reusable animation class that reads the custom properties:
css
.sd-animated { animation-name: var(–sd-animation-name, sd-fadeIn); animation-duration: var(–sd-duration, 200ms); animation-timing-function: var(–sd-easing, ease); animation-fill-mode: both; will-change: opacity, transform;}
- Map the shorthand -sd-animation to the custom property the class uses. Because custom properties can’t be dynamically mapped by CSS alone, set the mapping where your components are rendered (inline style, JS, or a preprocessor):
html
<div class=“sd-animated” style=”–sd-animation-name: sd-fadeIn; –sd-duration: 250ms; –sd-easing: ease-in;”> </div>
Alternatively, if you prefer the -sd-animation token directly:
css
[data-sd-animation=“sd-fadeIn”] { –sd-animation-name: sd-fadeIn;}
Then:
html
<div class=“sd-animated” data-sd-animation=“sd-fadeIn” style=”–sd-duration:250ms; –sd-easing:ease-in”>…</div>
Accessibility considerations
- Prefer reduced-motion: respect user preferences by disabling or simplifying animations when prefers-reduced-motion is set.
css
@media (prefers-reduced-motion: reduce) { .sd-animated { animation: none !important; opacity: 1; transform: none; }}
- Keep durations short (150–300ms) for UI micro-interactions to avoid disorientation.
- Ensure animated elements remain focusable and visible; don’t rely on animation to convey critical information.
Performance tips
- Animate transform and opacity (hardware-accelerated) instead of layout-triggering properties like width/height.
- Use will-change sparingly and remove it after animation if possible.
- Batch style changes in JavaScript with requestAnimationFrame when triggering animations programmatically.
Variations and composition
- Combine with scale or slight movement for emphasis:
css
@keyframes sd-fadeInScale { from { opacity:0; transform: translateY(6px) scale(.98); } to { opacity:1; transform: translateY(0) scale(1); }}
- Create utility tokens for duration/easing presets:
css
:root { –sd-duration-fast: 150ms; –sd-duration-default: 250ms; –sd-ease-default: ease; –sd-ease-entrance: cubic-bezier(.2,.9,.2,1);}
Example component usage
html
<button class=“sd-animated” data-sd-animation=“sd-fadeIn” style=”–sd-duration:250ms; –sd-easing:ease-in”> Save</button>
Summary
Using a small set of custom properties like -sd-animation, –sd-duration, and –sd-easing gives teams a consistent, flexible way to control micro-animations. Pair this with accessible defaults, prefers-reduced-motion handling, and performance-minded animations (opacity/transform) to create smooth, pleasant interfaces.
Leave a Reply