← Back to HTML & CSS, page 2

html-css-0039 · Medium mid

How does keyboard focus work, and when should you use :focus-visible, tabindex and a skip link?

Keyboard users move through focusable elements in DOM order. tabindex="0" adds an element to the tab order, -1 makes it focusable by script only, and positive values should be avoided. :focus-visible shows a focus ring mainly for keyboard focus. A skip link lets users bypass repeated navigation, and removing outlines without a replacement makes pages unusable.

Full answer

Keyboard users move through a page with Tab and Shift+Tab. The browser visits focusable elements in the order they appear in the DOM: links with href, buttons, form controls, and anything with a tabindex. The focus ring shows where they are. Removing it with outline: none and giving nothing back makes the page unusable for them.

tabindex has three useful meanings:

  • tabindex="0": add a normally non-focusable element to the tab order, in DOM order.
  • tabindex="-1": focusable by script (element.focus()) or a link, but not in the Tab sequence. Good for the target of a skip link or for moving focus to a heading.
  • A positive number: forces a custom order. Avoid it, because it breaks the natural order and is hard to maintain.

:focus matches any focused element, including after a mouse click on a button. :focus-visible matches only when the browser decides a visible indicator is useful, which is usually for keyboard focus and text inputs. It lets you keep a clear ring for keyboard users without showing it on every mouse click.

A skip link is the first link on the page. It lets keyboard users jump past a long navigation to the main content.

Pressing Tab once focuses the skip link and shows it. Another Tab visits the nav links in order. Clicking the button with the mouse focuses it, but :focus-visible does not match, so no ring appears. Pressing Tab to reach it from the keyboard matches :focus-visible, so the blue ring shows.

When you open a modal or change the page content from script, move focus yourself with focus(), and return it to the control that opened the modal when it closes.

html
<style>
  .skip { position: absolute; left: -999px; }
  .skip:focus { left: 8px; top: 8px; background: gold; padding: 8px; }
  button, a { outline-offset: 2px; }
  button:focus-visible, a:focus-visible { outline: 3px solid #0a6cff; }
  button:focus:not(:focus-visible) { outline: none; }
</style>
<a class="skip" href="#main" id="skip">Skip to main content</a>
<nav><a href="#a" id="n1">Home</a> <a href="#b" id="n2">Courses</a></nav>
<button id="btn" type="button">Enrol</button>
<main id="main" tabindex="-1"><h1>Main content</h1></main>

Checked with real Tab key presses and a mouse click on the button. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Tab order follows DOM order for links, buttons, controls and tabindex=0 elements.
  • tabindex=-1 allows script focus, such as moving focus to a heading.
  • Avoid positive tabindex values.
  • :focus-visible keeps a clear ring for keyboard users without noise on mouse clicks.
  • Never use outline: none without a visible replacement.

Common mistakes

  • outline: none on buttons and links with no replacement style. Keyboard users cannot see where they are.
  • making a div clickable with tabindex="0" and an onclick handler only. It then needs Enter and Space key handling and a role, so use a button instead.

Follow-ups

  • why avoid positive tabindex values?
  • How would you move focus after loading new content?
  • What does a skip link do and where should it be placed?

Tips

  • Tab through every page you build. If you lose track of where you are, or cannot reach or activate something, fix it before shipping.
  • Say that you tab through every page you build. It is a real habit interviewers want to hear.
  • Mention returning focus to the opener when a dialog closes.