Question
I am building a chat interface that loads messages using AJAX, and I want the messages container to stay scrolled to the bottom.
The messages are wrapped in this element:
#scroll {
height: 400px;
overflow: scroll;
}
How can I scroll this div to the bottom by default using JavaScript?
Also, how can I keep it scrolled to the bottom after an AJAX request adds new messages?
Short Answer
By the end of this page, you will understand how to programmatically scroll an element to its bottom in JavaScript, why this is common in chat applications, and how to re-apply scrolling after new content is added through AJAX or DOM updates.
Concept
In many interfaces, especially chat windows, logs, notifications, and activity feeds, new content is added to the bottom of a scrollable container. When that happens, the browser does not automatically move the scroll position for you.
To scroll a div to the bottom, JavaScript usually sets:
element.scrollTop = element.scrollHeight;
Here is what these properties mean:
scrollTop: how far the element is currently scrolled from the topscrollHeight: the full height of the scrollable content inside the element
When you set scrollTop equal to scrollHeight, the browser moves the visible area to the lowest possible position.
This matters because:
- chat users expect to see the newest message immediately
- logs often need to reveal the latest entry
- live dashboards may append updates continuously
In real programming, the main challenge is timing. You must scroll after the new content has been inserted into the DOM. If you scroll too early, the new height has not been calculated yet, so the container may not move far enough.
Mental Model
Think of the div like a tall paper receipt inside a small viewing window.
- The receipt length is
scrollHeight - The window's current vertical position is
scrollTop - Scrolling to the bottom means sliding the window down until it shows the very end of the receipt
When new messages are added, the receipt gets longer. If you want the latest message visible, you must move the window down again.
Syntax and Examples
The core pattern is:
const box = document.getElementById('scroll');
box.scrollTop = box.scrollHeight;
Example with HTML:
<div id="scroll">
<p>Hello</p>
<p>How are you?</p>
<p>Newest message</p>
</div>
#scroll {
height: 400px;
overflow: auto;
}
const box = document.getElementById('scroll');
box.scrollTop = box.scrollHeight;
Why it works
Step by Step Execution
Consider this example:
<div id="scroll">
<p>Message 1</p>
<p>Message 2</p>
</div>
const box = document.getElementById('scroll');
const p = document.createElement('p');
p.textContent = 'Message 3';
box.appendChild(p);
box.scrollTop = box.scrollHeight;
Step by step:
-
document.getElementById('scroll')- JavaScript finds the scrollable container.
-
document.createElement('p')- A new paragraph element is created for the next message.
-
p.textContent = 'Message 3'
Real World Use Cases
This pattern appears in many real applications:
- Chat apps: show the latest incoming or sent message
- Server logs: keep the newest log line visible
- Live notifications: auto-scroll as alerts are appended
- Comment feeds: jump to the latest comment in a discussion
- Terminal-like interfaces: show the latest command output
- Monitoring dashboards: follow live event streams
For AJAX-based apps, the common flow is:
- request new data
- update the DOM with the new content
- scroll the container to the bottom
Real Codebase Usage
In real projects, developers usually wrap scrolling logic in a reusable function:
function scrollToBottom(element) {
element.scrollTop = element.scrollHeight;
}
Then they call it after operations such as:
- appending a new message
- rendering AJAX response data
- loading older or newer conversation chunks
Common patterns
1. After AJAX success
function renderMessages(messages) {
const box = document.getElementById('scroll');
messages.forEach(message => {
const p = document.createElement('p');
p.textContent = message;
box.appendChild(p);
});
box.scrollTop = box.scrollHeight;
}
2. Guard clause for missing elements
function scrollToBottomById() {
element = .(id);
(!element) ;
element. = element.;
}
Common Mistakes
1. Scrolling before the content is added
Broken example:
box.scrollTop = box.scrollHeight;
box.appendChild(newMessage);
Why it fails:
- the height is measured before the new content exists
Correct version:
box.appendChild(newMessage);
box.scrollTop = box.scrollHeight;
2. Selecting the wrong element
Broken example:
document.body.scrollTop = document.body.scrollHeight;
Why it fails:
- this scrolls the whole page, not the chat container
Make sure you target the actual scrollable div.
3. Forgetting that the container must be scrollable
Broken CSS:
#scroll {
height: auto;
overflow: visible;
}
Comparisons
| Approach | What it does | Best for | Notes |
|---|---|---|---|
element.scrollTop = element.scrollHeight | Instantly jumps to bottom | Chat, logs, simple UIs | Most common and reliable |
element.scrollTo(0, element.scrollHeight) | Scrolls to specific coordinates | Slightly more explicit API | Similar result |
element.scrollTo({ top: element.scrollHeight, behavior: 'smooth' }) | Smoothly scrolls to bottom | Polished UI interactions | May be less ideal for rapid message streams |
Page scrolling with window.scrollTo(...) | Scrolls the whole page | Full-page layouts | Not for inner div elements |
Cheat Sheet
const box = document.getElementById('scroll');
box.scrollTop = box.scrollHeight;
Rules
- The element must be scrollable.
- Scroll after new content is inserted.
- Use the actual container, not the page.
- Re-run the scroll logic after each AJAX update.
Useful properties
scrollTop→ current vertical scroll positionscrollHeight→ total content heightclientHeight→ visible inner height
Common helper
function scrollToBottom(element) {
element.scrollTop = element.scrollHeight;
}
Smooth scrolling
element.scrollTo({
top: element.scrollHeight,
behavior: 'smooth'
});
Near-bottom check
FAQ
How do I scroll a div to the bottom in JavaScript?
Use:
element.scrollTop = element.scrollHeight;
This moves the scrollable area to the bottom.
Why does my chat box not scroll to the bottom?
Usually one of these is the cause:
- the content was not added yet when you scrolled
- the wrong element was selected
- the container is not actually scrollable
- the script ran before the DOM was ready
How do I keep a div scrolled to the bottom after AJAX?
Call the scroll code immediately after you insert the AJAX response into the DOM.
Should I use overflow: scroll or overflow: auto?
Both work. auto is usually cleaner because scrollbars appear only when needed.
Can I make the scrolling smooth?
Yes:
element.scrollTo({ top: element.scrollHeight, behavior: 'smooth' });
Should I always auto-scroll in a chat app?
Not always. If the user has scrolled up to read older messages, forcing the scroll can be annoying. Many apps only auto-scroll when the user is already near the bottom.
Mini Project
Description
Build a simple chat message panel that lets a user add messages and automatically keeps the message list scrolled to the newest entry. This demonstrates the exact pattern used in chat windows, activity feeds, and log viewers.
Goal
Create a scrollable message container that automatically jumps to the bottom whenever a new message is added.
Requirements
- Create a scrollable container with a fixed height
- Add a button that appends a new message to the container
- Automatically scroll to the bottom after each new message
- Keep the JavaScript focused on the message container, not the whole page
Keep learning
Related questions
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
CSS :not() Selector for Excluding a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common mistakes.
Can HTML Checkboxes Be Readonly? Understanding readonly vs disabled in HTML Forms
Learn why HTML checkboxes do not support readonly, how disabled differs, and practical ways to prevent changes while still submitting values.