edubun
English
1 / 20

4 / 6 · Intermediate

Using a page with the keyboard

Unplug the mouse and press Tab: every link, button and field should light up in turn, in the order you read the page.

Jump to the 10 practice questions

You will learn

  • Make every control work from the keyboard
  • Keep the Tab order and the focus ring
  • Add a skip link past repeated menus

Before this: Colour contrast

1Explain

Without a mouse

Many people never touch a mouse. Some cannot hold one steady; some use a switch, a mouth stick or speech recognition that sends key presses; screen reader users mostly move with the keyboard too. WCAG 2.2 success criterion 2.1.1 Keyboard, at level A, says everything on the page must work from a keyboard. Criterion 2.1.2 No Keyboard Trap adds that if focus can move into a part of the page, it must be able to move out again with the keyboard.

The usual keys. Tab moves forward, Shift+Tab back, Enter follows a link or presses a button, Space presses a button or ticks a checkbox, arrows move inside radio groups and menus.

2Problem

The next element

Which key moves focus to the next element?

Hint

Shift with it goes back.

Solution

Answer: Tab

Tab moves forward; Shift+Tab moves back.

3Problem

Ticking a checkbox

Which key ticks a focused checkbox?

Hint

The same key presses a button.

Solution

Answer: Space

Space toggles checkboxes and presses buttons.

4Explore

Real controls

Links with an href, button, input, select and textarea are focusable out of the box and respond to the right keys. A div with a click handler is not: it cannot be reached with Tab, Enter does nothing, and a screen reader does not announce it as a button. MDN's advice is plain. Do not use tabindex to make a div act like a button; use the button element, which comes with its role and keyboard behaviour built in.

Click into the preview and press Tab: focus skips the div and lands on the button. The list shows that only the second one is announced as a button.

