Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a scrollbar in a div with fixed header and footer?

I have an website and some problems with the scrollbar.

What I want I can best explain with this image.

My Website

But I can't get the scrollbar like this.

I have tried some, here is the jsfiddle

In this fiddle I also have:

div[role="main"]
{
    overflow-y: scroll;
    margin: 60px 0;
}

But this margin is not OK, how do I know what margin I need without knowing the header and footer height.

like image 319
Ron van der Heijden Avatar asked Dec 06 '12 10:12

Ron van der Heijden


People also ask

How do I get the scroll bar inside a div?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I make my header follow the scroll?

If your header row locates on the top of the worksheet, please click View > Freeze Panes > Freeze Top Rows directly. See screenshot. Now the header row is frozen. And it will follow the worksheet up and down while scrolling the worksheet.

How can I add a horizontal scrollbar to my div automatically?

Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line. Here the scroll div will be horizontally scrollable.

How do you keep the header static on top when scrolling?

Answer: Use CSS fixed positioning You can easily create sticky or fixed header and footer using the CSS fixed positioning. Simply apply the CSS position property with the value fixed in combination with the top and bottom property to place the element on the top or bottom of the viewport accordingly.


2 Answers

This can be slowed by using padding and box-sizing = border-box on body ( with body height 100% it will count padding into height, so the box with scroll will be exactly between header and footer)

html {
 overflow: hidden;
 height: 100%;
}

body {
 padding: 60px 0px;
 height: 100%;
 box-sizing: border-box;
}

div[role="main"] {
 overflow-y: scroll;
 height: 100%;
}

see http://jsfiddle.net/wPucQ/

EDIT: Added forgotten HTML tag in code

like image 90
SergeS Avatar answered Sep 28 '22 06:09

SergeS


You need to give the scrollable element a height so the scroll-bar position can be calculated.

div[role="main"]
{
    height:400px;
    overflow-y: scroll;
    margin: 60px 0;
}

http://jsfiddle.net/gkxV4/

like image 34
Keyo Avatar answered Sep 28 '22 05:09

Keyo