Keyboard Navigation and Focus Management

Make every interaction work without a mouse: tab order, visible focus, skip links, focus traps in modals and focus on route changes.

Intermediate · about 3 hours · difficulty 3/5

Learn first: ARIA and Accessible Custom Widgets

Many people navigate entirely by keyboard or with switch devices and voice control that emulate one. If something cannot be reached or operated with the keyboard, it is broken for them.

Basics

  • Tab and Shift+Tab move between interactive elements; Enter activates links and buttons; Space toggles buttons and checkboxes; arrow keys move within composite widgets (tabs, menus, radio groups); Escape closes overlays.
  • Focus order should follow visual order. Rearranging with CSS (order, absolute positioning) can make it jump around confusingly.
  • Use tabindex="0" to make a custom widget focusable, tabindex="-1" to focus something programmatically, and never positive tabindex values.
  • Style :focus-visible with a clear outline; never remove outlines without a replacement.

Focus management in apps

  • Modals: move focus into the dialog on open, keep it trapped inside, close on Escape and return focus to the trigger on close. Native <dialog> with showModal() does most of this, and the inert attribute disables background content.
  • Route changes in SPAs: move focus to the new page's main heading or announce the page title, otherwise screen reader users do not know anything changed.
  • Deleting items or closing panels: send focus somewhere sensible, not to <body>.
  • Skip link: a first focusable "Skip to main content" link helps keyboard users bypass repeated navigation.
  • Roving tabindex for toolbars and lists: one tab stop, arrow keys inside.

Good enough to move on

You can unplug your mouse and complete every flow in your app, with a visible focus ring, a skip link, a correct modal focus trap and focus moved on navigation.

Key concepts

  • Standard keyboard interactions
  • Logical focus order
  • tabindex 0 and -1
  • Visible focus with :focus-visible
  • Modal focus trapping and restoring
  • Focus on SPA route changes
  • Skip links
  • Roving tabindex

Mini project

Make your app fully keyboard operable: add a skip link, visible focus styles, a modal with focus trap and restore, focus management after route changes and after deleting a list item.

Check yourself

After a modal closes, where should keyboard focus go, and what happens if you do nothing?

Free resources