Fixed Div overlaps scrollbar of static div

一世执手 提交于 2019-12-04 06:23:58

Your wrapper element is position:fixed; I think that you are talking about a overlay with a navigation panel on the left. Well, I had a similar situation and found out, that you can't use position:fixed; if your parent element is also position:fixed;. This will overlap the scrollbar of the parent wrapper (the overlay).

So you have to use position:absolute; instead or use this open source plugin to remove the width/height of the scrollbar from your element:

Scrollbar Fixer

First of all, you don't have to add "position: static;" style into your div, because it's static by default. There are two ways to solve your problem:

Add "position: relative;" into #content selector and declare #content after #left-panel in your HTML.

<div id="left-panel"></div>
<div id="content"></div>

#content{
    position: relative;
    width:100%;
    height:100%;
    background:grey;
}

Codepen: http://codepen.io/anon/pen/yoHxl

Or add "position: relative; z-index: 1" (z-index of #content should be higher of #left-panel's) into #content selector.

<div id="content"></div>
<div id="left-panel"></div>

#content{
    position: relative;
    width:100%;
    height:100%;
    background:grey;
    z-index: 1;
}

Codepen: http://codepen.io/anon/pen/HgwDx

Best, Marek

perhaps your problem is in this code here

<div style="width:110%;border-right:10px solid black;height:200px;background:yellow;"></div>

remove the width:110%; and it should be good to go.

Here's the Updated Fiddle

Edit

I think I misunderstand what's the problem before.

Because on your case, you use position: fixed; for #wrapper and #left-panel, and both use stlye left: 0, their position will overlap each other in left from the viewport, you basically need to position the left of #wrapper not to be in the same position as #left-panel, so you need to add this to #wrapper

left:200px; /* the width of #left-panel (if left panel has border, add it to this too )*/
right:0;

and remove

width:100%;

here's the Updated Fiddle

to make sure it's what you want, try change the style of the div inside #content to width:200%; and add margin:20px;

Edit Two

the cause for the scrollbar to overlap each other is because in the fiddle you use position: fixed for the #wrapper, thus will not create a scrollbar in the body, then you add overflow:auto in the #wrapper, causing the scrollbar to be created for the #wrapper and not the body, so you need to change the CSS for #wrapper to this

#wrapper{
    background:gray;
}

I don't include the height because for the div, the height is based on the child inside it, so you couldn't see the gray background, except you add some padding to it.

here's the Working Fiddle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!