I'm looking to create a small red div that spans the full width at a fixed top position within another div that has an overflow scroll property. I've provided a jsFiddle link for reference: http://jsfiddle.net/mCYLm/2/.
The problem arises when the red div overlaps with the scrollbar. It seems like setting right: 0
positions the right side of div.wrapper
, without taking into account the scrollbar of div.main
. Moving the overflow: scroll
to div.wrapper
resolves the issue (fiddle), but it sacrifices having a fixed position (scrolling down causes the banner to move up).
How can I achieve both these objectives simultaneously?
- Have the red banner stay fixed in place like in this example.
- Ensure the red banner spans full width except for the scrollbar, much like in this scenario.
This setup should work on Google Chrome.
HTML:
<div class="wrapper">
<div class="red-banner"></div>
<div class="main">
<div class="item">foo</div>
<div class="item">foo</div>
<div class="item">foo</div>
<div class="item">foo</div>
</div>
</div>
CSS:
div.wrapper {
position: relative;
}
div.main {
height: 200px;
overflow-y: scroll;
}
div.item {
border: 1px solid black;
margin: 20px;
padding: 20px;
}
div.red-banner {
background-color: red;
position: absolute;
left: 0;
top: 0;
right: 0;
height: 20px;
}