The Wayback Machine - https://web.archive.org/web/20240712150821/https://www.geeksforgeeks.org/how-to-scroll-to-an-element-inside-a-div-using-javascript/
Open In App

How to scroll to an element inside a div using javascript?

Last Updated : 25 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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. Various JavaScript methods are available for scrolling to an element.

1. Using scrollIntoView( ) method

The scrollIntoView() method is used to scroll to the specified element in the browser. 

Syntax: 

element.scrollIntoView()

Example: In this example, we will see the implementation Using scrollIntoView() to scroll to an element.  

html
<!DOCTYPE html>
<html>

<head>
    <style>
        #condiv {
            height: 500px;
            width: 500px;
            overflow: auto;
            background: #82c93a;
        }

        #ele {
            top: 70%;
            height: 200px;
            width: 200px;
            background-color: green;
            position: absolute;
        }
    </style>
</head>

<body>

    <p>Click the button to scroll to the element.</p>

    <button onclick="scrolldiv()">Scroll</button>
    <div id="condiv">
        <div id="ele">
            GeeksforGeeks
        </div>
    </div>
    <script>
        function scrolldiv() {
            var elem = document
                .getElementById("ele");
            elem
                .scrollIntoView();
        }
    </script>
</body>

</html>

Output: 

cd

Output

2. Using scroll( ) method

The scroll() method is used to scroll to the specified element in the browser. 

Syntax:

element.scroll(x-cord,y-cord)

Example: In this example, we will see the implementation Using scroll() to scroll to an element. 

html
<!DOCTYPE html>
<html>
  <head>
    <style>
      #condiv {
        height: 500px;
        width: 500px;
        overflow: auto;
        background: #82c93a;
      }

      #ele {
        top: 70%;
        height: 200px;
        width: 200px;
        background-color: green;
        position: absolute;
      }
    </style>
  </head>

  <body>
    <p>Click the button to scroll to the element.</p>

    <button onclick="scrolldiv()">Scroll</button>
    <div id="condiv">
      <div id="ele">GeeksforGeeks</div>
    </div>
    <script>
      function scrolldiv() {
        window.scroll(0, findPosition(document.getElementById("ele")));
      }
      function findPosition(obj) {
        var currenttop = 0;
        if (obj.offsetParent) {
          do {
            currenttop += obj.offsetTop;
          } while ((obj = obj.offsetParent));
          return [currenttop];
        }
      }
    </script>
  </body>
</html>

Output: 

cd

Output

3. Using scrollTo( ) method

The scrollTo() method is used to scroll to the specified element in the browser. 

Syntax:

element.scrollTo(x-cord,y-cord)

Example: In this example, we will see the implementation Using scrollTo() to scroll to an element. 

html
<!DOCTYPE html>
<html>
  <head>
    <style>
      #condiv {
        height: 500px;
        width: 500px;
        overflow: auto;
        background: #82c93a;
      }

      #ele {
        top: 70%;
        height: 200px;
        width: 200px;
        background-color: green;
        position: absolute;
      }
    </style>
  </head>

  <body>
    <p>Click the button to scroll to the element.</p>

    <button onclick="scrolldiv()">Scroll</button>
    <div id="condiv">
      <div id="ele">GeeksforGeeks</div>
    </div>
    <script>
      function scrolldiv()
      {
        window
        .scrollTo(0, findPosition(document.getElementById("ele")));
      }
      function findPosition(obj) {
        var currenttop = 0;
        if (obj.offsetParent) {
          do {
            currenttop += obj.offsetTop;
          } while ((obj = obj.offsetParent));
          return [currenttop];
        }
      }
    </script>
  </body>
</html>

Output: 

cd

Output

4. Using animate() Method (jQuery)

The animate() method in jQuery can also be used for smooth scrolling animations to an element within a container. Ensure jQuery is included in your project for this approach.

Example: To demonstrate scrolling to an element inside a div using JQuery animate() method.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        #condiv {
            height: 300px;
            width: 500px;
            overflow: auto;
            background: #f0f0f0;
        }

        #ele {
            margin-top: 600px;
            height: 200px;
            width: 200px;
            background-color: #4caf50;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
    <p>Click the button to scroll to the element.</p>
    <button onclick="scrollToElement()">Scroll</button>
    <div id="condiv">
        <div id="ele">GeeksforGeeks</div>
    </div>
    <script>
        function scrollToElement() {
            $("#condiv").animate(
                {
                    scrollTop: $("#ele").offset().top - $("#condiv").offset().top,
                },
                1000
            );
        }
    </script>
</body>

</html>

Output:

scroll

Output



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 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 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
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 move one DIV element inside another using jQuery ?
In this article, we will learn to move one HTML div element inside another using jQuery. The HTML div element is used to define a division or a section in an HTML document. Method used: parent(): This method is used to get the parent of each element in the current set of matched elements, optionally filtered by a selector. detach(): This method is
2 min read
How to Scroll to Bottom of Div in JavaScript ?
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
4 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 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 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 make an image center-aligned (vertically & horizontally) inside a bigger div using CSS ?
We will know how to center-align the image in a div tag using CSS &amp; will also understand its implementation through the example. Given an image, we need to set the image that align to the center (vertically and horizontally) inside a bigger div. But first let's create a basic structure, in which inside the body tag with id=outer there is an img
2 min read
three90RightbarBannerImg