Skip to Content

How to Make a Div Scrollable — 3 Best Methods

How to Make a Div Scrollable — 3 Best Methods

A scrollable div is a container that can display more content than its actual size.

It allows users to scroll through the content that is not visible on the screen.

This is particularly useful when dealing with large amounts of data or displaying images, videos, or other types of content.

This post will give you a detailed guide of all the methods you can use to make your div scrollable.

How to Make a Div Scrollable

You can make a div scrollable using the CSS overflow property. You can also use the Bootstrap CSS framework using the overflow-auto class name. Javascript or jQuery will also come in handy if you want to add more functionality to the scroll bars.

Understanding Scrollable Divs in Web Development

You can make a div scroll either along the Y-axis or the X-axis.

If you make a div scrollable along the X-axis, you will have a scroll bar that you can drag to the right or left to view the hidden content. See the gif below.

Scroll a div along the x-axis

If you make a div scrollable along the Y-axis, you will have a scroll bar that you can drag up and down to view the hidden content. See the gif below.

Scroll a div along the Y-axis

Method 1: Make a Div Scrollable Using CSS Overflow Property

The easiest way to make a div scrollable is to use CSS. You can achieve this by setting the overflow property.

The CSS overflow property specifies whether content inside an element should be displayed when it overflows its boundaries. 

The CSS overflow property has four possible values:

  • visible: The default value. Content is not clipped and may be rendered outside the element’s box.
  • hidden: Content that overflows the element’s box is clipped and not viewable (hidden).
  • scroll: Adds a vertical and/or horizontal scrollbar to the element so that the content can be scrolled within the box.
  • auto: Similar to scroll, but only adds a scrollbar when necessary. It can add a scroll bar either along the X-axis, Y-axis or both.

Below is an example of making a div scrollable along the X-axis.

Below is an example of making a div scrollable along the Y-axis.

However, we highly recommend setting the overflow property to auto. That will make the browser determine where to put a scrolling bar depending on the size of the screen or the height/ width of the div container. See the example below.

By setting the overflow property to auto, you can see we have a scroll bar on both the X and Y axis of our div. See the gif below.

In this example, the scrollable class has been applied to the div, and its height has been set to 200 pixels.

The overflow property has been set to auto, adding a scrollbar to the div when the content exceeds its height or width.

Tip: If you want the scrollbar to always be visible, you can set the overflow property to scroll instead of auto.

Method 2: Make a Div Scrollable Using Inline CSS.

If you don’t want a separate file for your CSS styles, you can use inline CSS to make a scrollable div tag. Look at the HTML code below.

The HTML code above creates three HTML elements on our page – two paragraph tags and an image. By default, these elements would be stacked on top of each other, starting with the paragraph tags and then the image.

However, since we want to see the scroll bars, we will set the display property to flex using the .scrollable-div class. See the CSS code below.

When you open this code on your browser, you will see an output similar to the image below.

Make a Div scrollable Using CSS Overflow Property Method 2

Method 3: Make a Div Scrollable Using the Bootstrap Framework

If you are developing your project using a CSS framework like bootstrap, you don’t need to write a lot of CSS as that is already done for you. You can make a div scrollable using the overflow-auto class.

Here’s an example:

In this example, the .overflow-auto class is used to make the div scrollable, and the style attribute is used to set the height of the div to 200 pixels. 

The overflow-auto class has a CSS rule that sets the overflow property to auto, which enables the div to be scrolled when its content exceeds the specified height.

Method 4: Make a Div scrollable Using jQuery.

When you make a div scrollable using CSS, you might want to add some more functionality to it other than having a plain scrolling bar. For example, you might want to automatically scroll to the bottom of the div when the page is loaded.

Here’s an example of how you can achieve that using jQuery.

The HTML code above creates several paragraph elements on your web page. These paragraphs are enclosed in a div with the class name “scrollable-div.”

Below is the CSS code that we are using to style the div. We have set the height to 200px, and we have also enabled scrolling along the Y-axis using the overflow-y property.

Next, we add the jQuery code shown below. The code inside the “$(document).ready()” function sets the scrollTop property of the scrollable div to its maximum height using the “scrollHeight” property of the div element.

This will scroll the div to its bottom when the page is loaded.

When you open this code on your browser, you should see an output similar to Gif below.

How to Make a Div Scrollable with jQuery: Example 2

Below is a code snippet of how you can make a div scrollable with jQuery.

In this example, we are using jQuery to make the div with class “scrollable-div” scrollable. We are setting the CSS properties “overflow” to “auto” and “height” to “200px” using the “.css()” function.

How do I make a scrollable div in React?

To make a div scrollable in React, create a div element with a fixed height and overflow set to “scroll” or “auto” to make it scrollable. See the code snippet below.