Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fixed div with 100% width overlaps scrollbar

Tags:

html

css

As demonstrated here: http://codepen.io/anon/pen/rVPqeL

I am using 3 simple divs and I want to obtain an effect of a "global" scrollbar that has to go over the header.

The html is very basic

<div class="container">
    <div class="header">
    </div>
    <div class="content">
    </div>
</div>

and here's the css:

.container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
  overflow-y: scroll;
}

.header {
  position: fixed;
  width: 100%;
  height: 50px;
  background-color: red;
}

.content {
  margin-top: 50px;
  min-height: 2500px;
  background-color: blue;
}

The scrollbar keeps going under the header div. What am I doing wrong?

like image 662
sh0uzama Avatar asked Aug 06 '15 15:08

sh0uzama


People also ask

Can a div be scrollable?

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

Does width include scrollbar?

Many desktop browsers do include the scrollbar in the viewport width.

What is the width of scrollbar?

For Windows it usually varies between 12px and 20px . If the browser doesn't reserve any space for it (the scrollbar is half-translucent over the text, also happens), then it may be 0px .


3 Answers

.container {
  margin-top:50px; /* create room for header*/
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
  overflow-y: scroll;
}

.header {
  margin-top:-50px; /* move up by 50px*/
  position: fixed;
  width: 100%;
  height: 50px;
  background-color: red;
}

fixed positioned elements have "no width and height".

Hope it helps :)

EDIT: See this pen: This

Ps. I guess you also want to remove the margin of .content

like image 184
SimsaD Avatar answered Oct 19 '22 17:10

SimsaD


The below code does the trick http://codepen.io/anon/pen/XbOxgp

.container {
  background-color: gray;
  overflow-y: scroll;
}

.header {
  position: fixed;
  width: 100%;
  height: 50px;
  background-color: red;
  z-index: 2;
}

.content {
  z-index: 1;
  width: 100%;
  position: absolute;
  top: 60px;
  min-height: 2500px;
  background-color: blue;
}
like image 26
Jayan Karthik Pari Avatar answered Oct 19 '22 19:10

Jayan Karthik Pari


If I understand correctly you want the scrollbar always ontop. To do so change your css to the following

html{
    overflow-y: scroll;
}
.container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
}

Scroll on html will allow the entire page to have scroll while keeping header static and remove scroll from container.

like image 1
Bytesized Avatar answered Oct 19 '22 19:10

Bytesized