Overflow scroll on y axis with fixed height

Go To StackoverFlow.com

5

I have an apparently easy problem which is:

<div class="container">
    <div class="a"></div>
    <div class="b"></div>
    <div class="c"></div>
</div>​

I have 3 divs inside a container: A and B have fixed heights. C must have an extendable height, it extends along with the container height. If the content inside C are too big, I'd like C to scroll but to keep A and B in the same place.

Code in: http://jsfiddle.net/V2c9G/

I'm not able to do it.

I tried:

<div class="container">
    <div class="a"></div>
    <div class="b"></div>
    <div class="xxx" style="overflow-y:scroll">
       <div class="c"></div>
    </div>
</div>​

without success. The container div it's supposed to resize along the browser.

A complex example would be http://www.sencha.com/examples/#overview (I'm talking about the layout, make the browser smaller and you will see scrolls apperaring hile the headers keeps fixed) but it's not a solution since it uses JS to recalculate the heights.

Any idea?

2012-04-04 19:53
by Jordi P.S.
I think you have a slight misunderstanding. I updated your jSFiddle. Setting overflow-y on the div with the class "c" causes it to scroll when the content inside the element gets too long. It's not entirely clear the behavior you're expecting. Can you elaborate a little - George Mandis 2012-04-04 20:00
I'd like the green (c) div to fit into the red one (container) and the scroll to appear. I updated the fiddler - Jordi P.S. 2012-04-04 20:09
Imagine the container is the browser - Jordi P.S. 2012-04-04 20:10
Based on your last two comments, I have given an answer, do look into it - Jashwant 2012-04-04 20:17


1

Edit 3:

This is my recommended solution, which uses CSS from the Edit 2 below as a fallback, but uses JavaScript to resize your divs appropriately onload and when your window size changes. The CSS solution provides a decent starting point if the client has JavaScript disabled, and the JavaScript is such that it really shouldn't affect the performance of the page, so I see no reason not to use JavaScript to perfect what you want to see. A working fiddle can be seen here. Also, here is the appropriate JavaScript code:

var resizeDiv = function(){
    document.getElementById('c').style.height = getWindowHeight() - 64 + 'px';
};

//Framework code
var getWindowHeight = function(){
    if (window.innerHeight) {
        return window.innerHeight;
    }
    if (document.body && document.body.offsetHeight) {
        return document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' &&
        document.documentElement &&
        document.documentElement.offsetHeight ) {
        return document.documentElement.offsetHeight;
    }

    return 740;//provide a default height as a fallback
};

//resize on pageload
window.onresize = resizeDiv;
setTimeout(resizeDiv);




I think you need to adjust the absolute height on your third div to take up the rest of the space (either absolutely or with percentages), set overflow to hidden on the parent div, and let the content in the third inner div determine whether to show the scrollbar or not. Here's an updated fiddle using the absolute height method.

Edit:

From your "Imagine the container is the browser" comment (which to me means the scrollbar should be on the container), all you'd really have to do is set the overflow to 'scroll' and height in the third div to 'auto'. Here's an updated fiddle for that.

Edit #2:

According to your comment on this question, it sounds like you need to go with the percentage method. The most straightforward would be to make the height of a, b, and c a percentage (I had to tweak the margins to get it to fit for all zooms). Unfortunately with this method, the top components will not be fixed, and it sounds like you may be displaying static content there that would look funky. Thus, another option is to pick a minimum supported size for your browser window and adjust the percentage of the third element so that it just fits. Here's a fiddle for that. However, the downside there is that you'll have more empty space at the bottom of the page the bigger the height of the window, and you'll have 2 scrollbars below a certain height. To really do this properly with the fixed sized divs at the top, you'll need to add an event listener to the window.resize method and resize your third div when that happens appropriately based on the new size of the window.

Note: It is times like this where I wish the W3C would approve percentages plus pixels for their height, width, and other sizing properties!

2012-04-04 20:14
by Briguy37
I want exactly the first example you provided. The problem is that the container is the browser which can be resized. In your example C is not resized according to it's container - Jordi P.S. 2012-04-04 22:24
@JordiPlanadecursach: I've updated my answer for you with a few more options depending on your needs. This is definitely a problem where CSS is not very friendly to work with - Briguy37 2012-04-05 15:15
Hello, it almost works despite there is some empty space which is 20%-60px. Works perfectly when the container height is 60*5px - Jordi P.S. 2012-04-05 17:09
@JordiPlanadecursach: Yep, that's the price you must pay if you want to do it with pure CSS. Personally, the JavaScript method on top of the plain CSS method is what I'd go with. I've added it as my suggested solution to the problem - Briguy37 2012-04-05 19:01
Oh, really nice one! Thanks : - Jordi P.S. 2012-04-06 08:08


