FIX for DIV Not Scrolling Vertically

This step-by-step guide will walk you through enabling vertical scrolling for your div, ensuring that your content remains accessible even when it exceeds the available space

Jun 20, 2023 | Read time 7 minutes

🔔 Table of contents

Introduction

One of the annoying things when trying to design a web page is adding scrolling and its not working.

The obvious fix is to use the overflow-y CSS property. However its not as straight forward.

I had this exact issue and had to go through a few steps to find out why my DIV is not scrolling vertically correctly.

Reasons why its not working

  • Incorrect CSS Selector: Ensure that you are targeting the correct element with the overflow-y property. Double-check the class or ID name to ensure it matches the desired div element.

  • Content Height - If the content within the div element is not tall enough to exceed its height, the scrollbar will not appear. Make sure the content is larger than the div container to trigger the overflow behavior.

  • Conflicting CSS - Check for any conflicting CSS rules that may be overriding or interfering with the overflow-y property. Inspect the CSS cascade and ensure there are no conflicting styles applied to the element or its parent elements.

  • Parent Container Constraints - If the parent container has a fixed height or overflow property set to hidden, it may prevent the child div from scrolling. Verify that the parent container allows for overflow and is not restricting the scrolling behavior.

  • Box Sizing - The box-sizing property affects the calculation of an element’s height and may impact the effectiveness of overflow-y. Ensure that the box-sizing property is set appropriately, such as box-sizing: content-box or box-sizing: border-box, based on your layout requirements.

  • CSS Specificity - If there are conflicting CSS rules targeting the same element, the one with higher specificity will take precedence. Check if there are more specific rules overriding the overflow-y property.

  • JavaScript Modifications: If there is JavaScript code modifying the overflow-y property or the DOM structure dynamically, ensure that it is not interfering with the desired scrolling behavior.

Using the overflow-y property

If you have a div element that is not scrolling vertically, you can apply CSS properties to enable scrolling. Here’s an example of how you can achieve this:

HTML:

<div class="scrollable-div">
  <!-- Content goes here -->
</div>

CSS:

.scrollable-div {
  overflow-y: auto;
}

In the above code, the overflow-y: auto; property is used to enable vertical scrolling for the div element. This property specifies whether to display a vertical scrollbar when the content of the div exceeds its height. If the content is taller than the div, a scrollbar will appear, allowing the user to scroll vertically to see the rest of the content.

You can adjust the overflow-y property according to your needs. Here are some possible values:

  • overflow-y: scroll; - Always display a vertical scrollbar, even if the content is not overflowing.
  • overflow-y: hidden; - Hide the overflow content and disable scrolling.
  • overflow-y: visible; - Display the overflow content without scrollbars.

Remember to apply these styles to the appropriate div element in your code.

Specify the height property

Using the height property is not mandatory to enable vertical scrolling for a div element. The height property controls the explicit height of an element, and if you don’t specify it, the div will automatically expand to fit its content.

However, if you want to limit the height of the div and make it scrollable when the content exceeds that height, you can combine the overflow-y property with a specific height value. Here’s an example:

HTML:

<div class="scrollable-div">
  <!-- Content goes here -->
</div>

CSS:

.scrollable-div {
  height: 200px; /* Set the desired height */
  overflow-y: auto;
}

In the above code, the height property is set to 200px, but you can adjust it according to your needs. If the content inside the div exceeds the specified height, a vertical scrollbar will appear, allowing the user to scroll through the overflow content.

If you omit the height property, the div will expand vertically to accommodate its content, and the scrollbar will only appear when the content exceeds the available space in the parent container.

Nested Divs

If you have nested div elements and you want to enable scrolling for the inner div while keeping the outer div fixed, you can achieve this by applying CSS properties to both elements. Here’s an example:

HTML:

<div class="outer-div">
  <div class="inner-div">
    <!-- Content goes here -->
  </div>
</div>

CSS:

.outer-div {
  height: 300px; /* Set the desired height for the outer div */
  overflow-y: hidden; /* Hide any overflow content */
}

.inner-div {
  height: 100%; /* Occupy the full height of the outer div */
  overflow-y: auto; /* Enable vertical scrolling for the inner div */
}

In the above code, the outer-div has a specified height and overflow-y set to hidden to hide any overflow content and prevent scrolling of the outer div. The inner-div has a height set to 100%, which allows it to occupy the full height of the outer div. The overflow-y property is set to auto to enable vertical scrolling for the inner div when its content exceeds its height.

With this setup, the outer div will remain fixed, and any overflow content within the inner div will be scrollable. Adjust the height and other properties according to your specific requirements.

Browser compatibility

  • Effectively all browsers support the CSS 2.1 definition for single-value overflow as well as overflow-x & overflow-y and values visible, hidden, scroll & auto
  • If you are still on IE 6-11 support, then it does not support the two value overflow shorthand
  • IE 6-11 also does not support the clip value

Summary

In this post, I went over some reasons why laying out DIVs and getting them to scroll vertically is not working correctly.

Now the obvious solution is to use overflow-y:auto, but with CSS, its not as simple as that.

You will need to consider if you have the issues of:

  • incorrect CSS selector,
  • parent DIV
  • box sizing model is correct
  • CSS styling conflicts

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube