burger menu icon
WillMaster

WillMasterBlog > JavaScript

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Auto-Scroll a Div to the Bottom

You can automatically scroll the content of a div to the bottom. Generally, this would be done for user convenience.

I had need for that functionality just this morning. It is the reason for this article. My thought was that some of you would appreciate knowing how to do this.

First, a div with an id is required. We'll use this one for a demonstration (contains a short list of CSS values).

fit−content
inherit
initial
max−content
min−content
revert
revert−layer
unset

The demonstration div is up to an inch high. It will have scrollbars when its content is more than will fit.

Any div with an id value can be told to scroll to the bottom. The div may contain any content a div can contain. I made the demo with scrollbars and border.

Here is the source code of the demo div.

<div id="my-demo-div" style="max-height:1in; max-width:fit-content; overflow:auto; border:1px solid black; padding:.5em; border-radius:.5em;">
fit&minus;content<br>
inherit<br>
initial<br>
max&minus;content<br>
min&minus;content<br>
revert<br>
revert&minus;layer<br>
unset
</div>

So far, so good.

To get the div to scroll to the bottom automatically, this JavaScript can be used.

<script type="text/javascript">
let d = document.getElementById("my-demo-div");
d.scrollTop = d.scrollHeight;
</script>

Replace my-demo-div with the id of the div to be scrolled. Place the JavaScript somewhere below the div to be scrolled. The scrolling will occur when the page loads.

If you prefer the scrolling to occur when the user clicks something or upon some other event, put the these two lines of JavaScript code into a JavaScript function. Then call the function when the event happens.

let d = document.getElementById("my-demo-div");
d.scrollTop = d.scrollHeight;

Only 2 lines of JavaScript are required to scroll a div automatically to the bottom.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Beacon

Send a beacon to your script. Optionally, also send information.

Digging for an IP Address

While an app's IP address can always be found in the REMOTE_ADDR value, the value might not be the actual destination of the content retrieved from your website.

Simple CSS Rotate

Rotate divs, tables, and even characters. Specify the number of degrees or fraction of a turn, and you're good to go.

Avoid Expired SSL Certificate

Check the expiration date of your domain's SSL certificate

Image Zoom on Hover

Zoom the image with a hover. CSS. No JavaScript.

The INS and DEL Tags

The INS and DEL tags come in mighty handy to display editing marks and to emphasize ideas with HTML markup.

Viewing HTML Source Code

Three ways to view HTML source code.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC