HomeBloggingBest Scroll Back to Top button

Best Scroll Back to Top button

Best Scroll Back to Top button in html for website in simple way. This button is used to go to the top when there is too much material we need to go to the top.

Those buttons usually float to the bottom corner of the sites and then click you back to the top of the page. It can be done by javascript.

HTML code for scroll back to top
<!DOCTYPE html>
        <title>Made by deekway</title>
    <!--scroll button-->
     <div class="floating-btn"><img src"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQcJ7ufdPXZHvZA9bmfikO9LPygafBoNXQyIw&usqp=CAU"></div>
    <section class="section0">Scroll To Top Button scroll down</section>
    <section class="section1">section1</section>
    <section class="section2">Made by deekway</section>
    <section class="section3">section3</section>
    <section class="section4">section4</section>
CSS for scroll back to top
 /* Created by deekway */

  padding: 0;
  margin: 0;
  scroll-behavior: smooth;
  section {
  height: 75vh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: Sans-Serif;
  font-size: 32px;
  text-transform: uppercase;
  letter-spacing: 8px;

    background: #eb4d4b;
  background: #f0932b;
    background: black;
  background: #7ed6df;
  background: #22a6b3;

/*scroll button*/

.floating-btn {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 50px;
    height: 50px;
    border-radius: 50px;
    background-color: salmon;
    color: #fff;
    transform: scale(0);
    transition: all .1s linear;
    display: flex;
    justify-content: center;
    align-items: center;

.floating-btn img{

Javascript for scroll back to top button
function init() {

    // DOM node
    const btn = document.querySelector(".floating-btn");
    // handle window scroll
    function handlScroll() {
        if (window.pageYOffset > 300) {
            btn.style.transform = "scale(1)";
        } else {
            btn.style.transform = "scale(0)";
    // attach scroll to window
    window.addEventListener("scroll", handlScroll);
    // handle reset scroll
    function scrollUp() {
        window.scrollTo(0, 0);
    // reset window scroll
    btn.addEventListener("click", scrollUp);

// invoke init when DOM loaded
window.addEventListener("DOMContentLoaded", init);


Please enter your comment!
Please enter your name here

Must Read