In this blog we will see how to add back to top button in html through css, you can do so by following the given codes below.
Step 1: Go to notepad and type the given codes below.
<!DOCTYPE html>
<html>
<head>
<style>
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
background-color: green;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 10px;
}
#myBtn:hover {
background-color: #555;
}
</style>
</head>
<body>
<button onclick="topFunction()" id="myBtn" title="Go to top">Back To Top</button>
<div style="background-color:black;color:white;padding:30px" text align="center">Back To Top Button</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px" text align ="center">Scroll down to see the back to top button. </div>
<script>
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
</script>
</body>
</html>
Step 2: Save the file in .html extension and run.

its works
ReplyDelete