The Wayback Machine - https://web.archive.org/web/20240919024146/https://www.geeksforgeeks.org/how-to-scroll-to-bottom-of-div-in-javascript/
Open In App

How to Scroll to Bottom of Div in JavaScript ?

Last Updated : 04 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Scrolling to the bottom of a div is a common task in web development, especially when dealing with chat applications, message boards, or any content where new information is frequently added at the bottom.

There are several approaches to scrolling to the bottom of the div in JavaScript which are as follows:

Using scrollTop Property

The scrollTop property of a div element represents the number of pixels the content of the div is scrolled from its top. By setting this property to the div’s scrollHeight, we can scroll to the bottom.

Syntax:

// Get the div element
let divElement = document.getElementById('divId');
// Scroll to the bottom of the div
divElement.scrollTop = divElement.scrollHeight;

Example: To demonstrate the use of the scrollTop Property to scroll to the bottom of the div.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Scroll to Bottom of Div</title>
    <link rel="stylesheet" href=
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
 
        #scrollableDiv {
            width: 100%;
            height: 200px;
            overflow-y: scroll;
            border: 1px solid #dee2e6;
            background-color: #fff;
            padding: 10px;
            border-radius: 0.25rem;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
        }
 
        p {
            margin: 0 0 10px 0;
        }
 
        .scrollBtn {
            border-radius: 0.25rem;
        }
 
        h2 {
            text-align: center;
            color: #2e8c46;
        }
    </style>
</head>
 
<body>
 
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div id="scrollableDiv">
                    <h2>Geeks for Geeks</h2>
                    <p>
                          GeeksforGeeks is a computer science
                        portal for geeks. It contains well-written,
                          well-thought, and well-explained computer
                          science and programming articles,
                        quizzes, and practice/competitive
                        programming/company interview Questions.
                      </p>
                    <p>
                          GeeksforGeeks has grown tremendously since its
                        inception in 2009 and now has over 2 million
                        registered users and over 10 million
                        page views per month.
                      </p>
                    <p>
                          The website covers topics ranging from algorithms,
                        data structures, programming languages,
                        software development, to interview preparation
                        for various tech companies.
                      </p>
                    <p>
                          GeeksforGeeks also provides a platform for users
                        to practice coding through its coding practice
                        section and participate in coding contests.
                      </p>
                    <p>
                          Overall, GeeksforGeeks is a valuable
                        resource for computer science enthusiasts,
                        students, and professionals seeking to
                        enhance their knowledge and skills in the
                        field of computer science and
                        programming.
                      </p>
                </div>
                <button class="btn btn-primary btn-block mt-3
                               scrollBtn"
                        onclick="scrollToBottom()">
                      Scroll to Bottom
                  </button>
            </div>
        </div>
    </div>
 
    <script>
       
        // Get the scrollable div element
        let scrollableDiv =
            document.getElementById('scrollableDiv');
 
        // Function to scroll to the bottom of the div
        function scrollToBottom() {
            scrollableDiv.scrollTop = scrollableDiv
                .scrollHeight;
        }
    </script>
 
</body>
 
</html>


Output:

mainScroll

Scroll to the bottom of the div.

Using scrollIntoView Method

The scrollIntoView method is available on DOM elements. By default, this method scrolls the element into view, but we can specify options to control the scrolling behavior. In our case, we want to scroll to the bottom, so we pass block: ‘end’.

Syntax:

let divElement = document.getElementById('divId');
divElement.scrollIntoView({ behavior: 'smooth', block: 'end' });

Example: To demonstrate the use of the scrollIntoView Property to scroll to the bottom of the div.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scroll to Bottom of Div</title>
    <link rel="stylesheet" href=
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
 
        #scrollableDiv {
            width: 100%;
            height: 200px;
            overflow-y: scroll;
            border: 1px solid #dee2e6;
            background-color: #fff;
            padding: 10px;
            border-radius: 0.25rem;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
        }
 
        p {
            margin: 0 0 10px 0;
        }
 
        .scrollBtn {
            border-radius: 0.25rem;
        }
 
        h2 {
            text-align: center;
            color: #2e8c46;
        }
    </style>
</head>
 
