WCAG 2.4.7: Focus Visible

Introduction to WCAG 2.4.7 Focus Visible

WCAG (Web Content Accessibility Guidelines) 2.4.7, titled Focus Visible, is a Level AA success criterion that mandates a clear and visible indicator for any user interface component that receives keyboard focus. In essence, when a user navigates a website or application using only a keyboard (e.g., by pressing the Tab key), there must be a visible cue that clearly shows which element is currently active or ‘in focus’.

This criterion ensures that users can always discern their current position on a page, allowing them to interact confidently and effectively with content and controls.

Why Focus Visible Matters for Accessibility

The absence of a visible focus indicator can render a website virtually unusable for a significant portion of the population. It’s a foundational aspect of accessible design, directly impacting user experience and navigational efficiency.

Accessibility Impact

  • Orientation and Navigation: Without a clear focus indicator, keyboard users lose their sense of place. They cannot tell which element they are currently on, making it impossible to predict what action will occur if they press Enter or Space.
  • Error Prevention: By knowing exactly where focus is, users can avoid unintended actions or navigating past important content without interacting with it.
  • Efficiency: A visible focus helps users quickly scan the page and determine if they’ve reached their desired interactive element, reducing frustration and wasted effort.

User Groups Affected

A wide range of users benefits from clear focus indicators:

  • Keyboard-only Users: Individuals who cannot use a mouse due to motor impairments, hand tremors, or who prefer keyboard navigation. This group relies solely on the keyboard for interaction.
  • Users with Low Vision: People who may struggle to track the mouse cursor or perceive subtle changes in color or position without a strong visual cue. A prominent focus indicator helps them locate the active element.
  • Users with Cognitive Disabilities: Individuals who benefit from clear and consistent visual cues to understand the state of an interface. A visible focus indicator provides essential feedback about interaction.
  • Users of Screen Magnifiers: When a screen is significantly magnified, the visible area is reduced. A strong focus indicator helps these users quickly locate the interactive element within their magnified view.
  • Power Users and Developers: Many users, regardless of ability, find keyboard navigation efficient for tasks like filling forms or navigating complex interfaces.

Success Criterion 2.4.7: Focus Visible (Level AA)

The official wording for this success criterion is:

2.4.7 Focus Visible (Level AA): Any user interface component that has keyboard focus can be made visually obvious.

This means that whenever an interactive element (like a link, button, form field, or custom widget) receives keyboard focus, there must be a distinct visual change that clearly differentiates it from other elements and from its non-focused state. The change must be sufficient for a user to readily perceive it.

Requirements

  • All Interactive Elements: Applies to all elements that can receive keyboard focus.
  • Visually Obvious: The indicator must be prominent enough to be easily noticed by a user. Subtle changes that are difficult to perceive (e.g., a 1px change in border color on a very busy page) may not meet the criterion.
  • Programmatic Focus: Even if focus is shifted programmatically (e.g., after a modal opens), the newly focused element must still display a visible indicator.

Practical Guidelines for Compliance

Achieving compliance with WCAG 2.4.7 is primarily a design and CSS implementation task, though JavaScript may be involved for custom components.

Do Not Remove Default Focus Styles Blindly

Browsers provide default focus indicators (often an outline). Many developers remove these for aesthetic reasons using outline: none; or border: 0; without providing a suitable alternative. This is one of the most common WCAG failures for 2.4.7.

Implement Custom Focus Styles (If Necessary)

If default outlines are removed, or if a more branded or prominent focus style is desired, ensure a custom style is applied using CSS pseudo-class :focus (or :focus-visible for more nuanced control).

Effective Focus Indicator Properties:

  • outline: This is the most semantically correct and least disruptive property, as it doesn’t typically affect layout.
  • box-shadow: Can create an excellent, non-disruptive halo effect.
  • border: Can be effective, but be mindful of layout shifts (e.g., if a 1px border is added, the element might jump or expand). Use `border-width` on the non-focused state to prevent this.
  • background-color: A significant change in background color can work, especially when combined with other cues.
  • text-decoration: For text links, an underline (or a bolder underline) can serve as part of the focus indicator.
  • transform: Minor scaling or translation can be part of a robust indicator, but ensure it doesn’t cause disorientation.

Key Considerations for Custom Styles:

  • Contrast: The focus indicator must have sufficient contrast against the element’s background and surrounding content. There isn’t a strict contrast ratio for the focus indicator itself in WCAG 2.x, but general good practice suggests ensuring it is clearly distinguishable. WCAG 2.2 introduced 2.4.11 Focus Appearance, which does specify contrast for the focus indicator, but for 2.4.7 it’s about being “visually obvious”.
  • Thickness/Prominence: A 1px border might be too subtle. Aim for something easily perceivable, like a 2px outline or a shadow.
  • Consistency: Use a consistent focus style across all interactive elements on your site for predictability.
  • Accessibility for All: Ensure the indicator is visible regardless of the user’s color perception (don’t rely solely on color changes).

Using :focus-visible (Recommended for Modern Browsers)

