How To Create a Scroll Back To Top Button with Live Example

When your page have lots of content and you will scroll the page down to see all these content and if you want to go to top of the page so at that time this Scroll back to top button will be useful.

Scroll Back To Top Button

Here we will create one live demo example to create scroll back to the top of page and also will explain step by step how to configure and which CSS and Java Script required.

Step 1: Create one simple HTML file and include below library.

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Required library -->
    <script src=""></script>
    <!-- Bootstrap theme -->
    <link rel="stylesheet" href="">
    <script src=""></script>
    <title>How To Create a Scroll Back To Top Button with Live Example</title>

Write above code inside <head> tag and above libraries are used to design your page with bootstrap framework.

Step 2: Add some custom CSS.

<style type="text/css"> {
        opacity: 1;

    #back-to-top {
        font-size: 30px;
        position: fixed;
        bottom: 40px;
        right: 40px;
        z-index: 9999;
        width: 64px;
        height: 64px;
        text-align: center;
        line-height: 60px;
        background: rgb(19 174 223);
        color: #fff;
        cursor: pointer;
        border: 0;
        border-radius: 2px;
        text-decoration: none;
        transition: opacity 0.2s ease-out;
        opacity: 0;

Step 3: Set up your content and put a button at the end of the body tag.

    <!-- your content start -->
    <!-- your content end -->
    <a href="#" id="back-to-top" title="Back to top">&uarr;</a>

Step 4: Finally write some JavaScript code.

    if ($('#back-to-top').length) {
        var scrollTrigger = 100, // px
            backToTop = function() {
                var scrollTop = $(window).scrollTop();
                if (scrollTop > scrollTrigger) {
                } else {
        $(window).on('scroll', function() {
        $('#back-to-top').on('click', function(e) {
                scrollTop: 0
            }, 700);

You can write above code after the body tag and this code will be run while you click on Scroll to Top Button. By using this javascript code we will show one button while you scroll down and when you click on that button it will go to the top of the page.

Here we are sharing live demo link and also you can download whole code file with below download button.

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply