tags:

views:

216

answers:

4

I have a page with two divs on it which should fill the entire screen.

Both of them have width = 100%

The upper one's height should be defined by its content (the minimal possible height that fits all content) and never show any scrollbars.

The lower one should fill the rest of the screen. However, if its content does not fit the div, it should display the vertical scrollbar.

Like this:

<div id="header">This block should not display the scrollbars</div>
<div id="content">This block should fill the rest of the screen and show the vertical scrollbar if the content does not fit</div>

How do I do it with CSS?

Update:

I'm looking for a solution that would not require me to set the fixed height for the upper div.

Is that possible?

A: 

You should define fixed width for second div and use overflow css property to define scrollbars.

Eimantas
Also need to specify a height, or else you won't get scroll bars, the div will just expand in height.
Zoidberg
+3  A: 

In order to do it with CSS you need to define a height on the bottom div, and then add overflow:auto.

.content {
    height:90%;
    overflow:auto;
}

Unfortunately, this means that your top div will need a height defined as well, because content will have to take up a predefined amount of space on the page. If you use percentages for height, then you will need to define a height for your header div so stretching and shrinking the browser window doesn't cause issues.

Zoidberg
overflow auto will activate both scrollbars, horizontal and vertical, he only wants vertical scrollbars.
TeKapa
Yep, I know, in my experience the horizontal scroll bar is better than the div stretching sideways and pushing the vertical scrollbar off the view screen.
Zoidberg
then you use overflow-x hiden
TeKapa
True, if you want the content to be hidden...
Zoidberg
+5  A: 

this should fix your problem

#header{ overflow: hidden }

#content{ overflow-y: auto }

edit: you have to define the height of the divs aswell

TeKapa
But what about the divs fill the entire screen? Firts one should be the height of its content and the second fill the rest of the screen?
Eagle
+2  A: 

The only way I can see you achieving this is through Javascript. I know you didn't tag/ask for JS but I can't think of a straightforward, elegant CSS solution.

With JS you could capture the onpropertychange event of the header div, check to see if the property changed was offsetHeight/clientHeight and adjust the top style property of the content div. The content div would also need to have position:absolute; and bottom:0px;.

Sorry if you're not interested in a JS solution, I just don't think there is a CSS one without accepting a user experience below what you're trying to achieve.

Andy E
Yes, I know how to do this with JS, but I wonder if there is a way to do this with CSS
Eagle
What you're trying to do is define the total max height for 2 divs with one being a variable height and the other taking up the rest of the viewport. Really, I can only see this being possible with script with the current state of CSS.
Andy E