Web Development Pro | Lesson 018 | Focus, Hover, and Reduced Motion
Web Development Pro · Lesson 18 of 60
Module 2: CSS and responsive design
In this lesson
Prerequisites and learning outcomes
Complete lesson 17 first. This lesson builds on interaction states and user preferences. By the end, you can design usable visible states and provide reduced-motion alternatives. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
Design every interaction state
A link's resting appearance is only one part of its design. Visitors may hover with a pointer, focus with a keyboard, activate a control, or use a device without hover at all. Each state should preserve legibility and make the available action clear. A button that only becomes understandable on hover excludes people using touch or keyboard input.
Begin with a usable default. Keep descriptive text visible, maintain contrast, and make targets comfortably sized. Then add state changes that reinforce the action. Do not use a hover effect as the only way to reveal essential instructions or as a substitute for a real focus indicator.
Make focus unmistakable
Focus-visible lets a browser apply a focus style when it determines a visible indicator is appropriate, commonly during keyboard interaction. Keep a fallback focus style for environments or situations where it is needed. The exact heuristics are browser-controlled, so verify your real interaction rather than assuming every click and key produces the same indicator.
a { color: #174b36; text-decoration: underline; }
a:hover { text-decoration-thickness: 0.18em; }
a:focus, button:focus { outline: 3px solid #8a2400; outline-offset: 3px; }
a:focus:not(:focus-visible), button:focus:not(:focus-visible) { outline: none; }
a:focus-visible, button:focus-visible { outline: 3px solid #8a2400; outline-offset: 3px; }
.card { transition: transform 180ms ease; }
@media (hover: hover) {
.card:hover { transform: translateY(-2px); }
}
@media (prefers-reduced-motion: reduce) {
.card { transition: none; }
.card:hover { transform: none; }
}
In browsers that understand focus-visible, the middle rule removes the extra generic outline only when the browser says it is not the focus-visible state. Browsers that do not understand that selector retain the earlier focus rule. Do not copy outline: none without the rest of the strategy; that would remove an important navigation cue.
Keep indicators visible in context
An outline can extend outside the element's box, so a parent with overflow: hidden may clip it. A sticky header can also cover a focused link or fragment target. Testing only the color value misses these spatial problems. Move through the page with the keyboard and check that the focused control is actually visible, not merely styled somewhere offscreen.
The indicator must stand out against adjacent colors. An orange outline that works on white may disappear beside an orange panel. Review all contexts in which the component appears, including disabled or selected states where applicable. Component reuse does not automatically preserve contrast if its surroundings change.
Use more than color to show current navigation or selection. An underline, weight change, border, or text label can reinforce the meaning. The aria-current attribute from earlier lessons communicates the current page programmatically; CSS can style that attribute to provide a matching visible cue. Neither layer should contradict the other.
Respect motion preferences
Motion can help explain a state change, but large movements, parallax, and continuous animations can cause discomfort or distraction. The prefers-reduced-motion query reflects a user preference exposed by the browser. Provide a calmer alternative rather than assuming a short duration is harmless for everyone.
The card example uses a small optional lift on hover and removes both the transition and movement under reduced motion. Merely setting transition: none while retaining a large transform would still produce a sudden jump. Think about the actual visual effect you are removing. Essential information should never depend on an animation completing.
Avoid adding motion just because it is possible. A static underline may communicate a link state more clearly than a bouncing card. If movement is not helping the visitor understand or act, it adds complexity without a strong benefit. For this workshop site, restrained optional feedback is sufficient.
Consider input capabilities without excluding others
The hover media feature describes whether a primary input can conveniently hover. It does not prove the visitor will only use one input method. A laptop may have a touchscreen, mouse, and keyboard. Keep the core action usable in every case and use capability queries for optional enhancement.
Do not make a tooltip containing required information appear only on hover. A visible description or a properly designed disclosure is more reliable. If a custom popup is truly necessary, it needs careful keyboard, focus, dismissal, and pointer behavior beyond a simple CSS selector. Prefer the simpler native pattern until those requirements can be implemented and tested.
Review state changes systematically
Make a list of resting, hover, keyboard focus, active, and current states. Test each with actual controls and meaningful labels. Then enable the operating system's reduced-motion preference if available and inspect the page again. Static stylesheet checks only establish that relevant rules are present; they do not verify the browser's focus heuristic or the visual result.
Document any unrun tests clearly. The provided CSS is a starting implementation, not a certification that every assistive-technology or browser combination behaves perfectly. Good state design depends on both correct rules and observation of the complete interaction.
Worked practice
Worked exercise 1: Restore lost focus
Task: A reset stylesheet removes every outline. Worked answer: add a clear focus strategy such as the example, then navigate using Tab and Shift+Tab. Check each control against its actual background and ensure no ancestor clips the outline. A declaration's presence alone does not prove visibility.
Worked exercise 2: Reduce the whole effect
Task: Reduced-motion CSS removes a transition, but the card still jumps upward on hover. Worked answer: also remove the transform under the preference query. This avoids both the animated motion and the sudden positional change, while leaving the link's ordinary hover cue available.
Worked exercise 3: Replace hover-only information
Task: Preparation instructions appear only when hovering over an icon. Worked answer: place them in visible text or a native details disclosure with a meaningful summary. Test keyboard access and touch behavior rather than assuming hover is universally available.
Knowledge check
Question 1
What should a default control state provide? A) A label revealed only when a pointer enters. B) Enough information to understand the action without hover. C) An accessible name without any visible explanation. D) A visual cue that appears only during animation.
Check question 1: answer and explanation
Answer: B. Hover is optional enhancement, not a prerequisite for understanding.
Question 2
Why can a correct outline rule still fail users? A) An ancestor may clip the indicator. B) A large outline offset always prevents clipping. C) A sufficiently dark outline must remain visible under a sticky header. D) A focus rule automatically scrolls every obscured control into view.
Check question 2: answer and explanation
Answer: A. Visibility depends on surrounding layout as well as the rule itself.
Question 3
What does prefers-reduced-motion reflect? A) The measured frame rate of the current animation. B) A guaranteed slow device. C) An exposed user preference. D) A browser instruction to remove every animation automatically.
Check question 3: answer and explanation
Answer: C. It communicates a preference for reducing motion, not a performance benchmark.
Question 4
Why remove transform as well as transition in this example? A) To preserve the movement but make it instantaneous. B) To ensure the hover transform overrides the preference query. C) To make the base transition run under reduced motion. D) To avoid the remaining positional jump.
Check question 4: answer and explanation
Answer: D. Reduced motion should address the actual visual effect.
Question 5
What does a static rule check establish? A) Presence of the intended CSS rules. B) Every keyboard path passed. C) The focus ring was visibly observed. D) Every user preference was tested.
Check question 5: answer and explanation
Answer: A. Runtime and visual claims need additional evidence.