[ACCEPTED]-How to setup absolute position of a DIV with Jquery?-position

Accepted answer
Score: 17

As pointed out in the other answers, at 6 least one of the parent element of #lol has 5 a position set, that causes your element to be positioned 4 within the parent.

A solution with jQuery 3 would be to attach the element directly 2 to body.

$('#lol').css({ 
    position: "absolute",
    marginLeft: 0, marginTop: 0,
    top: 0, left: 0
}).appendTo('body');

This will make it to appear top 1 left of the window.

Score: 3

Why is that?

If you want to use position: absolute relatively 4 to the entire window, you need to make sure 3 that your #lol has no parent elements also positioned 2 absolute, fixed, or relative.

Otherwise, any positioning you 1 specify will take place relative to them.

Score: 3

Elements that have position: relative or position: absolute will be positioned 5 relative to the closest parent that has 4 position: relative or position: absolute. So, if you want your element to be 3 positioned relative to the entire window, keep 2 it outside of any parent wrappers with relative 1 or absolute positions.

Score: 0

That's just how positioning works. If you 7 have any parent elements with any position 6 specified the absolute positioning will 5 happen relative to them.

If you want it to 4 the window but can't do away with any of 3 the other elements' positioning you'll need 2 to remove the item from regular page flow 1 either manually or with a bit of JS.

Score: 0

If you want to use absolute:position on an element relative 6 to the entire window screen than any parent 5 of that element should not given any position 4 values like absolute,fixed or relative,because 3 the element will take the position relative 2 to its parent if any position attribute 1 is given to it. Hope it answer you well...:)

More Related questions