The CSS pseudo-class :focus-visible allows browsers to apply a focus indicator only when it makes sense. Typically, this means applying the focus indicator when using keyboard navigation, but not when clicking with a mouse (as a mouse user already knows where they clicked). This provides a better experience for mouse users without sacrificing keyboard accessibility.

  • It’s supported in most modern browsers.
  • It works in conjunction with the user’s browser settings and input modality.

Examples of Correct and Incorrect Implementations

Correct Implementations

These examples demonstrate how to provide clear and visible focus indicators.

HTML Structure:

<button class="example-button">Click Me</button>
<a href="#" class="example-link">Learn More</a>
<input type="text" class="example-input" placeholder="Enter text">

CSS for a prominent outline:

.example-button:focus,
.example-link:focus,
.example-input:focus {
  outline: 3px solid blue; /* Strong, clear outline */
  outline-offset: 2px;     /* Adds a little space around the element */
}

/* Using :focus-visible for a better user experience */
.example-button:focus-visible,
.example-link:focus-visible,
.example-input:focus-visible {
  outline: 3px solid blue;
  outline-offset: 2px;
}

/* Example with box-shadow */
.example-button.shadow-focus:focus-visible {
  outline: none; /* Remove default outline if using box-shadow exclusively */
  box-shadow: 0 0 0 4px dodgerblue; /* Creates a halo effect */
}

CSS for a combined focus style (border + background):

.example-button.combined-focus {
  border: 2px solid transparent; /* Prevent layout shift when focus border is added */
  padding: 8px 15px; /* Adjust padding if needed */
}

.example-button.combined-focus:focus-visible {
  border-color: green; /* Visible border */
  background-color: lightgreen; /* Subtle background change */
  box-shadow: 0 0 5px rgba(0, 128, 0, 0.5); /* Additional shadow */
}

Incorrect Implementations

These examples show common mistakes that lead to non-compliance.

HTML Structure (same as above):

<button class="bad-button">Click Me</button>
<a href="#" class="bad-link">Learn More</a>
<input type="text" class="bad-input" placeholder="Enter text">

CSS that removes focus without replacement:

.bad-button:focus,
.bad-link:focus,
.bad-input:focus {
  outline: none; /* Removes the browser's default focus indicator */
}

/* Or with a subtle, hard-to-perceive change */
.bad-button.subtle:focus {
  background-color: #f0f0f0; /* Too subtle on a white background */
  border-color: #ccc;      /* Almost invisible */
}

CSS where focus is hidden:

/* Imagine an element that is visually covered by another element on focus */
.covered-element {
  position: relative;
  z-index: 1;
}

.covering-element {
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
  background: rgba(0,0,0,0.5); /* Covers the element below */
  z-index: 2;
}

/* If the covered-element receives focus, its indicator might be hidden */
.covered-element:focus {
  outline: 2px solid red; /* But it's not visible due to covering-element */
}

Best Practices and Common Pitfalls

Best Practices

  • Do Not Suppress Default Focus Styles: Unless you are providing a more visible and accessible alternative. The default browser outlines are generally compliant.
  • Test with Keyboard Only: Always use the Tab key, Shift+Tab, and Enter/Spacebar to navigate your site. Ensure all interactive elements show a clear focus indicator.
  • Ensure Contrast and Prominence: Make your custom focus indicators bold and distinct. Consider a combination of properties (e.g., outline + box-shadow).
  • Consistency Across Components: Maintain a consistent visual style for focus indicators throughout your entire website or application.
  • Use :focus-visible: Implement this pseudo-class for a better user experience, allowing focus styles to appear only when needed (e.g., keyboard navigation).
  • Address All Interactive Elements: This includes links, buttons, form fields, checkboxes, radio buttons, select elements, custom widgets (e.g., tabs, accordions, carousels), and skip links.
  • Consider Hover States: While hover states are for mouse users, ensure your focus state is distinct from your hover state. Sometimes, developers reuse hover styles for focus, which might not be sufficiently prominent.

Common Pitfalls

  • outline: none;: The most frequent mistake. Avoid this CSS property unless immediately replaced by a robust custom focus style.
  • Subtle Changes: Using only a slight color change (e.g., from light gray to slightly darker gray) that is hard to perceive, especially for users with low vision or color blindness.
  • Focus Indicator Hidden: The focus indicator appearing outside the viewport or being obscured by other overlapping elements (e.g., fixed headers/footers, modals).
  • Inconsistent Focus Styles: Different interactive elements having wildly different or missing focus styles, leading to a confusing user experience.
  • Focus Not Applied: Forgetting to apply focus styles to all interactive elements, particularly custom components built with JavaScript.
  • Relying Solely on Mouse Clicks: Testing only by clicking elements with a mouse, which doesn’t reveal keyboard navigation issues.

Conclusion

WCAG 2.4.7 Focus Visible is a critical component of web accessibility, ensuring that all users, especially those relying on keyboard navigation, can effectively interact with digital content. By providing clear, visible, and consistent focus indicators, you create a more usable, understandable, and equitable web experience for everyone. Always remember to test your interfaces thoroughly using only a keyboard to catch any focus-related issues.

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.