I have the following html. I have multiple elements (e.g div with id = one, two, three) inside a div container which is scrollable.
In each element, I need to use css position 'absolute' which position related to its parent div (i.e. class='Anchor').
The problem I am having is, when I scroll the outer container, none of the div with absolute position moved. My understand is position 'absolute' is it is positioned relatieve to its parent DIV element. Can you please tell me how can I make those 'absolute' position moved as I scroll the outer container?
<div style="overflow-y: scroll">
<div>
<div class="Anchor" id="one">
<div style="position: absolute"> something </div>
<div style="position: absolute"> something else </div>
</div>
</div>
<div>
<div class="Anchor" id="two">
<div style="position: absolute"> something </div>
<div style="position: absolute"> something else </div>
</div>
</div>
<div>
<div class="Anchor" id="three">
<div style="position: absolute"> something </div>
<div style="position: absolute"> something else </div>
</div>
</div>
</div>
An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.
The fixed value is similar to absolute as it can help you position an element anywhere relative to the document, however this value is unaffected by scrolling.
Absolute Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.
You must set position: relative;
on the parent div to get the child elements to move in relation to it.
The reality is, you can have the parent div set to any user-defined position, as long as the default static
position isn't being used.
try position: sticky
on the div that you want to make float. also beware the browser support is not that great for sticky https://caniuse.com/#feat=css-sticky
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With