Advanced Accessibility (A11y)
Master the inclusive web. Learn how to support High Contrast Mode, manage keyboard focus, and build accessible components that pass WCAG 2.2 with flying colors.
Expert Answer & Key Takeaways
Master the inclusive web. Learn how to support High Contrast Mode, manage keyboard focus, and build accessible components that pass WCAG 2.2 with flying colors.
1. High Contrast Mode (Forced Colors)
Some users with low vision use 'Windows High Contrast Mode' which forces the browser to use a very limited, hyper-legible color set (e.g., only White, Black, and Yellow).
Styling for Forced Colors:
@media (forced-colors: active) {
.card {
/* Ensure the border is visible in high contrast */
border: 2px solid CanvasText;
/* Remove any background colors that might hide text */
background-color: Canvas;
}
}| Media Feature | Purpose | Typical Use Case |
|---|---|---|
forced-colors | Detects OS High Contrast mode. | Overriding gradients and background images. |
prefers-contrast | Detects a user desire for more contrast. | Darker text, better outlines. |
focus-visible | Only shows focus when keyboard is used. | Avoiding ugly 'blue' rings for mouse clicks. |
2. Focus Management Architecture
A website is unusable for keyboard users if they can't see 'where they are' on the screen. CSS should make the focus look beautiful, but always present.
The Example: The Offset Focus Ring
/* ❌ Standard ring (looks messy on some elements) */
a:focus { outline: 2px solid blue; }
/* ✅ The Professional Focus (Offset and Clear) */
button:focus-visible {
outline: 4px solid var(--accent-color);
outline-offset: 4px;
border-radius: 4px;
}3. Screen Reader-Only Styles
Sometimes you need to hide content visually but keep it available for Screen Readers (like a label that is implied by an icon).
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0); /* Legacy */
clip-path: inset(50%); /* Modern */
border: 0;
}[!IMPORTANT] Avoid 'display: none' for Accessibility: If you usedisplay: noneorvisibility: hidden, the element is hidden from BOTH the screen and the Screen Reader. Use the.sr-onlypattern if you want the text to be read out but not seen.
🎯 Practice Challenge: The Accessible Button
- Create a button with a background image icon and NO text.
- Task 1: Add a span inside the button with the label 'Delete' and apply the
.sr-onlyclass to it. - Task 2: Add a
:focus-visiblestate that adds a 4px outline with an offset. - Task 3: Add an
@media (forced-colors: active)block that ensures the button's background becomes a high-contrast system color. - Observation: Notice how the button remains accessible to screen readers, keyboards, and high-contrast users without changing the visual design for standard users.
4. Senior Interview Corner
Q: What is the difference between WCAG 'AA' and 'AAA' standards for color contrast?
A: WCAG AA requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text. WCAG AAA is the stricter 'gold standard,' requiring 7:1 for normal text and 4.5:1 for large text. Many enterprise and government websites are legally required to meet the 4.5:1 (AA) standard. As architects, we should always strive for 'AA' as a baseline and 'AAA' for core interactive components.
Q: When should you use 'prefers-contrast: more'?
A: This is a user-preference feature. If a user has enabled this in their OS, we should use CSS to remove thin fonts, reduce transparency (like backdrop-blur), and increase the darkness of text against backgrounds. This isn't just for 'low vision' users—it's also for anyone working in high-glare environments (like outdoors on a phone).
Top Interview Questions
?Interview Question
Q:Which CSS class pattern is used to hide text visually while keeping it readable for Screen Readers?
A:
.sr-only
?Interview Question
Q:What is the primary benefit of using :focus-visible instead of just :focus?
A:
It only shows the focus ring when using a keyboard, preventing ugly rings for mouse clicks
Course4All Engineering Team
Verified ExpertFrontend Architects
Focused on layout performance, modern CSS4+ features, and responsive design, our team provides the blueprint for professional web interfaces.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.