HOWTO · JavaScript

How to Scroll to Bottom of a Div in JavaScript

This article will teach you how to scroll to bottom of a div using a combination of scrollTop and scrollHeight, jQuery, jQuery .animate(), Element.scroll(), and Element.scrollIntoView().

On this page

To scroll a <div> to its latest content, set its scrollTop to scrollHeight - clientHeight. The container must have a constrained height and overflowing content. For example, after adding a chat message, run this calculation on the chat container. This article also covers jQuery, animated scrolling, element.scroll(), and when scrollIntoView() is appropriate.

Use scrollTop and scrollHeight to Scroll to Bottom of a <div> in JavaScript

scrollTop is the number of pixels a container has been scrolled vertically. scrollHeight measures the full content height, including the portion outside the visible area, and clientHeight measures the visible inner height. The maximum useful vertical position is therefore scrollHeight - clientHeight; assigning the full scrollHeight is not necessary.

A container needs a limited height and content taller than that height to scroll. CSS such as overflow-y: auto lets the browser show a scrollbar when needed; overflow-y: scroll also works and keeps the scrollbar area available. The following runnable JavaScript illustrates the calculation with the same values the DOM properties provide:

const container = { scrollTop: 0, scrollHeight: 640, clientHeight: 240 };
container.scrollTop = container.scrollHeight - container.clientHeight;
console.log(container.scrollTop);
400

In a browser, apply that assignment to the actual container after its content has been added, for example: const box = document.querySelector('#chat'); box.scrollTop = box.scrollHeight - box.clientHeight;. If content is rendered asynchronously, perform the assignment after the render so the measured height includes the new content.

Scroll to bottom with scrollTop and scrollHeight

Use jQuery to Scroll to Bottom of a <div> in JavaScript

If the page already uses jQuery, its scrollTop() method can set the vertical position. To read scrollHeight, access the matched element’s underlying DOM node, such as $('#chat')[0], then subtract that node’s clientHeight. For example, the operation is const box = $('#chat')[0]; $('#chat').scrollTop(box.scrollHeight - box.clientHeight);.

This approach still needs a scrollable container with content that overflows. jQuery is an external library dependency, so for a page that does not already use it, the native scrollTop approach avoids adding that dependency. This browser snippet is provided as an unexecuted illustration.

Scroll to bottom with jQuery

Use jQuery .animate() to Scroll to Bottom of a <div> in JavaScript

Use jQuery .animate() when you want the container to move over a duration instead of jumping immediately. Pass the target scrollTop and a duration in milliseconds. For instance, the operation is const box = $('#chat')[0]; $('#chat').stop(true).animate({ scrollTop: box.scrollHeight - box.clientHeight }, 500);.

The 500 value requests a half-second animation. Calling .stop(true) first stops the current animation and clears queued animations before starting the new one, so repeated updates do not accumulate a backlog. As with the immediate jQuery option, this needs jQuery and a container that can scroll. This browser snippet is unexecuted.

Scroll to bottom with jQuery smooth animation

Use element.scroll() to Scroll to Bottom of a <div> in JavaScript

The native element.scroll() method accepts a target position and can request smooth behavior. Use the same maximum scroll position as for scrollTop:

const box = document.querySelector('#chat'); box.scroll({ top: box.scrollHeight - box.clientHeight, behavior: 'smooth' });

Omit behavior: 'smooth' or use behavior: 'instant' for an immediate move. This browser API illustration is unexecuted; it assumes #chat identifies the intended scrollable container and that its content has already been rendered.

Scroll to bottom with Element scroll

Use element.scrollIntoView() to Scroll to Bottom of a <div> in JavaScript

scrollIntoView() brings the element it is called on into view. It may scroll a containing element, other ancestors, or the page to do so. It does not directly set a container’s internal scroll position to its bottom.

Use it when the goal is to reveal a particular last item or marker. Add a marker after the content and call document.querySelector('#latest-message').scrollIntoView({ block: 'end', behavior: 'smooth' });. This targets that element; depending on the layout, the page or ancestor containers may also move. For reliably setting only a specific container’s bottom position, use scrollTop or element.scroll() instead. This browser snippet is unexecuted.

Scroll to bottom with Element scrollIntoView

Conclusion

For a scrollable <div>, the direct native solution is scrollTop = scrollHeight - clientHeight, run after the latest content is present. Use jQuery’s scrollTop() if the page already depends on jQuery, or .animate() for a timed transition. element.scroll() provides a native option with smooth behavior. Choose scrollIntoView() when you want a specific child brought into view, rather than setting the container’s own scroll position.