How can I add a vertical scrollbar to my div automatically?

jay picture jay · Aug 12, 2013 · Viewed 393.9k times · Source

I want to add a vertical scrollbar to my <div>. I've tried overflow: auto, but it is not working. I've tested my code in Firefox and Chrome.

I'm pasting the div style code here:

float: left;
width: 1000px;
overflow: auto;

Answer

Mr_Green picture Mr_Green · Aug 12, 2013

You need to assign some height to make the overflow: auto; property work.
For testing purpose, add height: 100px; and check.
and also it will be better if you give overflow-y:auto; instead of overflow: auto;, because this makes the element to scroll only vertical but not horizontal.

float:left;
width:1000px;
overflow-y: auto;
height: 100px;

If you don't know the height of the container and you want to show vertical scrollbar when the container reaches a fixed height say 100px, use max-height instead of height property.

For more information, read this MDN article.