Favorites

Create a Scroll Back to Top Button

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Scroll to top Button

What we're building

On long pages, a "back to top" button saves people a lot of scrolling, especially on phones. It stays tucked away at the top of the page, appears in the corner once you've scrolled down, and glides you back to the top when pressed.

Scroll down inside the box below to see it appear, then press it:

Results
Top of the demo page

Scroll down inside this box. The "Back to top" button fades in once you've scrolled a little way.

This box stands in for a long web page, such as an article, a product list or a set of search results.

On a real page, the button would be fixed to the corner of the screen. Here it's fixed to the corner of the box.

Keep scrolling. On long pages, getting back to the top by hand can take a lot of swiping, especially on a phone.

The button is a plain link to the top of the page, so it works without any JavaScript.

Its appearance is controlled by a scroll-driven animation: scroll back up and it fades away again.

Nearly there. Press the button, or tab to it and press Enter, to go back to the top.

You've reached the end of the demo content.

In this tutorial we'll build it with a plain HTML link and CSS. No JavaScript is needed, although we'll also look at a JavaScript version for older browsers.

Step 1: use a link

Give the top of your page an id (here, the page header), and add a link to it at the end of the page:

<body>
  <header id="top">
    …
  </header>

  <main>
    …lots of content…
  </main>

  <a href="#top" class="back-to-top">
    <svg aria-hidden="true" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
      <path d="M12 19V5M5 12l7-7 7 7"/>
    </svg>
    Back to top
  </a>
</body>

Why a link rather than a <button> with JavaScript?

  • It works without JavaScript. Linking to #top is ordinary HTML, so it works even if scripts fail to load.
  • It moves keyboard focus too. When a link jumps to an element on the same page, the browser also moves the keyboard's starting point there. Pressing Tab afterwards continues from the top of the page, not from the bottom where the button was.
  • It's the right element. Links take you somewhere; buttons do something. Going to the top of the page is navigation.

Step 2: position and style it

.back-to-top {
  position: fixed;                               /* stays put while the page scrolls */
  right: max(16px, env(safe-area-inset-right));  /* clear of phone screen edges */
  bottom: max(16px, env(safe-area-inset-bottom));
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;                              /* a comfortable size to tap */
  padding: 10px 16px;
  border-radius: 999px;
  background: #1d4ed8;
  color: #ffffff;
  font-weight: 600;
  text-decoration: none;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.2);
}

.back-to-top:hover {
  background: #1e40af;
}

.back-to-top:focus-visible {
  outline: 3px solid #1d4ed8;
  outline-offset: 3px;
}
  • position: fixed with right and bottom pins the button to the bottom-right corner of the screen.
  • env(safe-area-inset-…) keeps it clear of rounded corners and the home bar on modern phones. max() picks whichever is larger: 16px, or the space the phone needs.
  • min-height: 44px makes it easy to tap with a finger.
  • The colours give white text a contrast ratio of about 6.7:1. The original's #007BFF was only about 3.9:1.

Step 3: smooth scrolling with CSS

By default, following the link jumps instantly to the top. One line of CSS makes it glide instead:

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

Wrapping it in prefers-reduced-motion: no-preference means only people who haven't asked their device to reduce motion get the smooth scroll. Everyone else gets an instant jump, which is more comfortable for people who find movement on screen unpleasant. It also applies to every other same-page link on your site, such as a table of contents.

Step 4: show it only after scrolling

There's no point showing the button when you're already at the top. A scroll-driven animation can fade it in as the page scrolls, using CSS alone:

@keyframes reveal {
  from {
    opacity: 0;
    visibility: hidden;          /* hidden buttons can't be clicked or focused */
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    visibility: visible;
    transform: none;
  }
}

@supports (animation-timeline: scroll()) {
  .back-to-top {
    animation: reveal linear both;
    animation-timeline: scroll();   /* progress follows the page's scroll position */
    animation-range: 200px 400px;   /* fade in between 200px and 400px of scrolling */
  }
}
  • animation-timeline: scroll() links the animation to scrolling instead of time: scroll down and it plays forwards, scroll up and it plays backwards.
  • animation-range: 200px 400px makes the button start appearing after 200 pixels of scrolling, and finish by 400 pixels.
  • visibility: hidden in the first keyframe matters. With only opacity: 0, the invisible button could still be clicked or reached with Tab.
  • @supports only applies the animation in browsers that understand it. Older browsers simply show the button all the time, which still works perfectly well.

Good to know: scroll-driven animations work in Chrome and Edge (115 onwards), Safari (26 onwards) and Firefox (159 onwards). The demo at the top of this post uses scroll(nearest block), so it follows the scrolling box rather than the whole page.

Alternative: show it with JavaScript

If you need the button to hide and appear in older browsers too, a few lines of JavaScript can do it. Rather than checking the scroll position on every scroll movement, an IntersectionObserver simply tells you when the top of the page goes out of view:

const button = document.querySelector('.back-to-top');
const top = document.querySelector('#top');

const observer = new IntersectionObserver(([entry]) => {
  // Show the button once the header is out of view
  button.classList.toggle('is-visible', !entry.isIntersecting);
});

observer.observe(top);
.back-to-top {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
}

.back-to-top.is-visible {
  opacity: 1;
  visibility: visible;
}

Out of date: the original version of this post used window.onscroll, checking document.body.scrollTop and document.documentElement.scrollTop. That code runs dozens of times a second while scrolling, and assigning onscroll replaces any other scroll handler on the page. document.body.scrollTop is also a leftover from very old browsers; today window.scrollY gives the scroll position everywhere. IntersectionObserver avoids all of this.

Accessibility checklist

  • Use a link to #top, so keyboard focus moves to the top along with the view.
  • Give it a clear text label, such as "Back to top", and hide decorative arrows from screen readers.
  • Make it easy to tap: at least 44 by 44 pixels, with strong colour contrast.
  • Hide it properly, using visibility: hidden or the hidden attribute, not just opacity: 0.
  • Don't cover content. Check the button doesn't sit on top of other controls, such as a cookie banner or chat widget, especially on small screens.
  • Respect reduced motion for the smooth scroll.

Wrapping up

A modern "back to top" button needs no JavaScript at all: a link to #top handles the navigation and keyboard focus, scroll-behavior: smooth adds the glide, and a scroll-driven animation shows it only once people have scrolled. Add a clear label, a generous tap size and good contrast, and it'll help every visitor find their way back up.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee