Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent div from scrolling despite position: absolute

Tags:

css

I am facing a bit of a 'div hell'. This is the html snippet:

<div id="xyz" class="sensoricon moveable type1 node-2-sensor-1 string1" style="position: absolute; top: 241.467px; left: 236.417px; opacity: 1;">
<img src="/content/images/shared/sensors/bla.png" alt="">
</div>

I understand that:

position: absolute;

should prevent the div from scrolling, but it still does (although only a little bit at the beginning). Can anyone see anything generally wrong?

like image 573
cs0815 Avatar asked Aug 13 '13 13:08

cs0815


People also ask

How do I stop div scrolling?

Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How do I keep my absolute div off screen?

You can use the min() function for conditional rendering like this. If 50vh would put the element off-screen, then 100vh - 100px will result in a smaller value. We can therefore use min() to put the element at the bottom if it would be off-screen.

Why you shouldn't use position absolute?

IF you were to use all absolute div tags you could easily have your tags overlap and hide each other when the data within them changed. Secondly, many good sites allow for the information to wrap depending on the screen resolution and browser size that a user uses to access the web page.


2 Answers

What you want to do is:

position: fixed;

Because position: absolute; sets position relative to parent coordinates and when the document is scrolled, absolute position is STILL relative to parent's and parents (through whatever count of levels) is relative to whole document position.

On the other hand position: fixed sets position relative to window's and not document's. That way your element stays in the same place when scrolled.

like image 66
Dovydas Navickas Avatar answered Sep 19 '22 13:09

Dovydas Navickas


Like i said in my comment:
You should use position: fixed.
It is similar to absolute, but it don't scroll with the whole site, it is "fixed" on the given position.

like image 27
Michael Schmidt Avatar answered Sep 21 '22 13:09

Michael Schmidt