Focus indicators

Focus Indicators, Logical Tab Order #

Description:

Focus indicators and logical tab order are essential elements of accessible design, especially for users who rely on keyboards or assistive devices for navigation. Focus indicators help users visually track their position on the page, while a logical tab order ensures that users navigate through content in an intuitive, sequential flow.

Why Focus Management Matters #

  • Prevents Disorientation: Without proper focus management, users may become lost or disoriented when new content appears.
  • Supports Screen Reader Users: Screen readers rely on focus cues to navigate content, so managing focus is essential when content changes.
  • Improves Usability: Proper focus handling in dynamic content improves the experience for all users, especially those using keyboards or assistive technologies.

Key Accessibility Practices for Focus Indicators and Logical Tab Order #

  1. Focus Indicators:
    • Provide Visible Focus Styles: Ensure all interactive elements, like buttons, links, form fields, and navigation items, have visible focus indicators. A clear outline, background color change, or border is effective for helping users identify which element is in focus.
    • Customize Focus Styles: You can improve the default browser focus style with a custom outline that has a contrasting color, thickness, or glow to make the indicator more prominent. However, avoid removing focus styles entirely, as this makes navigation difficult for keyboard users.
    • Consistent Focus Styles: Use consistent focus styles across the website for familiarity. For example, always use a 3px solid outline for interactive elements to make it easy for users to recognize focused items across different pages.
    • Avoid Overlapping Focus Indicators: Ensure that focus indicators do not overlap with other elements. For instance, dropdown menus or modal dialogs should not obscure focus indicators, as this can confuse users who rely on visual cues.
  2. Logical Tab Order:
    • Natural Tab Order: Structure the HTML to follow the logical reading order of the page (usually left to right, top to bottom) so that the default tab order is intuitive. This helps users move through the page without unexpected jumps.
    • Set Tab Index Properly: Avoid setting a tabindex value other than 0 or -1 unless necessary. Elements with tabindex="0" are part of the normal tab flow, while tabindex="-1" removes elements from the tab order but keeps them accessible to screen readers. Use positive tabindex values sparingly, as they can create an unnatural tab order.
    • Skip Links: Add a “Skip to Main Content” link at the beginning of the page, allowing users to bypass repetitive navigation and jump directly to the main content. This is particularly helpful for screen reader and keyboard users.
    • Modal Dialogs and Pop-ups: When a modal dialog or pop-up opens, move focus to the dialog and trap focus within it until the dialog is closed. This prevents users from accidentally navigating outside the modal, which can be confusing.

Here’s an example of a simple form with a clear focus indicator and a logical tab order.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Accessible Form with Focus Indicators and Logical Tab Order</title>
    <style>
        /* Custom focus indicator */
        .focusable:focus {
            outline: 3px solid #FFBF00; /* Yellow border for high contrast */
        }
        /* Basic styling for form elements */
        body {
            font-family: Arial, sans-serif;
        }
        form {
            width: 300px;
            margin: 20px;
        }
        label {
            display: block;
            margin-top: 15px;
        }
        input, button {
            width: 100%;
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #007BFF;
            color: white;
            font-size: 1em;
            cursor: pointer;
            border: none;
        }
        button:focus {
            outline: 3px solid #FFD700; /* Yellow focus outline for buttons */
        }
    </style>
</head>
<body>
    <!-- Skip Link for Better Navigation -->
    <a href="#main-content" class="focusable" style="position: absolute; left: -9999px;">Skip to Main Content</a>
    <h1>Accessible Form Example</h1>
    <!-- Main Content -->
    <main id="main-content">
        <form>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" class="focusable" tabindex="0">
            <label for="email">Email:</label>
            <input type="email" id="email" name="email" class="focusable" tabindex="0">
            <label for="message">Message:</label>
            <textarea id="message" name="message" class="focusable" tabindex="0"></textarea>
            <button type="submit" class="focusable" tabindex="0">Submit</button>
        </form>
    </main>
</body>
</html>

Example of Bad Practices: Inaccessible Focus Indicators and Disordered Tab Order #

The following example illustrates some common mistakes that reduce accessibility, such as removing focus indicators, using disorganized tabindex values, and creating confusing navigation paths.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bad Practices Example</title>
    <style>
        /* Bad practice: Removing focus indicators */
        button:focus, input:focus, a:focus {
            outline: none;
        }
        /* Unnecessary positive tabindex values */
        .skip-link {
            position: absolute;
            top: -9999px;
            left: -9999px;
        }
    </style>
</head>
<body>
    <!-- Disordered Tab Order with Positive Tabindex -->
    <a href="#main-content" class="skip-link" tabindex="10">Skip to Main Content</a>
    <h1 tabindex="3">Welcome to Our Website</h1>
    
    <form>
        <!-- Skipping around tab order by using arbitrary positive tabindex values -->
        <label for="name" tabindex="2">Name:</label>
        <input type="text" id="name" name="name" tabindex="5">
        <label for="email" tabindex="1">Email:</label>
        <input type="email" id="email" name="email" tabindex="4">
        <!-- Lack of focus indicator and unnecessary tabindex -->
        <button type="submit" tabindex="6">Submit</button>
    </form>
    <div id="main-content" tabindex="9">
        <p>Here is the main content of the page.</p>
    </div>
</body>
</html>

Explanation #

  1. Clear Focus Indicators:
    • Each interactive element (input, textarea, and button) has a focus indicator using .focusable:focus with a yellow outline for high contrast. This helps keyboard users track their position on the page.
  2. Skip Link for Efficient Navigation:
    • A “Skip to Main Content” link at the top of the page allows users to bypass repetitive content and go directly to the form. The skip link is only visible when focused, reducing visual clutter for users who don’t need it.
  3. Logical Tab Order:
    • The form’s tab order follows a natural flow from top to bottom, left to right, matching the reading order. This helps users navigate without unexpected jumps, creating a smooth experience.
    • Each form element has tabindex="0", which is the default tab order, keeping the navigation intuitive.

Key Takeaways #

  • Use Visible Focus Indicators: Apply custom focus styles to enhance the default browser focus indicator, ensuring users can easily see which element is in focus. Use contrasting colors and consistent styles across the site for clarity.
  • Maintain a Logical Tab Order: Structure the HTML and set tabindex to follow the natural reading order, usually top to bottom and left to right. Avoid using positive tabindex values, as they can disrupt the flow.
  • Add Skip Links: Provide a “Skip to Main Content” link at the start of the page to help keyboard and screen reader users bypass repetitive navigation.
  • Trap Focus in Modals: When opening modal dialogs or pop-ups, trap the focus within the modal and restore it to the trigger element upon closing. This prevents users from navigating outside the modal unintentionally.


Screen reader: NVDA
Video:


Screen reader: NVDA
Video: #

What are your feelings

Updated on March 5, 2025