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.
To create a fixed top menu, use position:fixed and top:0 .
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;.
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.
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; }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With