<body>
 
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div id="scrollableDiv">
                    <h2>Geeks for Geeks</h2>
                    <p>
                          GeeksforGeeks is a computer science
                          portal for geeks.It contains
                          well-written, well-thought,and
                          well-explainedcomputer science and
                          programming articles, quizzes, and
                        practice/competitive programming/company
                        interview Questions.
                      </p>
                    <p>
                          GeeksforGeeks has grown tremendously
                        since its inception in 2009
                        and now has over 2 million registered users and
                        over 10 million page views per month.
                      </p>
                    <p>
                          The website covers topics ranging from algorithms,
                        data structures, programming languages,
                        software development, to interview preparation
                        for various tech companies.
                      </p>
                    <p>
                          GeeksforGeeks also provides a platform for
                        users to practice coding through its coding
                        practice section and participate in coding
                        contests.
                      </p>
                    <p>Overall, GeeksforGeeks is a valuable resource for
                        computer science enthusiasts, students, and
                        professionals seeking to enhance their knowledge
                        and skills in the field of computer science
                        and programming.
                      </p>
                </div>
                <button class="btn btn-primary btn-block mt-3 scrollBtn"
                onclick="scrollToBottom()">Scroll to
                    Bottom</button>
            </div>
        </div>
    </div>
 
    <script>
        // Get the scrollable div element
        var scrollableDiv = document.
            getElementById('scrollableDiv');
 
        // Function to scroll to the bottom
        //of the div using scrollIntoView method
        function scrollToBottom() {
            var bottomElement = scrollableDiv.
                lastElementChild;
            bottomElement
                .scrollIntoView({ behavior: 'smooth', block: 'end' });
        }
    </script>
 
</body>
 
</html>


Output:

mainScroll

Scroll to the Bottom of the div



Similar Reads

