📖 Scenario: You are building a simple webpage using Bootstrap that includes an animated button. Some users prefer reduced motion to avoid animations that might cause discomfort. Your task is to add support for reduced motion preferences using CSS.
🎯 Goal: Create a Bootstrap-based webpage with a button that animates on hover. Then add CSS to disable the animation when the user prefers reduced motion.
📋 What You'll Learn
Use Bootstrap 5 CSS from CDN
Create a button with a hover animation
Add CSS that respects the user's reduced motion preference
Use the @media (prefers-reduced-motion: reduce) CSS media query
Ensure the animation is disabled when reduced motion is preferred
💡 Why This Matters
🌍 Real World
Many users have motion sensitivity or prefer less animation to avoid discomfort. Supporting reduced motion improves user experience and accessibility.
💼 Career
Web developers must create accessible websites that respect user preferences, including reduced motion settings, to meet accessibility standards and improve usability.
Progress0 / 4 steps
1
Create the basic HTML structure with Bootstrap
Create a basic HTML5 page with lang="en", charset="UTF-8", and viewport meta tags. Include the Bootstrap 5 CSS CDN link inside the <head>. Inside the <body>, add a <main> element with a Bootstrap button having the classes btn and btn-primary and the text Hover me.
Bootsrap
Hint
Remember to include the Bootstrap CSS link inside the <head> and create a button with the classes btn and btn-primary.
2
Add CSS for button hover animation
Add a <style> block inside the <head> after the Bootstrap CSS link. Inside it, write CSS to animate the button's background color to a lighter blue on hover using a smooth transition of 0.3 seconds. Use the selector button.btn-primary:hover and set background-color: #5a9bf6; with transition: background-color 0.3s ease; on the button.
Bootsrap
Hint
Use transition: background-color 0.3s ease; on the button and change background-color on hover.
3
Add reduced motion media query to disable animation
Inside the existing <style> block, add a CSS media query @media (prefers-reduced-motion: reduce). Inside it, select button.btn-primary and set transition: none; to disable the animation for users who prefer reduced motion.
Bootsrap
Hint
Use @media (prefers-reduced-motion: reduce) and inside it set transition: none; on the button.
4
Add accessible aria-label and finalize structure
Add an aria-label="Animated primary button" attribute to the <button> element for accessibility. Also, ensure the <main> element has a role="main" attribute to define the main content landmark.
Bootsrap
Hint
Add aria-label="Animated primary button" to the button and role="main" to the main element.
Practice
(1/5)
1. What is the main purpose of using @media (prefers-reduced-motion: reduce) in Bootstrap?
easy
A. To increase the speed of all animations on the page
B. To detect if the user prefers fewer animations for accessibility
C. To add more animations for visual appeal
D. To change the color scheme based on user preference
Solution
Step 1: Understand the media query purpose
The media query @media (prefers-reduced-motion: reduce) checks if the user has requested reduced motion in their system settings.
Step 2: Connect to accessibility goals
This helps improve accessibility by reducing or disabling animations that might cause discomfort or distraction.
Final Answer:
To detect if the user prefers fewer animations for accessibility -> Option B
Quick Check:
Reduced motion media query = detect user preference [OK]
Hint: Look for user preference detection in media queries [OK]
Common Mistakes:
Thinking it speeds up animations
Assuming it adds more animations
Confusing it with color scheme changes
2. Which of the following is the correct syntax to start a CSS block that applies styles only when the user prefers reduced motion?
easy
A. @media (prefers-reduced-motion: no-preference) { }
B. @media (motion-preference: reduce) { }
C. @media (prefers-reduced-motion: reduce) { }
D. @media (prefers-motion: reduce) { }
Solution
Step 1: Recall the exact media query syntax
The correct media query to detect reduced motion preference is @media (prefers-reduced-motion: reduce).
Step 2: Compare options
@media (prefers-reduced-motion: reduce) { } matches the exact syntax. Other options have incorrect property names or values.
Final Answer:
@media (prefers-reduced-motion: reduce) { } -> Option C
The media query must have parentheses around the feature: @media (prefers-reduced-motion: reduce).
Step 2: Identify the missing parentheses
The given code misses parentheses, so the media query is invalid and ignored by browsers.
Final Answer:
Missing parentheses around the media feature -> Option A
Quick Check:
Media queries require parentheses [OK]
Hint: Always wrap media features in parentheses [OK]
Common Mistakes:
Forgetting parentheses in media queries
Using wrong CSS selectors
Thinking animation:none is invalid
5. You want to improve accessibility by disabling Bootstrap's fade animations for users who prefer reduced motion. Which CSS snippet correctly achieves this?