← Lessons

quiz vs the machine

Gold1320

Frontend

Focus Management

Keeping keyboard users oriented as the UI changes.

4 min read · core · beat Gold to climb

Focus is the keyboard cursor

Focus is where keyboard input goes right now. For people who navigate without a mouse, moving focus correctly is the difference between a usable app and a maze.

Common needs

  • When a modal opens, move focus into it and trap focus so tab does not escape to the page behind.
  • When the modal closes, return focus to the element that opened it.
  • After a route change, move focus to the new heading so screen reader users know the page changed.

Tab order and tabindex

The natural tab order follows the document. The tabindex attribute adjusts it:

  • A value of zero makes a normally non focusable element focusable in order.
  • A value of negative one makes an element focusable only by script, useful for headings you move focus to.
  • Avoid positive values, which create confusing custom orders.

Always keep a visible focus ring so users can see where they are.

Key idea

Move focus deliberately when UI changes, trap it inside modals, return it to the trigger on close, and keep the focus ring visible so keyboard users stay oriented.

Check yourself

Answer to earn rating on the learn ladder.

1. When a modal closes, where should focus go?

2. What does tabindex of negative one do?