Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollable div inside container

Tags:

html

css

I have the following HTML: http://jsfiddle.net/fMs67/. I'd like to make the div2 to respect the size of div1 and scroll the contents of div3.

Is this possible?

Thanks!

UPDATE-1:

This is the more advanced case that I oversimplified when I asked the question: http://jsfiddle.net/Wcgvt/. I need somehow that header+it's sibling div to not overflow the parent div's size.

like image 282
Dan L. Avatar asked Mar 21 '12 19:03

Dan L.


2 Answers

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.

like image 131
Chris Carew Avatar answered Sep 25 '22 12:09

Chris Carew


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

like image 33
Frank van Wijk Avatar answered Sep 22 '22 12:09

Frank van Wijk