Very Simple HTML Scroll To Top Button (Free Download)

Welcome to a quick tutorial on how to create a simple scroll to top button in HTML. There are a ton of scroll-to-top plugins on the Internet, but know what? Forget those gimmicks. We can literally create one within minutes without using any 3rd party frameworks – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Click here to download | Example on CodePen

The example code is released under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

BACK TO TOP BUTTON

All right, let us now get into creating a “back to top” button – Using only HTML CSS Javascript. No frameworks required.

 

TUTORIAL VIDEO

 

1) THE HTML

scroll-top.html
<!-- (A) DUMMY CONTENT -->
<div id="top">Some content.</div>
 
<!-- (B) BACK TO TOP -->
<a id="backtop" href="#top">
  <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 16.67l2.829 2.83 9.175-9.339 9.167 9.339 2.829-2.83-11.996-12.17z"/></svg>
</a>
  1. Just give an id to the very first element on your page.
  2. Insert the “back to top” button at the bottom of the page.

P.S. I have directly inserted an <svg> up arrow into the button. You can use an image if you want, or check out this list for other SVG arrows.

 

2) THE CSS – PLACE BUTTON AT BOTTOM

scroll-top.css
/* (A) SMOOTH SCROLL */
html { scroll-behavior: smooth; }

/* (B) BACK TO TOP BUTTON */
#backtop {
  position: fixed; bottom: 20px; right: 20px;
  width: 50px; height: 50px; border-radius: 50%;
  display: none;
  justify-content: center; align-items: center;
  background: #dedfff;
}
  1. This should be self-explanatory, adds smooth scroll animation to the page.
  2. This may seem to intimidating to beginners, but keep calm and study slowly.
    • position: fixed; bottom: 20px; right: 20px; Place the button at the bottom right corner.
    • width: 50px; height: 50px; border-radius: 50%; Create a round button.
    • display: none; Hide the button by default, we will show it only when the user has scrolled down sufficiently.
    • justify-content: center; align-items: center; Center the arrow in the circle.
    • background: #dedfff; Well, the background color.

 

 

3) THE JAVASCRIPT – DETECT SCROLL

scroll-top.js
// (A) SHOW/HIDE BUTTON ON SCROLL/RESIZE
const togtop = () =>
  document.getElementById("backtop").style.display = window.scrollY >= 100 ? "flex" : "none";
 
// (B) LISTEN TO SCROLL + RESIZE
window.addEventListener("scroll", togtop);
window.addEventListener("resize", togtop);

TLDR – Show the “back to top” button only when the user has scrolled down at least 100 pixels. Feel free to adjust this.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Back To Top Button (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “Very Simple HTML Scroll To Top Button (Free Download)”

Leave a Comment

Your email address will not be published. Required fields are marked *