Your code
<style>
  .btn { display: inline-block; padding: 8px 14px; background: #1a5fb4; color: #fff; border-radius: 6px; }
</style>
<div class="btn" onclick="this.textContent = 'Added'">Add to basket</div>
<button onclick="this.textContent = 'Added'">Add to basket</button>
Preview
What a screen reader announces (simplified)

    5Problem

    A div with onclick

    A div has an onclick handler. What is wrong?

    Hint

    Can Tab reach it?

    Solution

    Answer: It cannot be reached or pressed with the keyboard

    Use a button, which is focusable and responds to Enter and Space.

    6Explain

    Tab order

    The Tab order follows the order of elements in the HTML source. Criterion 2.4.3 Focus Order asks that this order keeps the meaning and use of the page intact, so the simplest fix for a strange order is to rearrange the HTML. MDN lists three kinds of tabindex value. Zero puts an element into the normal order. A negative value, usually -1, takes it out of the Tab order but still lets a script call focus() on it, which is useful for a dialog or an error message.

    Positive tabindex values make focus jump out of reading order.

    Your code
    <a href="#" tabindex="3">Home</a>
    <a href="#" tabindex="1">Menu</a>
    <a href="#" tabindex="2">Contact</a>
    <p>Click here, then press Tab: Menu, Contact, Home. Delete the tabindex values and try again.</p>
    Preview

    7Problem

    tabindex="0"

    What does tabindex="0" do?

    Hint

    Zero means "in the normal order".

    Solution

    Answer: Adds the element to the normal Tab order

    Zero means focusable in source order.

    8Problem

    Recommended values

    Which tabindex values does MDN recommend?

    Hint

    Positive numbers jump ahead of the source order.

    Solution

    Answer: Only 0 and -1

    Use 0 for the normal Tab order and -1 when only programmatic focus is needed. Positive values jump ahead of source order and make navigation harder to follow.

    9Explain

    Seeing the focus

    Keyboard users need to see which element has focus. Criterion 2.4.7 Focus Visible, at level AA, asks for a visible focus indicator, so a stylesheet rule like outline: none with nothing in its place is a failure. WCAG 2.2 added two related criteria. 2.4.11 Focus Not Obscured (Minimum), at level AA, says the focused item must not be completely hidden behind something the author added, such as a sticky footer or a cookie banner.

    Click into the preview and press Tab twice. The first button gives no sign that it has focus.

    Your code
    <style>
      a, button { margin: 4px; }
      .bad:focus { outline: none; }
      .good:focus-visible { outline: 3px solid #1a5fb4; outline-offset: 2px; }
    </style>
    <button class="bad">No focus ring</button>
    <button class="good">Visible focus</button>
    Preview

    10Problem

    outline: none

    A stylesheet sets outline: none on every focused element. Which criterion fails?

    Hint

    Can a keyboard user see where they are?

    Solution

    Answer: 2.4.7 Focus Visible

    With no replacement style, keyboard users cannot see where they are.

    11Problem

    A sticky banner

    A sticky cookie banner covers the focused button completely. Which WCAG 2.2 criterion is that?

    Hint

    A WCAG 2.2 criterion about focus being covered.

    Solution

    Answer: 2.4.11 Focus Not Obscured (Minimum)

    Criterion 2.4.11, added in WCAG 2.2 at level AA, prevents the focused item from being completely hidden. A focus outline cannot help if the whole button is behind a banner.

    12Problem

    Target size

    What minimum target size, in CSS pixels per side, does 2.5.8 ask for?

    px

    Hint

    Criterion 2.5.8.

    Solution

    Answer: 24 px

    At least 24 by 24 CSS pixels, unless there is enough spacing or another exception applies.

    13Explore

    The skip link

    One more pattern helps a great deal: the skip link. A site with a long menu makes keyboard users press Tab through every menu item on every page. A link placed first in the page, pointing to the main content, lets them jump past it in one key press. It can be visually hidden until it receives focus.

    The skip link is the first thing a keyboard or screen reader user meets.

    Your code
    <a href="#main">Skip to main content</a>
    <nav>
      <a href="/">Home</a> <a href="/bread">Bread</a> <a href="/cakes">Cakes</a>
    </nav>
    <main id="main">
      <h1>Corner Bakery</h1>
    </main>
    Preview
    What a screen reader announces (simplified)

      14Problem

      What a skip link does

      What does a skip link do?

      Hint

      It points to main.

      Solution

      Answer: Jumps past repeated menus to the main content

      It is the first link on the page and points to main.

      15Problem

      No way out

      Focus enters a widget but no keyboard method lets the user leave it. Which criterion fails?

      Hint

      Focus that can enter must be able to leave.

      Solution

      Answer: 2.1.2 No Keyboard Trap

      Focus that can enter must be able to leave with the keyboard.

      16Practice

      A real button

      This "button" is a div, so it cannot be reached with Tab. Replace it with a real button element with the same text and the same onclick.

      Checks
      • a real button
      • no div button left
      Your code
      <style>
        .btn { display: inline-block; padding: 8px 14px; background: #1a5fb4; color: #fff; border-radius: 6px; cursor: pointer; }
      </style>
      
      <div class="btn" onclick="document.querySelector('#out').textContent = 'Added'">Add to basket</div>
      <p id="out"></p>
      Preview
      Hint

      Change the div tags to button; keep the class, onclick and text.

      A solution
      <style>
        .btn { display: inline-block; padding: 8px 14px; background: #1a5fb4; color: #fff; border-radius: 6px; cursor: pointer; }
      </style>
      
      <button class="btn" onclick="document.querySelector('#out').textContent = 'Added'">Add to basket</button>
      <p id="out"></p>

      17Practice

      Add a skip link

      Add a skip link as the very first element of the body: <a href="#main">Skip to main content</a>, and give the main element id="main".

      Checks
      • skip link comes first
      • main has id="main"
      Your code
      <nav>
        <a href="/">Home</a> <a href="/bread">Bread</a> <a href="/cakes">Cakes</a> <a href="/contact">Contact</a>
      </nav>
      <main>
        <h1>Corner Bakery</h1>
      </main>
      Preview
      Hint

      It must be the very first element, before the nav.

      A solution
      <a href="#main">Skip to main content</a>
      <nav>
        <a href="/">Home</a> <a href="/bread">Bread</a> <a href="/cakes">Cakes</a> <a href="/contact">Contact</a>
      </nav>
      <main id="main">
        <h1>Corner Bakery</h1>
      </main>

      18Practice

      Remove positive tabindex

      The positive tabindex values make focus jump around. Remove every positive tabindex so the order follows the HTML.

      Checks
      • three links still there
      • no positive tabindex
      Your code
      <a href="/" tabindex="3">Home</a>
      <a href="/menu" tabindex="1">Menu</a>
      <a href="/contact" tabindex="2">Contact</a>
      Preview
      Hint

      All three links lose their tabindex.

      A solution
      <a href="/">Home</a>
      <a href="/menu">Menu</a>
      <a href="/contact">Contact</a>

      19Explain

      Common mistakes

      Mistake
      <style>
        :focus { outline: none; }
      </style>
      <a href="#">Menu</a> <a href="#">Contact</a>
      Fixed
      <style>
        :focus-visible { outline: 3px solid #1a5fb4; outline-offset: 2px; }
      </style>
      <a href="#">Menu</a> <a href="#">Contact</a>

      Removing the outline without a replacement hides focus from keyboard users. Style :focus-visible instead.

      20

      Summary

      • Everything must work from the keyboard, with no traps (2.1.1, 2.1.2).
      • Use button and a href instead of clickable divs.
      • Tab order follows the HTML; use only tabindex 0 and -1.
      • Keep focus visible and unhidden; targets at least 24 by 24 CSS px.

      Next unitForm labels
      Full text · The whole lesson as one text

      Who uses the keyboard

      Many people never touch a mouse. Some cannot hold one steady; some use a switch, a mouth stick or speech recognition that sends key presses; screen reader users mostly move with the keyboard too. WCAG 2.2 success criterion 2.1.1 Keyboard, at level A, says everything on the page must work from a keyboard. Criterion 2.1.2 No Keyboard Trap adds that if focus can move into a part of the page, it must be able to move out again with the keyboard.

      The usual keys. Tab moves forward, Shift+Tab back, Enter follows a link or presses a button, Space presses a button or ticks a checkbox, arrows move inside radio groups and menus.

      Use elements that already work

      Links with an href, button, input, select and textarea are focusable out of the box and respond to the right keys. A div with a click handler is not: it cannot be reached with Tab, Enter does nothing, and a screen reader does not announce it as a button. MDN's advice is plain. Do not use tabindex to make a div act like a button; use the button element, which comes with its role and keyboard behaviour built in.

      Focus order and tabindex

      The Tab order follows the order of elements in the HTML source. Criterion 2.4.3 Focus Order asks that this order keeps the meaning and use of the page intact, so the simplest fix for a strange order is to rearrange the HTML. MDN lists three kinds of tabindex value. Zero puts an element into the normal order. A negative value, usually -1, takes it out of the Tab order but still lets a script call focus() on it, which is useful for a dialog or an error message. A positive value forces the element ahead of everything else, and MDN recommends using only 0 and -1, because positive values make the order hard to follow.

      Seeing where you are

      Keyboard users need to see which element has focus. Criterion 2.4.7 Focus Visible, at level AA, asks for a visible focus indicator, so a stylesheet rule like outline: none with nothing in its place is a failure. WCAG 2.2 added two related criteria. 2.4.11 Focus Not Obscured (Minimum), at level AA, says the focused item must not be completely hidden behind something the author added, such as a sticky footer or a cookie banner. 2.5.8 Target Size (Minimum), also AA, asks that things you click or tap are at least 24 by 24 CSS pixels, or have enough space around them.

      One more pattern helps a great deal: the skip link. A site with a long menu makes keyboard users press Tab through every menu item on every page. A link placed first in the page, pointing to the main content, lets them jump past it in one key press. It can be visually hidden until it receives focus.

      Testing takes two minutes. Click in the address bar, then press Tab repeatedly through the whole page. Check that every control can be reached, that the order matches the reading order, that you can always see where focus is, and that Enter and Space do what a mouse click would. Then press Shift+Tab to come back. Any control you cannot reach, or any place where focus disappears, is a bug.

      Key points
      • Everything must work from the keyboard, with no traps (2.1.1, 2.1.2).
      • Use button and a href instead of clickable divs.
      • Tab order follows the HTML; use only tabindex 0 and -1.
      • Keep focus visible and unhidden; targets at least 24 by 24 CSS px.

      Practice questions from Using a page with the keyboard on edubun

      Embed this quiz

      Free to embed. Please keep the credit line.

      Sources for this course (8)
      • W3C, Web Content Accessibility Guidelines (WCAG) 2.2, W3C Recommendation first published 5 October 2023, current edition 12 December 2024 (w3.org/TR/WCAG22/, checked 2026-10-17)
      • W3C WAI, What's new in WCAG 2.2 (w3.org/WAI/standards-guidelines/wcag/new-in-22/, checked 2026-10-17)
      • W3C WAI, Images tutorial and alt decision tree (w3.org/WAI/tutorials/images/decision-tree/, checked 2026-10-17)
      • W3C, Using ARIA (w3.org/TR/using-aria/, Discontinued Draft 24 February 2026, rules 1 to 4, checked 2026-10-17)
      • W3C WAI-ARIA Authoring Practices Guide, Read Me First, "No ARIA is better than bad ARIA" (w3.org/WAI/ARIA/apg/practices/read-me-first/, checked 2026-10-17)
      • MDN Web Docs, tabindex (developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/tabindex, checked 2026-10-17)
      • MDN, Heading elements, accessibility notes (developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/Heading_Elements, checked 2026-10-17)
      • MDN, autocomplete attribute (developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/autocomplete, checked 2026-10-17).

      Checked: 2026-10-17