Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to scroll down to bottom of page in selenium using JavaScriptExecutor
Next article icon

How to Scroll to Bottom of Div in JavaScript ?

Last Updated : 04 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Table of Content

  • Using scrollTop Property
  • Using scrollIntoView Method

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= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">     <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= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">     <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

Next Article
How to scroll down to bottom of page in selenium using JavaScriptExecutor

H

hanishj
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • 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#, e
    3 min read
  • How to get the height of scroll bar using JavaScript ?
    To get the height of scroll bar we could use different approaches. In this article, we are given an HTML document and the task is to get the height of the scrollbar using JavaScript. Following are the different approaches to solving this problem which are discussed below: Table of Content Using Cont
    3 min read
  • How to use Scrollspy in Bootstrap 5 via JavaScript ?
    In this article, we will see how to use Scrollspy in Bootstrap 5 via Javascript. Using Scrollspy in Bootstrap 5, the navigation or list group components of Bootstrap update automatically based on where the user is scrolling to indicate which link is currently active in the viewport. There are 2 ways
    6 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 scrollIntoV
    3 min read
  • How to Zoom an Image on Scroll using JavaScript?
    Zoom in and out of images is a common feature in web development. One way to achieve this effect is by using JavaScript to zoom in and out of an image when the user scrolls. In this article, we will go through the steps needed to create a zoomable image using JavaScript. Step 1: HTML Markup: The fir
    3 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 Rea
    2 min read
  • How to get the Width of Scroll bar using JavaScript?
    Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth
    3 min read
  • How to Create a Scroll To Top Button in React JS ?
    You will see there are lots of websites, that are using a useful feature like if you're scrolling the webpage, and now you are at the bottom of that page then you can use this button to scroll up automatically like skip to the content. The following example covers create a Scroll To Top button in Re
    3 min read
  • How to find the position of HTML elements in JavaScript ?
    In this article, we are given an HTML document and the task is to get the position of any element by using JavaScript. Use the following steps to get the position: Step 1: Selecting an element: Before finding the position, it is necessary to select the required HTML element. Every element in HTML is
    3 min read
  • How to get the position of scrollbar using JavaScript ?
    JavaScript is an amazing language and there are many functions available through which we can access any element of the HTML page through javascript. There are some simple techniques to get the scrollbar position that are discussed below: Approach 1: Whenever the function getScroll() is encountered,
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences