Look at the jsfiddle I created for you. Just give it position fixed, if you don't set top and left explicit, it will inherit for the parent, making it fixed for the parent and not for the window. So look at the css I wrote.
Remember if you set explicit top and left offset it will be referenced to the window, but if you make this trick, inherit from the parent and instead of using top
and left
you use margin-top
and margin-left
you will have your element fixed to the parent element and the offset given by the margin.
http://jsfiddle.net/carloscalla/q6ffnm09/
body{
margin: 0;
}
#absoluteinner{
position: fixed;
margin-top: 10px;
margin-left: 10px;
top: inherit;
left: inherit;
}
NOTE: margin: 0;
of body
is just to correct the margin given by the jsfiddle.
UPDATE: What you want to achieve can not be done with the html structure you have right now. You will have to wrap up your container in another div and inside this div put your #absoluteinner
so #absoluteinner
and #container
are siblings. Then you give the parent position: relative;
, and your #absoluteinner
position: absolute;
, but #absoluteinner
does not get into the #container
scroll, they are separated but you simulate that they are one inside of the other one.
I created an extra #outter-container
so you see another scroll and you see that #absoluteinner
is "fixed" to #container
.
If you use position fixed you will not be able to achieve this since position fixed takes the element out of flow, you can get that working for the basic scenario as the first answer I gave you but not for what you want to do if you want to insert this piece of html inside another containers.
Take a look at this jsfiddle I created for you: http://jsfiddle.net/carloscalla/gwphpfgy/4/
See the
<div id="container-wrapper" ...
this wraps up your container so you simulate a position fixed but this works inside other containers as well, as this sticks to the wrapper. Note the z-index: 1;
just because it was going behind the purple box, you can avoid this by reordering the html structure but this is not a big deal.