Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CSS absolute position inside a scrollable div element

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>
like image 876
n179911 Avatar asked May 26 '16 21:05

n179911


People also ask

What CSS position properties that stay in place even if you scroll up or down left or right?

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.

Which position property remain unaffected while scrolling?

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.

How do you move an absolute div position?

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.


2 Answers

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.

like image 137
qarthandso Avatar answered Oct 03 '22 09:10

qarthandso


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

like image 38
Kireeti K Avatar answered Oct 03 '22 11:10

Kireeti K