scrollable div inside container

Adding position: relative to the parent, and a max-height:100%; on div2 works.

<body>
  <div id="div1" style="height: 500px;position:relative;">
    <div id="div2" style="max-height:100%;overflow:auto;border:1px solid red;">
      <div id="div3" style="height:1500px;border:5px solid yellow;">hello</div>
    </div>
  </div>

</body>​

Update: The following shows the "updated" example and answer. http://jsfiddle.net/Wcgvt/181/

The secret there is to use box-sizing: border-box, and some padding to make the second div height 100%, but move it's content down 50px. Then wrap the content in a div with overflow: auto to contain the scrollbar. Pay attention to z-indexes to keep all the text selectable - hope this helps, several years later.


Instead of overflow:auto, try overflow-y:auto. Should work like a charm!


If you put overflow: scroll on a fixed height div, the div will scroll if the contents take up too much space.

Tags:

Html

Css