17

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?

sh0uzama
  • 582
  • 1
  • 6
  • 17

6 Answers6

4

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;
}
Saptarsi
  • 796
  • 5
  • 13
2

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;
}
fiskrisktisk
  • 158
  • 1
  • 12
1

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.

Bytesized
  • 133
  • 1
  • 15
1
.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

SimsaD
  • 27
  • 1
0

Remove overflow-y: scroll; from your .container

Ryan89
  • 1,216
  • 15
  • 20
  • 6
    If this answer is correct, which I think this is not the case, why would OP even have added `overflow-y: scroll` to his code? I think OP wants to scroll within the container and not within the whole page. Right? – Evochrome Aug 06 '15 at 16:22
-1

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;
}
Soul Eater
  • 505
  • 1
  • 7
  • 20