Skip to article content
FrontendXLesson 12 / 30

Keyboard navigation and focus management

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

intermediate · about 35 minutes · 2 exercises

  • Predict
  • Debug

What you will be able to do

#
  • Explain the difference between focus order and DOM order.
  • Manage focus when opening and closing overlays.

Overview

#

Keyboard users move through a page with Tab and Shift+Tab. The order follows the DOM, not the visual layout, so a CSS-only reordering can produce a focus order that jumps around the screen. If you reorder with order or grid-row, check that the tab sequence still matches what users see.

Some things are focusable and should not be. A positive tabindex value, such as tabindex="1", removes an element from the natural order and forces it to the front, which almost always creates a confusing sequence. Use tabindex="0" to add an element to the natural order, and tabindex="-1" to make it focusable only by script, which is what you use for focus management.

snippet.html
<button id="open">Open dialog</button>
<div role="dialog" aria-modal="true" aria-labelledby="dialog-title" tabindex="-1" id="dialog">
  <h2 id="dialog-title">Confirm</h2>
  <button id="close">Close</button>
</div>

When a dialog opens, move focus into it, usually to the dialog container or its first control. Keep focus inside while it is open, and return focus to the element that opened it when it closes. Without this, keyboard focus stays behind the overlay and the user is lost. A visible focus indicator is mandatory: never remove the outline without providing a clear replacement.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This dialog opens but keyboard focus stays on the button behind it. What attribute and focus step are missing?

    starter.html
    <div role="dialog" aria-modal="true" aria-labelledby="t">
      <h2 id="t">Confirm</h2>
      <button>Close</button>
    </div>

    Reveal the reference answer
    reference.html
    <div role="dialog" aria-modal="true" aria-labelledby="t" tabindex="-1" id="dialog">
      <h2 id="t">Confirm</h2>
      <button>Close</button>
    </div>
    <!-- on open: dialog.focus(); on close: opener.focus() -->

    Authored expected output: The dialog container has tabindex="-1" and the open handler moves focus into it, returning focus to the opener on close.

    tabindex="-1" makes the container programmatically focusable so script can move focus into the dialog.

  2. Exercise 2Predict

    What does tabindex="-1" do to an element?

    Choose the best answer

Common misconceptions

#
  • Positive tabindex is a good way to control focus order.
  • Removing the focus outline is fine because the design looks cleaner.

Follow-up practice

#
  • html-css-13-color-contrast

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.