Loading...

HTML essentialsAdvanced

Keyboard navigation & focus management

6 min lesson

Next
  • Keyboard navigation means every interactive element can be reached and used with the Tab, Enter, Space and arrow keys.
  • Focus is the element that currently receives keyboard input; it is usually shown with an outline.
  • HTML gives focus order for free: links, buttons and form fields come in the order they appear in the source.
  • The tabindex attribute changes the order: tabindex="0" adds an element to the natural order; tabindex="-1" makes it focusable only by script.
  • Positive tabindex values like tabindex="2" cause confusing jumps and should be avoided.
  • This builds on Accessibility & ARIA: focus is the keyboard counterpart of ARIA roles.
  • The example shows a focusable link, a button and a heading made focusable for a skip target; you will see a clear focus outline as you Tab.
  • Next, SEO & meta tags moves from users to search engines.
Keep in mind
  • Let the natural source order decide the tab order; do not reorder with positive tabindex.
  • Never remove the focus outline without replacing it with a visible style.
  • Add tabindex="-1" to a heading you jump to, so the browser moves focus there.
  • Test every page with the Tab key; you should never get lost.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Keyboard focus</title>
  <style>
    /* Make the focus outline thicker and easier to see */
    a:focus, button:focus, h2:focus {
      outline: 3px solid orange;
    }
  </style>
</head>
<body>
  <!-- A normal link is focusable by default -->
  <a href="#details">Jump to details</a>

  <!-- A button is focusable and works with Enter and Space -->
  <button type="button">Click me</button>

  <!-- tabindex=-1 lets a heading be the target of a focus jump -->
  <h2 id="details" tabindex="-1">Details</h2>
  <p>This heading can receive focus when reached by a link.</p>
</body>
</html>

What happens: A page with the link "Jump to details", the button "Click me", and the heading "Details" with a visible orange outline when focused. Tabbing moves from the link to the button, and clicking the link jumps focus to the heading.

Breaking it down

natural focus order

<a> then <button>

Links, buttons and inputs enter the tab order in the order they appear in the HTML source. No code needed.

tabindex=0

<div tabindex="0">

Makes a normally non-focusable element part of the natural tab order. Use sparingly.

tabindex=-1

<h2 tabindex="-1">

Focusable only by script or by a link jump. Perfect for headings you navigate to.

focus style

a:focus { outline: 3px solid orange; }

Makes the focused element visible. A strong outline is essential for keyboard users.

Focus attributes

ValueEffect
tabindex="0"Adds element to natural order
tabindex="-1"Focusable only by script
tabindex="1"Jumps ahead of natural order (avoid)
autofocusFocuses an element on page load (avoid)

A helpful analogy

Focus is like a spotlight on a stage: the performer in the spotlight is the one you control. Tab moves the spotlight to the next performer in line, and a clear outline shows the audience where it is.

Nesting rules

  • 1.Never skip the focus outline; if you must restyle it, keep a visible replacement.
  • 2.Do not place interactive elements inside other interactive elements, like a link inside a button.

Common mistakes

Removing the outline to look clean

Avoid

a:focus { outline: none; }

Use instead

a:focus { outline: 3px solid orange; }

Without a visible outline, keyboard users cannot see where focus is and get lost.

Using positive tabindex values

Avoid

<input tabindex="1"> before <a href="#">

Use instead

Rearrange the source instead

Positive values jump out of order and create a confusing tab path that breaks as the page grows.

Quick tips

  • •Press Tab repeatedly on your page; every interactive element should show a clear outline.
  • •Press Enter on links and Space on buttons; both should work without a mouse.
Previous
Next