Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto height div with overflow and scroll when needed

I'm trying to make a website with no vertical scrolling for a page, but i need that one of the DIVs i have to expand vertically to the bottom of the page (at most), and that when it has content that does not fit, the div should create a vertical scroller.

i already have the css for the inside of the div figured out in this fiddle, creating the scroller when needed. i also have figured out how to make the container div grow to occupy exactly the vertical space it has in the page. i just can't make them work out together!

please have in mind that in jsfiddle you won't be able to view the content of the whole website and in that sense what you get for the 2nd fiddle doesn't really show what's being done, but it works as i intended though.

just another note: as they are different fiddles, the id#container div in the 1st fiddle is he id#dcontent div of the 2nd example.

there is one other thing: for a type of content, this div will scroll vertically, but for other type of content, i want it to scroll horizontally, as it will have a product "slider" displaying elements horizontally inside this DIV.

please also look at this photo because it might be easier to understand what i'm trying to say: PICTURE

i tried looking to other questions regarding these topics, but none seemed to cover all of the aspects i'm trying to solve... :S

if there is something else i can provide to help you/me :) figuring it out, pls let me know!

thanks!

EDIT1: fixed typos

EDIT2: added picture for explanation

like image 258
Joum Avatar asked Apr 14 '12 16:04

Joum


People also ask

How do I make my Div overflow scroll?

Making a div vertically scrollable is easy by using CSS overflow property. There are different values in overflow property. For example: overflow:auto; and the axis hiding procedure like overflow-x:hidden; and overflow-y:auto;.

What is the difference between scroll and auto overflow?

overflow: autoThe auto value is similar to scroll , but it adds scrollbars only when necessary: You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.


2 Answers

I'm surprised no one's mentioned calc() yet.

I wasn't able to make-out your specific case from your fiddles, but I understand your problem: you want a height: 100% container that can still use overflow-y: auto.

This doesn't work out of the box because overflow requires some hard-coded size constraint to know when it ought to start handling overflow. So, if you went with height: 100px, it'd work as expected.

The good news is that calc() can help, but it's not as simple as height: 100%.

calc() lets you combine arbitrary units of measure.

So, for the situation you describe in the picture you include: picture of desired state

Since all the elements above and below the pink div are of a known height (let's say, 200px in total height), you can use calc to determine the height of ole pinky:

height: calc(100vh - 200px);

or, 'height is 100% of the view height minus 200px.'

Then, overflow-y: auto should work like a charm.

like image 110
Brandon Avatar answered Oct 04 '22 12:10

Brandon


Well, after long research, i found a workaround that does what i need: http://jsfiddle.net/CqB3d/25/

CSS:

body{     margin: 0;     padding: 0;     border: 0;     overflow: hidden;     height: 100%;      max-height: 100%;  }  #caixa{     width: 800px;     margin-left: auto;     margin-right: auto; } #framecontentTop, #framecontentBottom{     position: absolute;      top: 0;        width: 800px;      height: 100px; /*Height of top frame div*/     overflow: hidden; /*Disable scrollbars. Set to "scroll" to enable*/     background-color: navy;     color: white;  }  #framecontentBottom{     top: auto;     bottom: 0;      height: 110px; /*Height of bottom frame div*/     overflow: hidden; /*Disable scrollbars. Set to "scroll" to enable*/     background-color: navy;     color: white; }  #maincontent{     position: fixed;      top: 100px; /*Set top value to HeightOfTopFrameDiv*/     margin-left:auto;     margin-right: auto;     bottom: 110px; /*Set bottom value to HeightOfBottomFrameDiv*/     overflow: auto;      background: #fff;     width: 800px; }  .innertube{     margin: 15px; /*Margins for inner DIV inside each DIV (to provide padding)*/ }  * html body{ /*IE6 hack*/     padding: 130px 0 110px 0; /*Set value to (HeightOfTopFrameDiv 0 HeightOfBottomFrameDiv 0)*/ }  * html #maincontent{ /*IE6 hack*/     height: 100%;      width: 800px;  } 

HTML:

<div id="framecontentBottom">     <div class="innertube">         <h3>Sample text here</h3>     </div> </div> <div id="maincontent">     <div class="innertube">     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed scelerisque, ligula hendrerit euismod auctor, diam nunc sollicitudin nibh, id luctus eros nibh porta tellus. Phasellus sed suscipit dolor. Quisque at mi dolor, eu fermentum turpis. Nunc posuere venenatis est, in sagittis nulla consectetur eget... //much longer text...     </div> </div> 

might not work with the horizontal thingy yet, but, it's a work in progress!

I basically dropped the "inception" boxes-inside-boxes-inside-boxes model and used fixed positioning with dynamic height and overflow properties.

Hope this might help whoever finds the question later!

EDIT: This is the final answer.

like image 35
Joum Avatar answered Oct 04 '22 11:10

Joum