To trick to solving this problem is that instead of separately declaring fixed positions for sidebar and the banner, group them under a common parent element whose position is set to fixed. You can then give the parent element a background colour to allow it to mask any content that comes under it.
See your revised example ported over to a JSFiddle: http://jsfiddle.net/88Sf9/1/
The revised CSS rules are as follow:
.wrapper {
width: 100%;
position: fixed;
}
.container {
width:100%;
margin:0 auto;
padding: 0px 15px;
background-color: black;
overflow:hidden;
}
.mainContent {
color: white;
padding-top: 290px;
width: 980px;
}
Some notes:
- You don't need to declare a negative z-index for the
.mainContent
- You can safely remove the
overflow: hidden;
declaration
I have also applied a top padding to your main container, so that the content will be padded away from the fixed elements at the top of the viewport when your scroll position along the y-axis is 0 (i.e. when you are at the top of the page).
However, the solution will not work if you are handling a container with dynamic background, e.g. images or gradients. For that case, you might want to look into using CSS masks, which suffers from lack of cross-browser support and standardization.