navigation-menus
beginner
scroll
button
animation
navigation
ux
Category · Navigation Menus Difficulty Level · Beginner Published on · August 22, 2025

Animated Back to Top Button

Smooth animated button that appears when scrolling down and smoothly scrolls back to the top of the page.

#scroll #button #animation #navigation #ux

Responsive Design

Yes

Dark Mode Support

Yes

lines

241

Browser Compatibility

Chrome · Firefox · Safari · Edge

Live Preview

Interact with the component without leaving the page.

200px

Overview

Smooth animated button that appears when scrolling down and smoothly scrolls back to the top of the page.

How to use

  1. Copy the HTML markup into your page.
  2. Paste the CSS into your stylesheet and ensure the selectors match your markup.
  3. Paste the JavaScript and load it after the markup.
  4. Adjust spacing, colors, and text to match your design system.

Customization tips

  • Rename class names to avoid collisions with your existing CSS.
  • Replace hard-coded colors with CSS variables for theming.
  • Verify the layout at 320px, 768px, and 1024px widths.
  • Test dark mode with your preferred background and text colors.

HTML

19

lines

CSS

149

lines

JavaScript

73

lines


                <div class="back-to-top-demo">
  <div class="demo-content">
    <h3>Scroll Down to See the Button</h3>
    <p>This is a demonstration of the back to top button. Scroll down to see it appear.</p>
    <div class="spacer"></div>
    <p>Keep scrolling...</p>
    <div class="spacer"></div>
    <p>Almost there...</p>
    <div class="spacer"></div>
    <p>Now scroll back up to see the button in action!</p>
  </div>
</div>

<button class="back-to-top" id="backToTopBtn" aria-label="Back to top" title="Back to top">
  <span class="sr-only">Back to top</span>
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
    <path d="M12 19V5M5 12l7-7 7 7"/>
  </svg>
</button>

              
19lines
710characters
HTMLLanguage

Browser Compatibility

Chrome

>= 50

Firefox

>= 45

Safari

>= 10

Edge

>= 15

Related Code Snippets

Explore template packs

Need larger building blocks? Browse responsive landing pages and component bundles.

Open HTML Template Library ->