Animated Transitions and Accessibility
What you'll learn: How to add smooth height transitions to components like accordions while respecting user preferences and maintaining accessibility.
Animations can make your accordion and tab panels feel polished and modern. However, not everyone wants or can tolerate motion—some users experience dizziness, nausea, or distraction from animations.
The Balance Between Polish and Access
When you expand or collapse an accordion panel, animating the height change creates a smooth visual flow. But animations come with accessibility considerations:
-
Respect
prefers-reduced-motion: This CSS media query detects if users have requested reduced motion in their operating system settings. When true, skip or minimize animations. -
Don't break functionality: Your component must work perfectly even if animations are disabled. The animation is decoration, not structure.
-
Keep it fast: Long animations frustrate keyboard users who are waiting to interact with newly-revealed content.
Implementation Pattern
.accordion-panel {
overflow: hidden;
transition: height 0.3s ease;
}
@media (prefers-reduced-motion: reduce) {
.accordion-panel {
transition: none;
}
}
Think of it like an elevator versus stairs. The smooth elevator ride (animation) is nice, but some people need to take the direct route (instant transition). Both get you to the same floor.
Testing Your Animations
Enable "Reduce Motion" in your OS settings (macOS: System Settings → Accessibility → Display; Windows: Settings → Accessibility → Visual Effects). Your accordion should still function perfectly, just without the smooth transition.
Key Takeaway: Always provide smooth animations as an enhancement while respecting prefers-reduced-motion and ensuring your components work perfectly without animation—accessibility means giving users control over their experience.