Accessible Coding

Visible Focus and Focus Management

Effective keyboard navigation and clear visual cues are fundamental to creating accessible digital content for all users, especially those who cannot use a mouse. This chapter explores two key aspects of keyboard navigation: visible focus indicators and proper focus management.

focus state collage

What is Visible Focus?

Visible focus refers to the visual cues that show which interactive element on a web page is currently selected or active when a user is navigating using a keyboard.

For example, when tabbing through links or form fields, a visible outline, highlight, or border appears around the active element. 

The purpose of visible focus is to keep keyboard-only users informed of their current location on the page. Think of it as a breadcrumb, something that guides users through interactive elements more easily. 

What is Focus Management?

Focus management is the practice of controlling the logical order in which the cursor moves between interactive elements on a web page, particularly during keyboard navigation. When a new element appears, such as a pop-up dialog, modal, or form, focus should automatically move to that element so users know it requires attention. Once the element is dismissed, focus should return to a meaningful place, like the button that opened the dialog. 

Proper focus management ensures a predictable and logical navigation flow, preventing users from getting lost or trapped. This also helps improve navigation efficiency and is essential for making interactive elements accessible to keyboard users and people relying on screen readers.

Quiz Yourself

What’s the difference between visible focus and focus management?

@

Not Quite!

Hint: Think about the difference between seeing where you are on a page versus controlling where you go on a page.

How Do Visible Focus and Focus Management Impact Accessibility?

Both visible focus and focus management are essential for making interactive elements accessible to everyone.

Visible Focus

Visible focus indicators are crucial for keyboard-only users who rely on these visual cues to know where they are on the page and to interact effectively with elements like links, buttons, menu items, and form fields. Without a clear indicator, these users would be unable to tell which element is currently selected, making navigation impossible.

Focus Management

Focus management prevents user confusion or disorientation. When elements are styled with CSS or have a tabindex that causes the focus order to deviate from the visual or source order, it creates significant problems. This disruption impacts both visual keyboard users and screen reader users, making the page difficult to understand and operate. Focus management ensures consistent, logical keyboard interactions that mirror mouse navigation, which is vital for providing a usable and predictable experience for all users.

What WCAG Success Criteria are Relevant for Visible Focus and Focus Management?

Several WCAG success criteria directly apply to visible focus and focus management, helping ensure that users can reliably navigate and interact with web content. 

Visible Focus

  • WCAG 2.4.7 Focus Visible: This success criterion mandates that any keyboard-operable user interface must have a mode of operation in which the keyboard focus indicator is visible.

  • WCAG 1.4.11 Non-text Contrast: This criterion requires that meaningful visual cues, which include focus indicators, achieve a minimum contrast ratio of 3:1 against the background. This ensures that the focus indicator is visible even for individuals with low contrast sensitivity.

Focus Management

  • WCAG 2.4.3 Focus Order: This criterion states that if a web page can be navigated sequentially and this sequence affects meaning or operation, then focusable components must receive focus in an order that preserves meaning and operability.

  • WCAG 2.1.2 No Keyboard Trap: This essential criterion dictates that if keyboard focus can be moved to a component, then focus must also be able to be moved away from that component using a standard method, or the user must be advised of a non-standard navigation method. This is particularly critical for elements like pop-up dialogs.

How Do I Maximize Accessibility When I Code Visible Focus and Focus Management?

Ensuring visible focus and proper focus management is critical for keyboard and assistive technology users. Proper implementation improves usability, prevents confusion, and ensures compliance with WCAG success criteria like Focus Visible, Focus Order, and No Keyboard Trap. 

Below are a few ways to maximize accessibility around both concepts.

Visible Focus

As mentioned above, visible focus informs users which element currently has keyboard focus, helping them navigate forms, menus, links, and interactive components. Best practices include:

  • CSS pseudo-classes: Use :focus or :focus-visible to provide clear visual cues for focused elements.

  • Complex forms: Use :focus-within on <fieldset> containers to highlight an entire group when any element inside is focused.

  • Contrast and styling: Ensure focus indicators meet contrast requirements (WCAG 1.4.11) and are easy to see against the background.

Focus Management

Proper focus management ensures that keyboard and screen reader users can navigate content logically and predictably. Key strategies include:

  • Prioritize semantic HTML and native controls: Using semantic HTML elements (e.g., <button>, <input>, <dialog>) and native controls ensures better accessibility and a more reliable user experience across platforms. They come with built-in accessibility features and predictable behaviors, reducing the need for custom coding and testing. The general principle is "no ARIA is best" if semantic HTML can achieve the desired functionality.

  • Custom controls: When non-semantic elements like div and span are used for custom controls, you must manually assign proper ARIA roles, provide accessible names, manage states and properties with ARIA and custom events, and implement keyboard navigation. This process is prone to errors and can create barriers to accessibility.

  • Logical focus order: Avoid styling elements with CSS or using a tabindex value other than -1 or 0 in ways that cause the focus order to deviate from the visual or source order of the page. Such deviations confuse users and disrupt the page's meaning and operability.

  • Keyboard interaction methods: To effectively manage cursor focus for keyboard navigation, utilize either the activedescendant or element.focus methods.

