Sub topic 3
Respecting Reduced Motion in Tailwind CSS
Assume basic HTML and CSS, familiarity with Tailwind utility classes, and a basic understanding of CSS transitions and keyframes. This chapter focuses on accessibility, customization, performance, enter transitions, and reusable interaction patterns.
Respecting reduced motion preferences
Some users enable reduced motion because animation causes discomfort or makes interfaces harder to use. Tailwind provides motion-safe and motion-reduce variants.
<div class="size-6 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin motion-reduce:animate-none"></div>The spinner runs only when motion is allowed. When a user prefers reduced motion, motion-reduce:animate-none disables it. Keep important information and state changes visible without animation.
Arbitrary values and theme customization
Use arbitrary values for a one-off duration or easing curve:
<button class="transition-transform duration-[ ಬಸ200ms] ease-[cubic-bezier(0.2,0.8,0.2,1)] hover:scale-105">
Open
</button>Avoid invalid characters in arbitrary values. The valid version is:
<button class="transition-transform duration-[200ms] ease-[cubic-bezier(0.2,0.8,0.2,1)] hover:scale-105">
Open
</button>For values used across a design, define theme variables instead. This keeps animation timing consistent.
Writing custom keyframes and animation utilities
In Tailwind v4, define an animation variable and its keyframes inside @theme:
@import "tailwindcss";
@theme {
--animate-wiggle: wiggle 0.4s ease-in-out 2;
@keyframes wiggle {
0%, 100% { transform: rotate(-3deg); }
50% { transform: rotate(3deg); }
}
}An element can now use animate-wiggle.
Keeping animations smooth
Prefer transform and opacity. Browsers can usually animate them without recalculating the page layout. Avoid repeatedly animating properties such as width, height, top, or left. Use translate-* instead of changing positions with layout properties.
Animating elements as they appear
Tailwind v4 supports enter transitions with starting:open and transition-discrete:
<div popover id="menu"
class="opacity-0 transition-opacity transition-discrete duration-200 open:opacity-100 starting:open:opacity-0">
Menu content
</div>starting:open supplies the initial open state. transition-discrete permits discrete properties, such as display, to participate in the transition.
Putting it together: common patterns
Combine state variants, motion preferences, and transforms:
<a href="#"
class="group inline-flex items-center gap-2 text-indigo-600 motion-safe:transition-colors motion-safe:duration-200 hover:text-indigo-800">
<span>Read more</span>
<span class="motion-safe:transition-transform motion-safe:duration-200 group-hover:translate-x-1">→</span>
</a>Exercise
Which Tailwind variants should you use to run an animation normally but disable it for users who prefer reduced motion?
Show answer
Use motion-safe: for the animation and motion-reduce: with animate-none or another reduced-motion style.