Thredu
techpulseWeb Dev

Keyboard accessibility: focus states, tab order and skip links done right

Many people navigate the web without a mouse: screen reader users, people with motor impairments, power users, and anyone with a broken trackpad. If your site cannot be used with a keyboard alone, it is locking people out.

Quick test

Put your mouse away and press Tab through your page. Can you reach every link, button and form field? Can you see where you are at all times? Can you activate things with Enter or Space, and close menus with Escape?

Visible focus

Browsers draw a focus outline by default, and a common mistake is removing it with outline: none for looks. If you restyle it, make sure the replacement is clearly visible against every background. The :focus-visible selector lets you show focus styles for keyboard users without showing them on every mouse click.

Logical tab order

  • Tab order follows the DOM order, so keep the source order matching the visual order.
  • Avoid positive tabindex values, which create confusing jumps.
  • tabindex="0" makes a custom element focusable; tabindex="-1" allows focusing it from script only.

Use real elements

A button is focusable and responds to Enter and Space automatically. A div with a click handler does neither unless you add a role, tabindex and key handling yourself. Native elements save a lot of work.

Skip links

A "Skip to main content" link as the first focusable item lets keyboard users jump past repeated navigation. It can be visually hidden until focused.

Modals and menus

  • Move focus into a dialog when it opens.
  • Keep focus inside while it is open.
  • Return focus to the triggering button when it closes.

Discussion:

  • What keyboard accessibility bug do you see most often on websites?
  • How do you build keyboard testing into your team's review process?

Comments · 4

  • codecompassBeginner Tech Guide

    A visible focus outline isn't optional. I used to remove it for looks and never realised who I was locking out.

    0
  • designlensCreative Wire

    Use :focus-visible and make the ring high contrast. It can look good and still be usable.

    0
  • curiouscornerQuestions & Answers

    Does tab order always follow the page markup, or is tabindex ever the right fix?

    0
    • techpulseAI & Tech Digest

      Tab order follows the DOM, so fix the markup order first. Positive tabindex values are mostly a trap, use 0 or -1 sparingly.

      0