Skip to content

Back to Top Button that Fades In

Floating Tailwind CSS back-to-top button that fades in after scrolling, smooth-scrolls up and returns keyboard focus to the page title.

Also known as: scroll to top button, floating back to top, go to top button, scroll up button

Back to Top Button that Fades In live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A floating "Back to top" button built with Tailwind CSS that stays hidden at the top of the page, fades and slides in once the visitor has scrolled down, and smoothly scrolls back to the start when clicked. The demo places it on a long-form article, the typical home for this pattern alongside blogs, documentation and long landing pages.

How it works

The demo wrapper .btt-page holds an article whose title, #btt-top, is the scroll target. The button itself is a single link, a.btt-button, positioned with fixed bottom-5 right-5 (sm:bottom-8 sm:right-8 on larger screens) and styled with a violet to fuchsia gradient, an arrow icon and a visible "Back to top" label.

style.css keeps the button invisible by default with opacity: 0, visibility: hidden and a small downward transform. When .btt-visible is added, it transitions to full opacity and its resting position. The visibility transition is delayed while hiding, so the fade finishes before the button leaves the tab order.

script.js finds every .btt-button on DOMContentLoaded and listens to the window scroll event (passive and throttled with requestAnimationFrame). When window.scrollY passes the value in data-btt-offset, it toggles .btt-visible. On click it prevents the default jump, calls window.scrollTo with smooth scrolling (instant under reduced motion) and moves focus to the element named in the link href.

Customization

  • data-btt-offset="400" on .btt-button: the scroll distance in pixels before the button appears; use 150 for short pages or 1000 for very long ones.
  • href="#btt-top": point to any element id, such as your site header or a skip-link target, to control where focus lands after scrolling.
  • bottom-5 right-5 and sm:bottom-8 sm:right-8: move the button, e.g. left-5 for the opposite corner.
  • from-violet-600 to-fuchsia-500 on the button: change the gradient, or replace both with a single bg-stone-900.
  • Remove the text "Back to top" and add aria-label="Back to top" to the link for an icon-only round button (also change pl-4 pr-5 to p-3).
  • transform: translateY(1rem) scale(0.95) in .btt-button: change the entrance, for example translateX(1rem) for a slide from the side.
  • 0.3s in the .btt-button transitions: speed up or slow down the fade.

Use cases

  • Blogs: readers jump back to the article title after scrolling a long post
  • Documentation sites: a floating button fades in after scrolling so long reference pages are easier to navigate
  • Long landing pages: visitors return to the top with a smooth scroll instead of dragging the scrollbar
  • Accessible sites: keyboard focus moves to the page title after the click so tabbing continues from the top

Accessibility

The control is a native link with a visible text label and a decorative aria-hidden icon, so screen readers announce "Back to top, link". While hidden, visibility: hidden keeps it out of keyboard focus and the accessibility tree. After a click, focus moves to the #btt-top heading (which has tabindex="-1"), so the next Tab continues from the top of the content. A focus-visible ring marks keyboard focus. With prefers-reduced-motion: reduce the scroll jumps instantly, the fade and hover lift are disabled and the header's bouncing "Scroll" arrow stops.

Usage notes

Copy only a.btt-button, style.css and script.js into an existing page and point the href at your own top element. The article is demo content. Several buttons work independently if needed, although one per page is typical.

Back to Top Button that Fades In at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile
Category
Back to Top
License
Free for personal and commercial use

Questions about this template

Can I use the Back to Top Button that Fades In code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Back to Top Button that Fades In – free Tailwind CSS back to top button template
Back to Top Button that Fades In · free Tailwind CSS back to top button template

More Back to Top

Back to Top Button with Scroll Progress – free Pure CSS back to top button template

Back to Top Button with Scroll Progress

Round back-to-top button with an SVG ring that fills as you scroll, a percentage label and smooth scrolling, for the page or any scroll container.