Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS position: absolute screen resolution problem

Tags:

html

css

CSS code:

top:45;
left:98;
float:right;
position:absolute;z-index:2;

I have done the above coding for a floating div when I was working on 1024 resolution, but when I tested the same on a different resolution it's out of alignment.

How can we fix it?

like image 663
dave Avatar asked Jul 21 '10 11:07

dave


People also ask

Why does position absolute not working?

If you are placing an element with absolute position, you need the base element to have a position value other than the default value. In your case if you change the position value of the parent div to 'relative' you can fix the issue.

Is it bad to use absolute positioning CSS?

As long as you structure your HTML so that the elements follow a logical order that makes sense when rendered without CSS, there is no reason why using absolute positioning should be considered bad practice. Save this answer.

What can I use instead of absolute position?

We can use CSS absolute positioning to overlap elements, but there's some advantages to using CSS grid instead.

Can I use display flex with position absolute?

If you position a flex item absolutely, it no longer participates in the flex layout. This means any flex properties on the item become moot. You can remove them if you like.


2 Answers

Absolutely positioned elements are positioned according to either a relatively positioned ancestor or the window. It sounds like in your case, it's being positioned in the window.

The way to fix this is to put your absolutely positioned <div> inside a relative container. That way, as the window changes size, it will stay in the correct spot:

<div style="position: relative">
    <div style="position: absolute; left: 98px; top: 45px;">
         This div will always be 98px from the left and 45px from the top of its parent .
    </div>
</div>
like image 170
Pat Avatar answered Oct 27 '22 16:10

Pat


None of the above solutions work for me... keep it on absolute position and give margin-top, margin-left...in %age like;

margin-top:10%;
margin-left:5%;

that will automatically adjust w.r.t. screen resolution...

this worked perfect for me. tested on different resolutions.

have a fun!

like image 35
Aamir Shahzad Avatar answered Oct 27 '22 14:10

Aamir Shahzad