Welcome to a quick tutorial on how to create a simple HTML scroll to top button. There are a ton of scroll-to-top plugins all over the Internet, but know what? Forget those overly complicated ones. We don’t even need to load an entire library just to create a “back to top” button.
To create an easy HTML scroll to top button without Javascript:
- Add an id to the body tag itself –
<body id="top">
. - At the bottom of the page, add a link back to the top –
<a href="#top">Top</a>
Yes, that’s all. But let us walk through a better example in this guide – Read on!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
BACK TO TOP
All right, let us now get into the example of how to create a “back to top” button – With pure HTML CSS Javascript.
1) THE HTML
<!-- "BACK TO TOP" BUTTON AT BOTTOM -->
<img src="up-button.png" id="backtop" onclick="totop()">

Just an image button. Yep, that’s all.
2) FIXED BUTTON ON THE CORNER
#backtop {
/* (A) BOTTOM RIGHT CORNER */
position: fixed;
bottom: 20px; right: 20px;
z-index: 999;
/* (B) HIDDEN BY DEFAULT */
visibility: none;
opacity: 0;
/* (C) COSMETICS */
cursor: pointer;
transition: opacity 0.3s;
}
/* (D) SHOW BUTTON */
#backtop.show {
visibility: visible;
opacity: 1;
}
Next, we add some CSS to the button:
position: fixed; bottom: X; right: X;
will fix the button at the lower right corner, regardless of the scrolling.visibility: none; opacity: 0;
Hide the button by default.- Well, just a bit of cosmetics.
.show { visibility: visible; opacity: 1; }
The idea is to attach this CSS class to the button when the user has scrolled down sufficiently, this will show the button; When the user is back at the top, we remove this CSS class to hide the button.
3) JAVASCRIPT
// (A) SMOOTH SCROLL TO TOP
const totop = () => window.scroll({
top: 0, left: 0, behavior: "smooth"
});
// (B) SHOW/HIDE BUTTON
const togtop = () => { if (window.scrollY >= 100) {
document.getElementById("backtop").classList.add("show");
} else {
document.getElementById("backtop").classList.remove("show");
}};
window.addEventListener("scroll", togtop);
window.addEventListener("resize", togtop);
Lastly, the Javascript should be pretty straightforward.
- Fired when we click on the “back to top” button, smooth scroll to the top of the page.
- Sadly, CSS cannot detect scrolling at the time of writing. So we will use
window.scrollY
to detect the scrolling, add/remove theshow
CSS class to show/hide the “back to top” button accordingly.
EXTRA BITS & LINKS
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
- Smooth Scroll Animation With CSS & Javascript – Code Boxx
- PNGTree – Search for your free “up buttons” here
- Back To Top Button Demo on CodePen
INFOGRAPHIC CHEAT SHEET

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!
Simple and nicely working solution. Exactly what I was looking for. Thanks a lot!
Nice, thanks! 🙂