Color Contrast Requirements for Focus
What you'll learn: How to ensure your custom focus indicators meet WCAG accessibility standards by maintaining sufficient color contrast.
When you design custom focus indicators (those outlines or borders that show which element is selected), you can't just pick any color you like. The focus indicator must be clearly visible against both the element's background and the page background around it. This is where WCAG 2.4.7 comes in with specific mathematical requirements.
The 3:1 Contrast Ratio Rule
WCAG Success Criterion 2.4.7 (Level AA) requires that the visual indicator showing keyboard focus must have a contrast ratio of at least 3:1 between:
- The focus indicator color and the non-focused state
- The focus indicator and adjacent colors
Think of it like a highlighter marker: if you use yellow highlighter on yellow paper, no one can see what you marked. The colors need enough difference (contrast) to be distinguishable.
Measuring Contrast
The contrast ratio is calculated mathematically using relative luminance values, ranging from 1:1 (no contrast, like white on white) to 21:1 (maximum contrast, like black on white). A 3:1 ratio means the lighter color is three times as bright as the darker one.
For example:
- A dark blue outline (
#0000FF) on a white background (#FFFFFF) has excellent contrast (8.6:1) - A light gray outline (
#CCCCCC) on white (#FFFFFF) fails (1.6:1)
Use contrast checker tools to verify your focus styles meet the 3:1 minimum threshold. This ensures users with low vision or color blindness can still see which element has keyboard focus.
Key Takeaway: Your custom focus indicators must maintain at least a 3:1 contrast ratio against their surroundings to meet WCAG 2.4.7 Level AA accessibility requirements.