'fixed div with 100% width overlaps scrollbar

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?



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

.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

Solution 4:[4]

Remove overflow-y: scroll; from your .container

Solution 5:[5]

i tried with replacing position:fixed with position:sticky and added top:0 and it worked well for me, no more overlapping vertical scrollbar.

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

Solution 6:[6]

put the overflow-y: scroll; inside the body element:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jayan Karthik Pari
Solution 2 Bytesized
Solution 3 SimsaD
Solution 4 Ryan89
Solution 5 Saptarsi
Solution 6 Soul Eeater