Back to Top Button

A tailwind back to top button: floating circular button that smooth-scrolls the page to the top.

Tailwind CSS
<button onclick="window.scrollTo({top: 0, behavior: 'smooth'})" aria-label="Back to top" class="fixed bottom-6 right-6 z-50 w-12 h-12 rounded-full bg-slate-900 text-white shadow-lg shadow-slate-900/30 hover:bg-indigo-600 hover:-translate-y-1 transition-all duration-200 flex items-center justify-center">
  <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M5 15l7-7 7 7"/></svg>
</button>

Free to use in personal and commercial projects. No attribution required.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Emerald

Fits green or eco brand palettes where the indigo hover would clash.

Tailwind CSS

Square

Sharp corners suit brutalist or enterprise dashboards better than a soft circle.

Tailwind CSS

Ghost

A quiet white button that stays out of the way on content-heavy pages.

Tailwind CSS

About this back to top button

A tailwind back to top button is the small floating control that appears in the corner once a visitor scrolls down a long page. One click smooth-scrolls back to the top, which saves readers of docs pages, long articles, and landing pages from a tedious manual scroll.

This back to top button css uses fixed positioning (bottom-6 right-6) with a high z-index so it floats above content. The click handler calls window.scrollTo with behavior smooth for the animated return, and the hover state lifts the button while shifting it to indigo. An aria-label keeps it meaningful for screen readers.

Show the button only after the user has scrolled a few hundred pixels: toggle opacity and pointer-events with a scroll listener so it never distracts at the top of the page. Keep it clear of mobile sticky bars and cookie banners, and keep the hit area at least 44px so it is easy to tap.

Long pages have a navigation problem nobody notices until they are ten screens deep in documentation. Scrolling back up by hand is tedious, and hunting for the logo to click home breaks reading flow. A back to top button restores a one-tap return path exactly when the reader needs it. There is a quieter benefit too: visitors who know they can get back easily are willing to scroll deeper in the first place.

The button is a fixed circle pinned with bottom-6 right-6 and a high z-index so it floats above everything. A scroll listener watches window.scrollY and swaps the button between opacity-0 with pointer-events-none and opacity-100 once the visitor passes a few hundred pixels. The click handler calls window.scrollTo with top 0 and behavior smooth for the animated return. The pointer-events toggle matters as much as the fade: an invisible button that still eats clicks would block footer links.

Reveal it after about 600px of scrolling; showing it immediately is visual noise on short pages. Hide it again near the footer so it never overlaps contact info or signup forms. Smooth-scroll to top over 500ms; instant jumps disorient, and the animation confirms the button did something. On infinite-scroll pages, consider whether back-to-top even makes sense; if the top is miles away, a proper navigation reset serves better.

The classic failure is a z-index tie with modals: the button keeps floating above an open dialog because both sit at z-50. Visitors see a stray circle hovering over the modal backdrop, and clicking it scrolls the page behind the dialog. Keep modal and dialog layers strictly above the button, or hide the button while any overlay is open.

Where to use it

  • ✓ Long-form blog articles
  • ✓ Documentation pages
  • ✓ Landing pages with many sections
  • ✓ E-commerce category pages

Customization tips

Move it to bottom-left if the bottom-right holds a chat widget. Shrink to w-10 h-10 for denser layouts, or swap the arrow for a progress ring that fills as you scroll. For a CSS-only smooth scroll, add scroll-behavior: smooth to the html element instead of the inline handler.

Design notes

When to use

A back to top button pays off on genuinely long pages, like documentation, blog archives, and product listings, where scrolling back manually is a chore. It should appear only after the visitor has scrolled a good distance, so it never clutters the first screen. One quiet circular button in the corner is all it needs to be.

What to avoid

Do not add it to short pages where the top is one swipe away, because a useless floating button is just clutter. Avoid placing it where a chat widget or cookie banner already lives, since stacked floating elements collide. On pages with infinite scroll, think twice: sending someone to the top of an endless feed is rarely what they wanted.

Accessibility

Use a real button with an aria-label like Back to top and a visible focus ring. Respect prefers-reduced-motion by jumping to the top instead of smooth scrolling for users who asked for less motion.

Frequently asked questions

How do I build a tailwind back to top button?

Use a fixed-position circular button (fixed bottom-6 right-6 z-50) with an onclick that calls window.scrollTo({top: 0, behavior: 'smooth'}). Add a scroll listener to fade it in only after scrolling down.

How do I show the scroll to top button only after scrolling?

Listen for the scroll event and toggle classes like opacity-0 pointer-events-none versus opacity-100 once window.scrollY passes a threshold such as 400px.

Does smooth scrolling work in all browsers?

The behavior: 'smooth' option in window.scrollTo works in all modern browsers. As a fallback you can set scroll-behavior: smooth in CSS, which older browsers simply ignore and jump instantly instead.

How do I create a CSS-only back to top button without JavaScript?

Place the button after your page content and give it position: sticky with a bottom offset like bottom: 1.5rem. Once the user scrolls past its natural position in the document, it sticks to the bottom of the viewport. It needs no scroll listener, but it cannot fade in or out, it simply appears once scrolled past.

Should my back to top button have an aria-label?

Yes. Icon-only buttons have no text for assistive technology to announce, so add aria-label="Back to top" or "Scroll to top". Screen reader users then hear a clear purpose instead of just "button". This is a one-line fix that makes the component properly accessible.

Can I control how fast smooth scrolling to the top feels?

The native window.scrollTo({ top: 0, behavior: "smooth" }) uses a browser-controlled duration you cannot set. If the default feels too slow or too fast, build a custom animation with requestAnimationFrame and an easing function over 500 to 700 milliseconds. That range feels responsive without feeling abrupt.

Customize it with our tools