Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header div stays at top, vertical scrolling div below with scrollbar only attached to that div

I have 2 main divs, the header and a scrolling list contained in a div. I want the header to always remain at the top of the page, and the scrolling list below. The scrollbar should be attached to the scrolling div and not to the whole page, i.e. the scrollbar does not appear on the right of the header, just the scrolling div.

This is what i'm trying to achieve:

______________________ |_______header_______| |                  |*| |   Container Div  |*| |                  |*| |                  |*| |                  |*| |                  |*| |                  |*| ----------------------  * = scrollbar 

The layout should be fluid and if the window is stretched vertically, only the container div and it's scrollbar should get longer.

I don't want to position the header position: fixed; as then the scrollbar will be on the right of it instead of underneath it.

like image 655
Garywoo Avatar asked Mar 13 '12 03:03

Garywoo


People also ask

How do you make a header stay on top in CSS?

To create a fixed top menu, use position:fixed and top:0 .

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;.

How do I make my vertical overflow scroll?

Use the overflow-y property to specify whether the content must be hidden, visible or scrolling vertically when the content overflows the element's top and bottom edges. Set the "auto" value. Use the text-align property with its "center" value.


2 Answers

HTML:

​<div class="header">This is the header</div> <div class="content">This is the content</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

CSS:

​.header {     height:50px; } .content {     position:absolute;     top: 50px;     left:0px;     right:0px;     bottom:0px;     overflow-y:scroll;         }​ 
like image 185
bhamlin Avatar answered Sep 24 '22 15:09

bhamlin


Found the flex magic.

Here's an example of how to do a fixed header and a scrollable content. Code:

<!DOCTYPE html> <html style="height: 100%">   <head>     <meta charset=utf-8 />     <title>Holy Grail</title>     <!-- Reset browser defaults -->     <link rel="stylesheet" href="reset.css">   </head>   <body style="display: flex; height: 100%; flex-direction: column">     <div>HEADER<br/>------------     </div>     <div style="flex: 1; overflow: auto">         CONTENT - START<br/>         <script>         for (var i=0 ; i<1000 ; ++i) {           document.write(" Very long content!");         }         </script>         <br/>CONTENT - END     </div>   </body> </html> 

* The advantage of the flex solution is that the content is independent of other parts of the layout. For example, the content doesn't need to know height of the header.

For a full Holy Grail implementation (header, footer, nav, side, and content), using flex display, go to here.

like image 35
AlikElzin-kilaka Avatar answered Sep 22 '22 15:09

AlikElzin-kilaka