How to Make a Div Scrollable Using CSS

Introduction

When the content inside a div exceeds its height or width, making the div scrollable is essential to allow users to view the overflow content without affecting the overall page layout. This can be easily done using CSS properties like overflow, overflow-y, and overflow-x to control vertical and horizontal scrolling.

In this tutorial, you'll learn how to make a div scrollable using CSS. We’ll cover both vertical and horizontal scrolling and make the scrollable area visually appealing.

Development Steps

  1. Create the HTML Structure for the Scrollable div: Set up the basic HTML structure.
  2. Set a Fixed Height or Width for the div: Use CSS to limit the size of the div.
  3. Enable Vertical and/or Horizontal Scrolling: Apply the overflow property to enable scrolling.
  4. Style the Scrollable Content: Add padding, borders, and other styles.
  5. Make the Scrollable div Responsive: Use media queries to adjust the scrollable area on smaller screens.

Step 1: Create the HTML Structure

We'll begin by creating a simple HTML structure with a container div that will hold the scrollable content.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scrollable Div Example</title>
</head>
<body>

    <!-- Step 1: Create a scrollable div -->
    <div class="scroll-container">
        <p>This is the first item in the scrollable div.</p>
        <p>This is the second item in the scrollable div.</p>
        <p>This is the third item in the scrollable div.</p>
        <p>This is the fourth item in the scrollable div.</p>
        <p>This is the fifth item in the scrollable div.</p>
        <p>This is the sixth item in the scrollable div.</p>
        <p>This is the seventh item in the scrollable div.</p>
        <p>This is the eighth item in the scrollable div.</p>
        <p>This is the ninth item in the scrollable div.</p>
        <p>This is the tenth item in the scrollable div.</p>
    </div>

</body>
</html>

Explanation:

  • The div with the class scroll-container contains multiple <p> elements. This content will overflow, which we will handle by making the div scrollable.

Step 2: Set a Fixed Height or Width for the div

To ensure the div becomes scrollable, we need to set a fixed height or width. This limits the visible content and allows the rest to be accessed via scrolling.

/* Step 2: Set fixed height and width */
.scroll-container {
    width: 300px;  /* Set a fixed width */
    height: 200px;  /* Set a fixed height */
    border: 2px solid #333;  /* Add a border around the container */
    padding: 10px;  /* Add padding inside the container */
}

Explanation:

  • width: 300px;: Sets a fixed width for the div.
  • height: 200px;: Sets a fixed height for the div. When content exceeds this height, scrolling will be enabled.
  • border: 2px solid #333;: Adds a border around the div to visually define the scrollable area.
  • padding: 10px;: Adds padding inside the container for better spacing.

Step 3: Enable Vertical and/or Horizontal Scrolling

Now, we’ll enable scrolling. You can control both vertical and horizontal scrolling using the overflow property. We’ll focus on vertical scrolling first and also look at how to enable horizontal scrolling if needed.

Vertical Scrolling

/* Step 3: Enable vertical scrolling */
.scroll-container {
    overflow-y: auto;  /* Enables vertical scrolling */
}

Horizontal Scrolling

If the content exceeds the width of the div, you can also enable horizontal scrolling.

/* Optional: Enable horizontal scrolling */
.scroll-container {
    overflow-x: auto;  /* Enables horizontal scrolling */
}

Both Vertical and Horizontal Scrolling

/* Enable both vertical and horizontal scrolling */
.scroll-container {
    overflow: auto;  /* Enables both vertical and horizontal scrolling */
}

Explanation:

  • overflow-y: auto;: Enables vertical scrolling when the content exceeds the height of the div.
  • overflow-x: auto;: Enables horizontal scrolling when the content exceeds the width of the div.
  • overflow: auto;: Enables both vertical and horizontal scrolling as needed.

Step 4: Style the Scrollable Content

Let’s add some additional styles to the scrollable content for better visual structure.

/* Step 4: Style the scroll items */
.scroll-container p {
    background-color: #f8f8f8;  /* Light gray background */
    margin: 0 0 10px;  /* Margin between paragraphs */
    padding: 10px;
    border-radius: 5px;  /* Rounded corners for each item */
}

Explanation:

  • background-color: #f8f8f8;: Sets a light gray background for each paragraph inside the scrollable div.
  • margin: 0 0 10px;: Adds space between the paragraphs.
  • padding: 10px;: Adds padding inside each paragraph for better readability.
  • border-radius: 5px;: Rounds the corners of the paragraphs for a softer look.

Step 5: Make the Scrollable div Responsive

To ensure that the scrollable div works well on smaller screens, you can use media queries to adjust its width or height.

/* Step 5: Responsive design for smaller screens */
@media (max-width: 600px) {
    .scroll-container {
        width: 100%;  /* Make the width flexible for smaller screens */
        height: 150px;  /* Adjust the height for mobile devices */
    }
}

Explanation:

  • width: 100%;: On screens smaller than 600px, the div takes the full width of the screen.
  • height: 150px;: Adjusts the height of the div to fit smaller screens, ensuring a compact layout.

Complete Code

Here’s the complete HTML and CSS code to make a div scrollable using CSS:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Scrollable Div Example</title>
    <style>
        /* Step 2: Set fixed height and width */
        .scroll-container {
            width: 300px;
            height: 200px;
            border: 2px solid #333;
            padding: 10px;
            overflow-y: auto;  /* Enables vertical scrolling */
        }

        /* Step 4: Style the scroll items */
        .scroll-container p {
            background-color: #f8f8f8;
            margin: 0 0 10px;
            padding: 10px;
            border-radius: 5px;
        }

        /* Step 5: Responsive design for smaller screens */
        @media (max-width: 600px) {
            .scroll-container {
                width: 100%;  /* Full width for small screens */
                height: 150px;  /* Adjusted height for smaller screens */
            }
        }
    </style>
</head>
<body>

    <!-- Scrollable Div -->
    <div class="scroll-container">
        <p>This is the first item in the scrollable div.</p>
        <p>This is the second item in the scrollable div.</p>
        <p>This is the third item in the scrollable div.</p>
        <p>This is the fourth item in the scrollable div.</p>
        <p>This is the fifth item in the scrollable div.</p>
        <p>This is the sixth item in the scrollable div.</p>
        <p>This is the seventh item in the scrollable div.</p>
        <p>This is the eighth item in the scrollable div.</p>
        <p>This is the ninth item in the scrollable div.</p>
        <p>This is the tenth item in the scrollable div.</p>
    </div>

</body>
</html>

Output

You can play with the above HTML in Online HTML Editor and Compiler. Here is the output of the above HTML page.:

Conclusion

In this tutorial, you learned how to make a div scrollable using CSS. By setting a fixed height or width and applying the overflow property, you can easily control vertical and horizontal scrolling. Additionally, you styled the scrollable content and made the section responsive to ensure usability across different screen sizes.

Comments