1

I think you might be searching for something like this: http://jsfiddle.net/QsLFt/. However, I'm not sure how to get rid of the divs hiding the scrollbar, the easiest solution would probably be to set it a fixed width?

2012-04-04 20:08
by Henrik Karlsson
Yes that's what I want! Good approach but not clean : - Jordi P.S. 2012-04-04 22:19


1

You need to apply overflow-y:scroll in .container

See this,

http://jsfiddle.net/v4ZtN/

Edit (after comments):

Css:

.container{
    background-color: red;
    width: 90%;
    margin: 0 auto; 
    height:220px;
}

.a{
   background-color: yellow;
   height: 30px;  
   margin: 2px;    
}

.b{
   background-color: blue;
   height: 30px;
   margin: 2px;    
}

.c{
   background-color: green;
   overflow-y: scroll;    
   height:inherit;
}

Html:

<div class="container">
    <div class="a"></div>
    <div class="b"></div>
    <div class="c"><img src="http://www.boingboing.net/images/_documents_anemone_images_anemone850-1.jpg" alt=""/></div>
</div>

Edit:2 (after comments)

Change .c style with this.

.c{
       background-color: green;
       overflow-y: scroll;    
       height:100%;
    }

Check this fiddle: http://jsfiddle.net/XM4gH/6/

2012-04-04 20:18
by Jashwant
I'd like A and B to keep fixed. The scroll should be only in C - Jordi P.S. 2012-04-04 22:19
Updated my answer. Try with different height of .container. Height of .c will update with height of .containerJashwant 2012-04-05 04:40
Sorry, height: inherit is not supported in IE7 / IE8. http://www.w3schools.com/cssref/prdimheight.as - Jordi P.S. 2012-04-05 08:24
Height:inherit is not supported in IE7 only. Also, now I have updated the answer and fiddle link with height:100%. Its working for me firefox 11. Do try it - Jashwant 2012-04-05 16:24
yes but then the total height is more than 100% of its container, having a part than we don'se - Jordi P.S. 2012-04-05 17:05
I dont get it.Can you point out the issue you are facing with this approach - Jashwant 2012-04-05 17:39


0

div only show scroll when you put some data in it, here is the result; jsfiddle

2012-04-04 20:09
by m-t
Yes but I'd like C to fit in the container - Jordi P.S. 2012-04-04 22:20
only have to adjust c height, hope you want this; http://jsfiddle.net/mtariq/V2c9G/12 - m-t 2012-04-04 22:28
yes but if the container's height grows the height of C doesn' - Jordi P.S. 2012-04-04 22:31


0

Based on what's currently up on your jsFiddle, I think you can simply add this to the style declarations for your .container class:

    overflow:hidden;

You'll have to actually add content to the .c div to see any scrolling however.

2012-04-04 20:12
by George Mandis


0

did this anser is match to your request ? enter link description here

.container{
    background-color: red;
    width: 90%;
    margin: 0 auto;

    height: 315px;
}
.a{
   background-color: yellow;
   height: 30px;  
   margin: 2px;    
width: 90%;
}


.b{
   background-color: blue;
   height: 30px; 
   margin: 2px;    
width: 90%;
}

.c{
   background-color: green;
   height: 250px; 
   margin: 2px; 
width: 90%;    
   overflow-y: scroll;    
}
​
2012-04-04 20:22
by israel love php


0

It's hard to tell exactly what you are trying to do based on the question, but if this is the desired result, these are the problems I discovered in your code:

  1. You needed to hide overflow on the red box, so the green box does not extend beyond the container
  2. In the green box, if there is enough data to extend, you want a scroll bar. This was working, but the height you had set specifically (250px) was enough to extend out of the container. You want a specific height here, the number is whatever is remaining in the container. I got 132px. Then with the overflow scroll applied, anything that extends beyond this height will be scrollable.
2012-04-04 20:32
by Josh Stodola
I don't want to use JS to recalculate the heights on resize, any idea - Jordi P.S. 2012-04-04 22:27


0

An alternative solution for the height layouts can be found in the following article:

http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/

2012-04-05 08:07
by Jordi P.S.
Ads