To scroll an overflow <div> container automatically to the bottom in JavaScript, set container.scrollTop = container.scrollHeight (or call container.scrollTo({ top: container.scrollHeight, behavior: "smooth" }) for smooth scrolling), ensuring the container has a constrained height and overflow-y: auto (or overflow: auto / scroll) so scrollHeight exceeds clientHeight. Because scrollTop is clamped automatically to scrollHeight - clientHeight, assigning container.scrollHeight cleanly scrolls all the way to the bottom edge.
1. Instant and Smooth Scrolling to the Bottom of a <div>
Assign container.scrollTop = container.scrollHeight for immediate positioning (ideal right after appending chat messages or logs), or use container.scrollTo() with behavior: "smooth" when animated scrolling is preferred:
// Scroll an overflow div container to the bottom immediately or smoothly
function scrollToBottom(container, options = { smooth: false }) {
if (!(container instanceof HTMLElement)) {
throw new TypeError("Expected container to be an HTMLElement.");
}
const targetTop = container.scrollHeight;
if (options.smooth) {
container.scrollTo({
top: targetTop,
behavior: "smooth"
});
} else {
container.scrollTop = targetTop;
}
}
// Example usage:
const logBox = document.querySelector("#log-container");
scrollToBottom(logBox); // Instant jump to bottom
scrollToBottom(logBox, { smooth: true }); // Smooth scroll to bottom
- No manual
scrollHeight - clientHeightmath required: SettingscrollTophigher than the maximum scrollable offset automatically clamps to the bottom. - Requires scrollable overflow CSS: The container needs a fixed/max height and
overflow-y: auto(orscroll) so a scrollbar exists.
2. Auto-Scrolling Only When the User Is Near the Bottom
In live feeds or chat UIs, forcing users back to the bottom while they read older history is disruptive—only auto-scroll when the user is already near the bottom (within a threshold like 80px):
// Auto-scroll only when the user is already near the bottom edge
function appendAndMaybeScroll(container, newElement, thresholdPx = 80) {
if (!(container instanceof HTMLElement) || !(newElement instanceof Node)) {
throw new TypeError("Expected a container HTMLElement and a child Node.");
}
const distanceFromBottom =
container.scrollHeight - container.scrollTop - container.clientHeight;
const shouldStickToBottom = distanceFromBottom <= thresholdPx;
container.appendChild(newElement);
if (shouldStickToBottom) {
container.scrollTop = container.scrollHeight;
}
}
// Example usage:
const messageFeed = document.querySelector("#chat-feed");
const item = document.createElement("div");
item.textContent = "New incoming message";
appendAndMaybeScroll(messageFeed, item);
- Respects manual reading position: If
distanceFromBottomexceeds the threshold, the user stays where they scrolled. - Measure before appending: Computing
scrollHeight - scrollTop - clientHeightbefore adding the new node reflects the user’s current position accurately.
3. Waiting for DOM / Image Layout Before Scrolling
If appending content triggers async images, fonts, or DOM layout updates, schedule the scroll after rendering with requestAnimationFrame() or observe size changes with ResizeObserver so scrollHeight includes the final rendered height:
// Scroll after the browser completes the next paint frame
function scrollToBottomAfterPaint(container) {
if (!(container instanceof HTMLElement)) {
throw new TypeError("Expected container to be an HTMLElement.");
}
window.requestAnimationFrame(() => {
container.scrollTop = container.scrollHeight;
});
}
// Example usage:
const panel = document.querySelector("#output-panel");
scrollToBottomAfterPaint(panel);
- Avoid stale
scrollHeightreads: ReadingscrollHeightbefore layout or image decoding can leave a small gap at the bottom. - Pair with
overflow-anchor: autowhen helpful: Modern browsers support CSSoverflow-anchorto reduce scroll jumps during dynamic content insertion.