How to make div fixed after scroll to that div ?
In this article, we will see how to make a div fixed after you scroll to it by using the position property with a sticky value. This effect is generally used on websites when an important message needs to be shown. Set the property position to sticky to create elements that behave as fixed positions whenever the user scrolls to that position. Fixin
4 min read
How to set textarea scroll bar to bottom as a default using JavaScript/jQuery ?
Given an HTML document containing &lt;textarea&gt; element and the task is to set the position of scrollbar to bottom of &lt;textarea&gt; with the help of jQuery. Approach 1: Get the scrollHeight property of the textarea. Use scrollTop property to set the position of vertical scrollbar using jQuery. Example: This example implements the above approa
3 min read
How to Scroll to an Element Inside a Div using JavaScript?
To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript: Table of Content Using scrollIntoView( ) method Using scroll( ) method Using scrollT
3 min read
Hide scroll bar, but while still being able to scroll using CSS
Hiding the scrollbar with CSS enhances webpage aesthetics by removing visible scrollbars while maintaining scroll functionality. This can be achieved using browser-specific CSS properties for Chrome, Safari, Firefox, and Internet Explorer, ensuring a cleaner, more streamlined user interface without sacrificing usability. There are many other option
3 min read
How to set Bootstrap 4 scroll to the bottom of long sticky sidebar?
In Bootstrap 3, to set bootstrap scroll to the bottom of long sticky sidebar is possible using Affix which is handled by "Affix jQuery plugin". Unfortunately, in Bootstrap 4 migration "Affix jQuery plugin" was dropped. To achieve this affix property as like as Bootstrap 3, Bootstrap 4 recommends us CSS property i.e., position: sticky; directly in s
3 min read
CSS | Scroll Padding bottom
The Scroll Padding bottom is an inbuilt property in Scroll Snap module. This property set the scroll padding longhand. This property acts as a magnet on the top of the sliding element that stick to the top of the view-port and stop the scrolling(forcefully) in that place. The Scroll Padding property is optional, this property is used when the Scrol
2 min read
CSS scroll-margin-bottom Property
The scroll-margin-bottom property is used to set all the scroll margins to the bottom of an element at once. The value specified for the scroll-margin-bottom determines how much of the page that is primarily outside the support should remain visible. Hence, the scroll-margin-bottom values represent outsets that define the scroll snap area that is u
2 min read
How to create a Scroll To Bottom button in React JS ?
Learn to implement a "Scroll to Bottom" button in React using the useState() hook, a feature commonly seen in chat applications like WhatsApp and Telegram for effortless navigation to the end of a chat window. Prerequisites:NodeJS or NPMReact JSstyled-componentReact useState()Steps to Create the React Application And Installing Module:Step 1: Creat
2 min read
How to show Toast at Bottom-left after Scroll Down in Bootstrap 5 ?
Toasts are quick, gentle pop-up messages that keep users informed without interrupting. To achieve this functionality bootstrap does not provide any built-in class. But we can achieve it by combining utility classes like position-fixed, bottom-0, and start-0, and you can position the toast at the bottom-left corner of the viewport. Syntaxif (scroll
3 min read
How to scroll automatically to the Bottom of the Page using jQuery?
In this article, we will use the scrollTop() method of jQuery to scroll the page in different scenarios as discussed below: Table of Content Using the scrollTop with height() methodUsing scrollTop with animate() methodUsing the scrollTop with height() methodIn this approach, we will use the scrollTop() method with the height() method to scroll the
3 min read
How to scroll down to bottom of page in selenium using JavaScriptExecutor
Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS and its scripts are written in various languages i.e Java, Python, JavaScript, C#, etc. In this article, we will be working with Java
3 min read
Animate a div on mouse scroll using AOS
AOS is a small library to animate elements on your page as you scroll. AOS allows you to animate elements as you scroll down, and up. If you scroll back to top, elements will animate to it's previous state and are ready to animate again if you scroll down. Installation: Installing AOS is very simple: Add the below tag in the head tag of the webpage
1 min read
How to align content of a div to the bottom using CSS ?
Basic property of CSS: position: The position property specifies the type of positioning method used for an elements. For example static, relative, absolute and fixed.bottom: The bottom property affects the vertical position of a positioned element. This property has no effect on non-positioned elements.left: The left property affects the horizonta
2 min read
How to set div with left image and button at bottom using bootstrap?
We can set div with left image and button at the bottom by two methods as follows: Method 1: Using bootstrap Float-left These utility classes float-left a component to the left or disable floating, based on the current viewport size utilizing the CSS float property. !important is included to dodge (avoid) specificity issues. These utilize the same
4 min read
How to Maintain Aspect Ratio of Div Grid Element to Set Correct Margins on Top/Bottom or Sides in CSS ?
Maintaining the aspect ratio of grid elements is an important aspect of web design as it ensures that the layout of the page remains consistent across different screen sizes and resolutions. When the aspect ratio of a grid element is maintained, it will always have correct margins on top/bottom or sides, making it easier to design responsive layout
4 min read
How to detect when user scrolls to the bottom of a div ?
In this article, we will learn how we can detect when the user scrolls to the bottom of a div. There are a few ways available in jQuery that we can use to accomplish this task as listed below: Table of Content Using the jQuery on() methodUsing the scroll() methodUsing the jQuery on() methodThe on() method is used to attach one or more events to the
2 min read
How to stick a div at the bottom left of the screen using Bootstrap5 ?
In this article, we will see how to stick a div element in the bottom left of the screen using Bootstrap 5 utility classes. Bootstrap is a famous front-end framework used for developing websites. Bootstrap provides a collection of built-in design components, and styles for making the design easier. Syntax&lt;div class="fixed-bottom p-3 start-0 "
2 min read
How to position a div at the bottom of its container using CSS?
Positioning a div at the bottom of its container means aligning the div so that it stays at the lowest point within the container's boundaries. This can be done using CSS techniques like absolute positioning, Flexbox, or Grid, each method providing different layout control options. Table of Content Using Absolute Positioning Using Flexbox Using Gri
3 min read
How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
How to center a div within another div?
The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed and that division/section works like a container whose CSS styling can be done as a single unit or javascript can be used to perform various tasks on that container. Syntax: &lt;div&gt; &lt;h3&gt;'This is a div of the web page.&gt;/h
4 min read
How to center a div on the edge of another div in SASS ?
In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. Centering a div on the edge of another div is not a difficult task. We can achieve it b
4 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to clear all div's content inside a parent div ?
In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the
2 min read
How to Overlay/Hover Div in Flexbox Container Div?
In CSS, overlay/hover effects can be achieved by using Flexbox containers to center content and applying styles to reveal overlays on hover. By positioning elements absolutely within a Flexbox container, overlays can cover the entire content area. These are the following approaches: Table of Content Using ::before Pseudo-elementUsing an Overlay Div
3 min read
How to get the width of scroll bar using JavaScript ?
Given an HTML document and the task is to get the width of the scrollbar using JavaScript. There are two different approach to solve this problem which are discussed below: Approach 1: Create an element (div) containing scrollbar. OffsetWidth defines the width of an element + scrollbar width. ClientWidth defines the width of an element. So scrollba
3 min read
How to disable scroll to change number in &lt;input type="number"&gt; field using JavaScript/jQuery?
Given an HTML document containing &lt;input type = "number"&gt; attribute and the task is to disable the mouse scroll wheel to change the number value using JavaScript/jQuery. Approach: Select the element.Add an event to that element which will call a function when scrolling starts.Make the element disabled on scrolling.Example 1: This example impl
2 min read
Slide Down a Navigation Bar on Scroll using HTML, CSS and JavaScript
To create a slide down navigation bar you need to use HTML, CSS, and JavaScript. HTML will make the structure of the body, CSS will make it looks good. This kind of sliding navbar looks attractive on a site. By using JavaScript you can easily make the navigation bar slideable when the user scrolls down. Creating Structure: In this section, we will
4 min read