Pop-up dialogs: Pop-up dialogs (e.g., a cookie banner or special offer) require special attention because they can trap focus or prevent users from accessing other content:

  • Semantic dialogs: Use the <dialog> element whenever possible, as it provides built-in focus control.

  • Source order: If using custom pop-ups, place the code at the top of the source order so screen readers encounter it early.

  • Modal dialogs: Maintain focus within the modal until it is dismissed.

  • Non-modal dialogs: Close or release focus when interaction is optional or lost.

  • Keyboard operability: Users must not be trapped within a dialog and should be able to move past it using common keystrokes like Tab, arrow keys, or the Escape key to close it.

Quiz Yourself

When a modal pop-up dialog appears, what is the best practice regarding keyboard focus?

@

Not Quite!

Hint: Think about what needs to happen so assistive technologies can interact with the dialog efficiently without getting lost or trapped on the page.

How Do I Test the Accessibility of Visible Focus and Focus Management?

Testing the accessibility of both visible focus and focus management requires a combination of both automated and hands-on testing. 

Keyboard Navigation Test (Manual)

Keyboard testing simulates users who cannot use a mouse. Steps include:

Navigation:

  • Use the Tab key to navigate forward through interactive elements (links, form controls).

  • Use Shift + Tab to navigate backward through interactive elements.

Activation:

  • Press Enter to activate links or buttons and to submit most forms.

  • Use the Spacebar to activate checkboxes and buttons, expand select menus, or scroll the window.

  • Use arrow keys to navigate radio buttons, select boxes, dropdown menus, sliders, tab panels, and tree menus.

  • Press the Escape key to close dialogs and menus.

What to Check

  • Confirm every focusable element has a focus indicator/outline with at least 3:1 color contrast.

  • Verify that the navigation order of the entire page is logical and intuitive.

  • Test dialogs and pop-ups: 

    • Focus should move into the dialog when it appears.

    • Modal dialogs must trap focus until dismissed; non-modal dialogs should release focus when interaction ends.

    • Focus should return to a logical location after dismissal.

Tip for Mac Safari users

In Safari settings, under Advanced > Accessibility, select "Press Tab to highlight each item on a webpage" to enable full keyboard navigation.

Automated Testing with WAVE

The WAVE browser extension (available for Chrome, Firefox, and Edge) helps detect accessibility issues and visualize navigation order. To test with WAVE, complete the following steps:

  • Run a scan on the page using WAVE.

  • Use the Order tab in the WAVE report to check the navigation sequence.

  • Confirm that the numbered focus order icons on the page match the intended logical flow.

  • Utilize the WAVE browser extension (available for Chrome, Firefox, and Edge) to evaluate web content for accessibility issues directly within your browser.

Automated testing is great for catching structural issues, but it should be paired with manual keyboard and screen reader testing for full coverage.

Screen Reader Testing

Screen reader testing gives crucial insights into how users relying on assistive technologies experience focus and navigation:

  • Use screen reader software such as VoiceOver (built-in for Apple products) or NVDA (a free download for Windows) to navigate the page.

  • Navigate using arrow keys (e.g., Control + Option + Right/Left arrow for VoiceOver) to test that the page navigates in a meaningful, predictable, and logical order.

  • Use the Tab key with the screen reader to verify the focus order of interactive elements.

  • Observe how pop-up dialogs behave: confirm that keyboard focus is driven to the pop-up to allow efficient interaction for screen reader users.

The approach is essential for confirming that both visual and programmatic focus cues are correct and that users are not trapped or disoriented.

Quiz Yourself

What is the minimum contrast ratio required for a visible keyboard focus indicator against its background, according to WCAG?

@

Not Quite!

Hint: Go back and review the section on how to test the accessibility of visible focus and focus management.

Quiz Yourself

Which WCAG success criterion primarily addresses the logical order in which focusable components receive focus?

@

Not Quite!

Hint: Think about which success criterion ensures keyboard users can navigate the page in a meaningful, predictable sequence.

How Can  I Learn More About Visible Focus and Focus Management?

  • Integrate Accessibility Testing into Your Workflow: Consistently incorporate the use of screen readers, keyboard navigation, and other accessibility testing tools into your development process. This hands-on approach helps bridge the gap between understanding accessibility guidelines and effectively implementing them.

  • Start Small: If you are new to building accessible digital content, begin by focusing on meeting all guidelines for a single type of element before moving on to more complex components.

  • Emphasize Thorough Testing: Even when adhering to technical guidelines, unexpected behaviors can arise, making thorough testing an essential part of ensuring robust accessibility.

Keep Learning

Move to the next chapter: Images.

Frequently Asked Questions