Skip to content
Frontsiter

Back-to-top button: move keyboard focus to the top too

Most back-to-top buttons only scroll the page, so the next Tab jumps back to the footer. A link to an id at the top fixes it without JavaScript.

By · Published · 3 min read

The problem

A keyboard user reaches the end of a long page and presses "Back to top" (also called a scroll-to-top button). The page scrolls up, but the next press of Tab jumps back down to the footer. The button moved the screen, but keyboard focus stayed at the bottom of the page.

The usual code does exactly this: a button that calls window.scrollTo(0, 0), or a link to #top when no element on the page has id="top". In our test, both sent the next Tab to the footer in Chrome, Firefox, and Safari's engine.

  • Scrolls only

    The screen shows the top, but focus stayed on the button, so the next Tab lands on the footer link.

  • Scrolls and moves focus

    Focus moved to the top with the screen, so the next Tab lands on “Home”, the first link.

Both pages right after “Back to top” was pressed with the keyboard. The dashed frame is what the screen shows.

window.scrollTo() moves only the screen; focus stays on the button. A link to #top scrolls up even when no element has that id, because browsers treat #top as a special case. But without a real element, the browser has nowhere to move focus, so Tab continues from the link at the bottom.

The fix

Add the button only to long pages: Nielsen Norman Group recommends it for pages longer than four screens and calls it overkill on short pages.

1. Link to a real element at the top. Put the id on <body>, and the link at the end of the page:

html
<body id="top">
  <!-- header, main content, footer -->
  <a href="#top">Back to top</a>
</body>

The next Tab goes to the first link on the page. Avoid putting the id on the header itself: in our test, Firefox then skipped the first link inside the header.

2. Scroll smoothly only when the reader allows motion. In globals.css:

css
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

With smooth scrolling, focus still moved to the top in all three engines. The rule also smooths every other link to an id on the page, such as a skip link.

3. With a button, move focus yourself. A link adds #top to the URL, so the browser's Back button returns to the bottom of the page. If you need a button instead, focus an element at the top. A script cannot focus <body>, so give the header tabindex="-1":

html
<header id="top" tabindex="-1">…</header>
<button type="button" id="back-to-top">Back to top</button>
js
document.getElementById("back-to-top").addEventListener("click", () => {
  const smooth = matchMedia("(prefers-reduced-motion: no-preference)").matches;
  window.scrollTo({ top: 0, behavior: smooth ? "smooth" : "auto" });
  // preventScroll: without it, Chrome stops the smooth scroll near the bottom.
  document.getElementById("top").focus({ preventScroll: true });
});
css
/* The header gets focus only so that Tab starts from it; it is not a control. */
#top:focus {
  outline: none;
}

Check that it works

  • Keyboard: scroll to the bottom, Tab to "Back to top", press Enter, then press Tab once. Focus lands on the first link of the page, not on the footer.
  • Safari on a Mac: turn on "Press Tab to highlight each item on a webpage" first (see keyboard accessibility).
  • Reduced motion: with "Reduce motion" on in the system settings, the page jumps to the top instead of scrolling.

Frontsiter's back-to-top button is a button: when it is pressed from the keyboard, it scrolls up and moves focus to the skip link at the top of the page.

Questions or ideas?

Corrections and topic ideas are always welcome. Email hello@frontsiter.com.

Contact us